我正在尝试执行以下命令,该命令运行良好
我正在执行test.sh,它将调用script1.sh。
成功运行,输出如下
applicationuser@servername:/application> sudo ./test.sh
damn, there was an error
timeout happened这两个文件的内容如下
test.sh
timeout 10 script1.sh && echo "timeout not happened" || echo "timeout happened"script1.sh如下所示
if ech "right echo" 2>/dev/null ; then echo 'command was successful'; else echo 'damn, there was an error'; fi但是当我将两个脚本组合成一个文件(test.sh)时,如下所示,
test.sh
timeout 10 if ech "right echo" 2>/dev/null ; then echo 'command was successful'; else echo 'damn, there was an error'; fi && echo "timeout not happened" || echo "timeout happened"当我执行脚本时,我将得到如下语法错误
applicationuser@servername:/application> sudo ./test.sh ./test.sh:第1行:如果ech“右回送”2>/dev/null,则
then' ./test.sh: line 1:超时10附近的语法错误;然后回送‘命令成功’;否则回送‘该死,有错误’;fi && echo“超时未发生”
如何消除script1.sh,并将其内容放在test.sh中,并在没有语法错误的情况下执行?
发布于 2021-02-04 08:06:29
一个解决方案可以直接使用bash运行命令:
timeout 10 bash -c 'if ech "right echo" 2>/dev/null ; then echo "command was successful"; else echo "damn, there was an error"; fi && echo "timeout not happened" || echo "timeout happened"'https://stackoverflow.com/questions/66041349
复制相似问题