无法找出为什么我的 bash 脚本在访问被拒绝后退出

发布于 2024-09-18 16:31:53 字数 919 浏览 7 评论 0原文

我的脚本中有一部分是这样做的:

  1. 删除目录中的所有内容
  2. 强制同步该目录
  3. 将文件从另一个目录复制到所述目录,其中存在一些源代码管理防止被覆盖的冲突(这是可以预料的,并且是什么我想要)

在我得到这个之前:

...
cp <source path> <dest path>
echo done copying
...
echo done

输出:

...
Permission Denied:file1
Permission Denied:file2
done copying
...
done

所以,它会做这些事情,并完成。 然后,我进行了某种检查,以确保目录像这样退出:

if[ -d sourcepath ]
      then 
       if [ -d destpath ]
           then
              cp <source path> <dest path>
           else
              echo problem with dest
              exit 1
        fi
    else
        problem with source
       exit 1
fi

但现在脚本只是在最后一次权限拒绝后退出,之后没有命中任何内容,因此输出如下所示:

输出:

...
Permission Denied:file1
Permission Denied:file2

我对 bash 规则不太了解,所以我只是想在这里发布这个问题,因为我找不到它。但似乎在 if 中,存在权限问题导致它退出。

I have a part of my script that does this:

  1. Removes everything in directory
  2. Force syncs from perforce that directory
  3. copies files from another directory to said directory, of which there are some conflicts that the source control prevent from being overwritten (which is expectable and what I want)

Before I would have just this:

...
cp <source path> <dest path>
echo done copying
...
echo done

Output:

...
Permission Denied:file1
Permission Denied:file2
done copying
...
done

So, it would do the stuff, and reach done.
Then, I went and made a sort of check to make sure the directory exits like so:

if[ -d sourcepath ]
      then 
       if [ -d destpath ]
           then
              cp <source path> <dest path>
           else
              echo problem with dest
              exit 1
        fi
    else
        problem with source
       exit 1
fi

But now the script just exits after the last of the Permission Denies, not hitting anything after, so the output is like this:

Output:

...
Permission Denied:file1
Permission Denied:file2

I'm not too savvy in the bash rules, so I just thought I'd post this question here since I couldn't find it. It seems that in the if, though, the fact that there are permission problems cause it to exit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2024-09-25 16:31:53

除了在此处的剪切和粘贴过程中可能引入的语法错误之外,该代码没有任何问题,如以下实验所示:

#!/bin/bash

rm -rf sourcepath destpath
mkdir sourcepath
mkdir destpath
touch sourcepath/xyz
touch destpath/xyz
chmod 000 destpath/xyz

if [ -d sourcepath ]
      then
       if [ -d destpath ]
           then
              cp sourcepath/xyz destpath/xyz
           else
              echo problem with dest
              exit 1
        fi
    else
       echo problem with source
       exit 1
fi
echo Woohoo!

运行时,此输出:

cp: cannot create regular file `destpath/xyz': Permission denied
Woohoo!

因此您可以看到它在失败后继续运行。

一条非常宝贵的建议:当您调试 bash 脚本时,请将以下行:

set -x

放在顶部(如果有的话,放在 #!/bin/bash 之后)。这将导致它在执行每个命令之前输出它。

事实上,我总是以以下开头纠正我的脚本:

#!/bin/bash
#set -x

所以我可以取消注释第二行以进行调试。

顺便说一句,我会将其编码为:

if [[ ! -d sourcepath ]] ; then
    problem with source
    exit 1
fi
if [[ ! -d destpath ]] ; then
    problem with dest
    exit 1
fi
cp <source path> <dest path>

但这只是因为我不喜欢复杂的 if-then-else 结构。

Other than syntax errors presumably introduced during cut'n'paste here, there's nothing wrong with that code, as shown by the following experiment:

#!/bin/bash

rm -rf sourcepath destpath
mkdir sourcepath
mkdir destpath
touch sourcepath/xyz
touch destpath/xyz
chmod 000 destpath/xyz

if [ -d sourcepath ]
      then
       if [ -d destpath ]
           then
              cp sourcepath/xyz destpath/xyz
           else
              echo problem with dest
              exit 1
        fi
    else
       echo problem with source
       exit 1
fi
echo Woohoo!

When run, this outputs:

cp: cannot create regular file `destpath/xyz': Permission denied
Woohoo!

so you can see it's carrying on after the failure.

One piece of invaluable advice: when you're debugging bash scripts, put the line:

set -x

right up the top (after #!/bin/bash if it's there). That will cause it to output each command before executing it.

In fact, I always right my scripts starting with:

#!/bin/bash
#set -x

so I can just uncomment the second line for debugging purposes.

As an aside, I would code that as:

if [[ ! -d sourcepath ]] ; then
    problem with source
    exit 1
fi
if [[ ! -d destpath ]] ; then
    problem with dest
    exit 1
fi
cp <source path> <dest path>

but that's just because I don't like complicated if-then-else constructs.

新雨望断虹 2024-09-25 16:31:53

你必须检查写入权限

test -w / && echo "writable" 
test -w ~ && echo "writable" 

you have to check write permissions

test -w / && echo "writable" 
test -w ~ && echo "writable" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文