我有一个脚本,除了执行位置参数之外没有任何用处。(我知道安全风险,并且该脚本没有任何用处,因为它是一个最小的工作示例。)
$ cat script
> #!/usr/bin/env bash
>
> eval "${*}"
$ cat "docu ment"
> Lorem ipsum dolor sit amet
我想做的是用./script cat "docu ment"
, 或调用脚本./script cat docu\ ment
,但引号或转义字符消失,脚本将尝试cat docu ment
,这是行不通的。在这种情况下,我将如何修复报价?
编辑:我真正想做的是多次调用命令,直到它返回成功的退出代码,或者它尝试了 n 次。我的脚本如下所示:
#!/usr/bin/env bash
# Try command several times, until it reports success (exit code 0)
# or I give up (tried n times)
tryMax=10
try=1
# Do until loop in bash
while
eval "${@}"
exitcode="${?}"
[[ "${exitcode}" -ne 0 && "${try}" -lt "${tryMax}" ]]
do (( try++ ))
done
if [[ "${exitcode}" -ne 0 ]]; then
echo -n "I tried hard, but did not manage to make this work. The exit code "
echo "of the last iteration of this command was: ${exitcode}."
exit "${exitcode}"
fi