使用超时意外令牌'}'时Shell语法错误



我不确定我得到的语法错误是否真的是语法错误。

timeout 120 bash -c "date && (time nc -z -v -w 10 -q 60 google.com 443 && date) || {echo 'running command failed.' ; date ; exit 1 ; }"

语法错误:

bash: -c: line 0: syntax error near unexpected token `}'

它在抱怨右花括号,因为它没有看到一个开花括号——
这是因为它需要空白来将它与echo分开,以使它成为一个不同的东西。

$: bogus && echo ok || {echo 'running command failed.' ; date ; exit 1 ; }
-bash: syntax error near unexpected token '}'
$: bogus && echo ok || { echo 'running command failed.' ; date ; exit 1 ; }
-bash: bogus: command not found
running command failed.
Mon Jun 20 18:16:02 GMT 2022
logout

问题的真正根源:

$: {echo 'running command failed.'
bash: {echo: command not found

把空格留起来,如果行的话

$: timeout 120 bash -c "date && (time nc -z -v -w 10 google.com 443 && date) || { echo 'running command failed.' ; date ; exit 1 ; }"
Mon Jun 20 18:11:46 GMT 2022
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 2607:f8b0:4004:837::200e:443.
Ncat: 0 bytes sent, 0 bytes received in 0.04 seconds.
real    0m0.039s
user    0m0.007s
sys     0m0.008s
Mon Jun 20 18:11:46 GMT 2022

试着一次粘贴一块到https://www.shellcheck.net/

Line 2:
{echo 'running command failed.' ; date ; exit 1 ; }"
^-- SC1054 (error): You need a space after the '{'.

相关内容

  • 没有找到相关文章