Home > Mobile >  Shell syntax error when using timeout unexpected token `}'
Shell syntax error when using timeout unexpected token `}'

Time:06-21

I am not sure if the syntax error I am getting is really a syntax error.

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

Syntax error:

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

CodePudding user response:

It's complaining about the close curly because it hasn't seen an open curly -
which is because it requires whitespace to separate it from the echo to make it a distinct thing.

$: 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

The real root of the problem:

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

Put that space it it runs fine.

$: 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

Try pasting it in a piece at a time at https://www.shellcheck.net/

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