Linux 如何将命令的一部分存储到变量中
我有下面的bash脚本Linux 如何将命令的一部分存储到变量中,linux,bash,find,Linux,Bash,Find,我有下面的bash脚本 CMD="\( -path ./.git -o -name .gitignore \) -prune -o"; find . $CMD -print find . $CMD -exec chmod 440 {} \+ 我需要列出除.git dir和.gitignore文件中的文件之外的所有文件,然后对它们进行chmod。除了,您真的不应该运行两次find,尤其是当您可以在一个命令中组合-print和-exec时。除了,您真的不应该运行两次find,特别是当您可以在一个
CMD="\( -path ./.git -o -name .gitignore \) -prune -o";
find . $CMD -print
find . $CMD -exec chmod 440 {} \+
我需要列出除.git dir和.gitignore文件中的文件之外的所有文件,然后对它们进行chmod。除了,您真的不应该运行两次
find
,尤其是当您可以在一个命令中组合-print
和-exec
时。除了,您真的不应该运行两次find
,特别是当您可以在一个命令中组合-print
和-exec
时。而不是使用find
,您可以使用dotglob
和extglob
:
shopt -s dotglob extglob
for file in !(.git?(ignore)); do echo "$file" && chmod 440 "$file"; done
dotglob
意味着glob扩展还包括以
开头的文件,extglob
启用“扩展globbing”,这允许您否定匹配,以及其他内容。在本例中,glob匹配所有不是.git
或.gitignore
的文件
要在完成后取消设置这些选项,可以使用shopt-u dotglob extglob
有关更多详细信息,请参阅。您可以使用
dotglob
和extglob
而不是使用find
:
shopt -s dotglob extglob
for file in !(.git?(ignore)); do echo "$file" && chmod 440 "$file"; done
dotglob
意味着glob扩展还包括以
开头的文件,extglob
启用“扩展globbing”,这允许您否定匹配,以及其他内容。在本例中,glob匹配所有不是.git
或.gitignore
的文件
要在完成后取消设置这些选项,可以使用shopt-u dotglob extglob
有关更多详细信息,请参阅。尽管tripleee担心,但使用数组进行编码的正确方法是:
find_opts=( \( -path ./.git -o -name .gitignore \) -prune -o )
find . "${find_opts[@]}" -print
find . "${find_opts[@]}" -exec chmod 440 {} \+
尽管tripleee担心,但正确的编码方法是使用数组:
find_opts=( \( -path ./.git -o -name .gitignore \) -prune -o )
find . "${find_opts[@]}" -print
find . "${find_opts[@]}" -exec chmod 440 {} \+
find
有一个-not
选项。要扩展@tripleee所说的不使用字符串的内容,请使用数组@MarcB GNU find有,POSIX和busybox没有(它们使用!
)。@MarcB-not-path
不是-prune
的正确方法。find
有一个-not
选项。要扩展@tripleee所说的不使用字符串,请使用数组@MarcB GNU find做,POSIX和busybox不做(它们使用!
)。@MarcB-not-path
不是-prune
的正确方法。