问题
From here: http://www.sat.dundee.ac.uk/psc/watchdog/watchdog-testing.html
for n in $(seq 1 60); do echo $n; sleep 1; sync; done
I get:
:~$ sudo for n in $(seq 1 60); do echo $n; sleep 1; sync; done
bash: syntax error near unexpected token `do'
回答1:
The shell parses the command line and because for
looks like an argument to sudo
, you basically get a do
without a for
.
To fix it, run the loop in a subshell, either as a separate script, or like this;
sudo sh -c 'for n in $(seq 1 60); do echo "$n"; sleep 1; sync; done'
Better yet, avoid running anything unnecessary as a privileged user:
for n in $(seq 1 60); do echo "$n"; sleep 1; sudo sync; done
The first sudo
will require a password, but subsequent iterations should have it cached, with the default settings on most distros.
If you are on Bash, you can use {1..60}
instead of $(seq 1 60)
. Obviously, if you want to use Bash-specific syntax inside the single quotes in the first example, you need bash -c
instead of sh -c
回答2:
for
is an internal function (not to be confused with functions) of a shell that's why you can't call it. You should explicitly call the binary of the shell that runs with the code like this:
sudo sh -c 'for n in $(seq 1 60); do echo "$n"; sleep 1; sync; done'
With bash:
sudo bash -c 'for n in {1..60}; do echo "$n"; sleep 1; sync; done'
sudo bash -c 'for ((n = 1; n <= 60; ++n)); do echo "$n"; sleep 1; sync; done'
回答3:
It's because the fist semicolon terminates the sudo
command, which will make do
a new command. The easiest way to fix this is to put the loop inside a file and execute it, like
sudo /bin/bash ./myfile
来源:https://stackoverflow.com/questions/25175596/syntax-error-near-unexpected-token-do-when-run-with-sudo