【发布时间】:2023-01-11 04:24:11
【问题描述】:
我想传入多个命令在单个函数调用中使用 && 运算符。每个命令都可以有不同数量的参数,所以我使用 $@ 来捕获所有参数。使用单个命令,下面的函数可以正常工作。
function try_log () {
$@ \
&& echo "PASS!"
|| echo "FAIL!"
}
$ try_log touch foo bar
PASS!
$ try_log rm foo bar
PASS!
$ try_log rm foo bar
rm: cannot remove 'foo': No such file or directory
rm: cannot remove 'bar': No such file or directory
FAIL!
我现在想在输入命令上添加对 && 运算符的支持。目的是运行整个命令,然后在最后显示 PASS 或 FAIL。我尝试了以下但没有成功。
try_log ls && ls # runs the send ls after the first completes
foo bar
PASS!
foo bar
try_log 'ls && ls'
ls: cannot access '&&': No such file or directory
ls: cannot access 'ls': No such file or directory
FAIL!
$ try_log `ls && ls`
foo: command not found
FAIL!
【问题讨论】:
标签: bash