避免在 shell 脚本中使用单独的文件
Avoid seperate file in shell script
我正在尝试执行以下命令 效果很好
我正在执行 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: line
1: syntax error near unexpected token then' ./test.sh: line 1:
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"'
如何消除script1.sh并将其内容放入test.sh并执行无语法错误?
一个解决方案可以是 运行 直接使用命令 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"'
我正在尝试执行以下命令 效果很好
我正在执行 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: line 1: syntax error near unexpected token
then' ./test.sh: line 1:
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"'
如何消除script1.sh并将其内容放入test.sh并执行无语法错误?
一个解决方案可以是 运行 直接使用命令 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"'