无法找出为什么我的 bash 脚本在访问被拒绝后退出
我的脚本中有一部分是这样做的:
- 删除目录中的所有内容
- 强制同步该目录
- 将文件从另一个目录复制到所述目录,其中存在一些源代码管理防止被覆盖的冲突(这是可以预料的,并且是什么我想要)
在我得到这个之前:
...
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:
- Removes everything in directory
- Force syncs from perforce that directory
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了在此处的剪切和粘贴过程中可能引入的语法错误之外,该代码没有任何问题,如以下实验所示:
运行时,此输出:
因此您可以看到它在失败后继续运行。
一条非常宝贵的建议:当您调试
bash
脚本时,请将以下行:放在顶部(如果有的话,放在
#!/bin/bash
之后)。这将导致它在执行每个命令之前输出它。事实上,我总是以以下开头纠正我的脚本:
所以我可以取消注释第二行以进行调试。
顺便说一句,我会将其编码为:
但这只是因为我不喜欢复杂的
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:
When run, this outputs:
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: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:
so I can just uncomment the second line for debugging purposes.
As an aside, I would code that as:
but that's just because I don't like complicated
if-then-else
constructs.你必须检查写入权限
you have to check write permissions