[英]Syntax error when I try to use bash from sh `echo 'rm -rf !(cookbooks)' | bash`
When I use /bin/sh I can issue commands through bash simply by echoing to it 当我使用/ bin / sh时,我可以通过回显它来通过bash发出命令
vagrant@vagrant:~$ sh
$ echo 'ls' | bash
some.sh
But when I try to use this command rm -rf !(cookbooks)
I get this 但是当我尝试使用此命令
rm -rf !(cookbooks)
我得到了
$ echo 'rm -rf !(cookbooks)' | bash
bash: line 1: syntax error near unexpected token `('
bash: line 1: `rm -rf !(cookbooks)'
And I need issue this command from /bin/sh. 我需要从/ bin / sh发出此命令。
@anubhava from a packer http://www.packer.io/ provision script 来自打包程序http://www.packer.io/设置脚本的@anubhava
if [ -d "/opt/chef/chef-solo/" ]; then
cd /opt/chef/chef-solo
rm -rf !(cookbooks)
fi
!(cookbooks)
is an extglob. !(cookbooks)
是extglob。 They're not enabled by default; 默认情况下未启用它们。 you need to run
shopt -s extglob
in a prior line of bash (because the parser operates line-by-line) to make it valid. 您需要在bash的前一行中运行
shopt -s extglob
(因为解析器逐行操作)以使其有效。
So: 所以:
printf '%s\n' 'shopt -s extglob' 'rm -rf !(cookbooks)' | bash
...or you can enable extglobs via the command line (thanks to chepner for the addendum): ...或者您可以通过命令行启用extglobs(感谢chepner的附录):
echo 'rm -rf !(cookbooks)' | bash -O extglob
By the way, it is possible to do this in POSIX sh, without use of extglobs. 顺便说一句,这是可以做到这一点POSIX SH,不使用extglobs的。 For instance:
例如:
# wrap in a function to have a separate $@ rather than overriding global
delete_all_but_cookbooks() {
set --
for f in *; do
[ "$f" = "cookbooks" ] || set -- "$@" "$f"
done
rm -rf "$@"
}
delete_all_but_cookbooks
...or, much simpler, just using find: ...或更简单,只需使用find:
find . -mindepth 1 -maxdepth 1 -name cookbooks -prune -o -exec rm -rf '{}' +
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.