使用sudo运行时,语法错误接近意外令牌“do”

时间:2022-01-31 22:43:48

From here: http://www.sat.dundee.ac.uk/psc/watchdog/watchdog-testing.html

在这里:http://www.sat.dundee.ac.uk/psc/watchdog/watchdog-testing.html

for n in $(seq 1 60); do echo $n; sleep 1; sync; done

n元(seq 1 60);echo $ n;睡眠1;同步;完成

I get:

我得到:

:~$ sudo for n in $(seq 1 60); do echo $n; sleep 1; sync; done  
bash: syntax error near unexpected token `do'

3 个解决方案

#1


7  

The shell parses the command line and because for looks like an argument to sudo, you basically get a do without a for.

shell解析命令行,因为for看起来像sudo的参数,所以基本上不需要for。

To fix it, run the loop in a subshell, either as a separate script, or like this;

要修复它,可以在子shell中运行循环,或者作为单独的脚本,或者像这样;

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.

第一个sudo将需要一个密码,但是后续的迭代应该缓存它,在大多数发行版上使用默认设置。

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

如果您在Bash中,可以使用{1.. .60}而不是$(seq1 60)。显然,如果希望在第一个示例中的单引号中使用特定于bash的语法,需要bash -c而不是sh -c

#2


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:

因为是壳层的内部函数(不要和函数混淆),所以不能调用它。您应该显式地调用shell的二进制,该shell使用如下代码运行:

sudo sh -c 'for n in $(seq 1 60); do echo "$n"; sleep 1; sync; done'

With bash:

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


1  

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命令,该命令将执行一个新命令。解决这个问题的最简单方法是将循环放在文件中并执行它,比如

sudo /bin/bash ./myfile

#1


7  

The shell parses the command line and because for looks like an argument to sudo, you basically get a do without a for.

shell解析命令行,因为for看起来像sudo的参数,所以基本上不需要for。

To fix it, run the loop in a subshell, either as a separate script, or like this;

要修复它,可以在子shell中运行循环,或者作为单独的脚本,或者像这样;

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.

第一个sudo将需要一个密码,但是后续的迭代应该缓存它,在大多数发行版上使用默认设置。

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

如果您在Bash中,可以使用{1.. .60}而不是$(seq1 60)。显然,如果希望在第一个示例中的单引号中使用特定于bash的语法,需要bash -c而不是sh -c

#2


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:

因为是壳层的内部函数(不要和函数混淆),所以不能调用它。您应该显式地调用shell的二进制,该shell使用如下代码运行:

sudo sh -c 'for n in $(seq 1 60); do echo "$n"; sleep 1; sync; done'

With bash:

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


1  

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命令,该命令将执行一个新命令。解决这个问题的最简单方法是将循环放在文件中并执行它,比如

sudo /bin/bash ./myfile