$ ls -l /tmp/test/my\ dir/
total 0
Run Code Online (Sandbox Code Playgroud)
我想知道为什么以下运行上述命令的方法失败或成功?
$ abc='ls -l "/tmp/test/my dir"'
$ $abc
ls: cannot access '"/tmp/test/my': No such file or directory
ls: cannot access 'dir"': No such file or directory
$ "$abc"
bash: ls -l "/tmp/test/my dir": No such file or directory
$ bash -c $abc
'my dir'
$ bash -c "$abc"
total 0
$ eval $abc
total 0
$ eval "$abc"
total 0
Run Code Online (Sandbox Code Playgroud) 我需要使用“find”命令在我的 Bash 函数中查找几组不同的文件,具体取决于我的脚本输入。
所以,我有这样的事情:
DAYS=30
case $1 in
A1) ARGLINE="-name 'FOO*.xml' -or -name 'BAR*.xml' -or -name 'BTT*.txt'"
;;
A2) ARGLINE="-name 'PO*xml' -or -name 'PR*xml'"
;;
...
esac
find . -maxdepth 1 -type f -mtime +${DAYS} `${ARGLINE}`
Run Code Online (Sandbox Code Playgroud)
这有效。
但是,只要我想使用变量搜索返回的天数,就像这样:
DAYS=30
case $1 in
A1) ARGLINE="-name 'FOO*.xml' -or -name 'BAR*.xml' -or -name 'BTT*.txt'"
;;
A2) ARGLINE="-name 'PO*xml' -or -name 'PR*xml'"
;;
...
esac
if [[ $# -gt 1 ]]; then
DAYS=$2
fi
find . -maxdepth 1 -type f -mtime +${DAYS} `${ARGLINE}`
Run Code Online (Sandbox Code Playgroud)
当 …