[英]Bash test and execute if directory pattern exists
How do you do an inline test for the existence of a directory pattern? 如何对目录模式的存在进行内联测试?
If a directory pattern exists, then I want to chmod that pattern. 如果存在目录模式,那么我想chmod该模式。
eg I'm trying to do the following: 例如,我正在尝试执行以下操作:
[ -d /usr/local/myproject/*/bin ] && chmod +x /usr/local/myproject/*/bin/*
but this gives me the error "-bash: [: too many arguments". 但这给了我错误“-bash:[:太多的论点”。
没有必要测试:
chmod +x /usr/local/myproject/*/bin/* 2>/dev/null
It doesn't work because -d
test takes one argument. 它不起作用,因为-d
test需要一个参数。 You seem to be passing more than one. 你似乎传递了不止一个。 A fix would be: 修复将是:
for dir in /usr/local/myproject/*/bin; do
[[ -d $dir ]] && chmod +x $dir/*
done
为了从我的答案中挽救一些有用的东西,假设你有太多的bin目录,你不能这样做yi_H的方式。
find /usr/local/myproject/ -path '/usr/local/myproject/*/bin' -maxdepth 2 -type d -exec chmod a+x {} + 2>/dev/null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.