Syntax Error Near Unexpected Token 'Do' When Run with Sudo

syntax error near unexpected token `do' when run with sudo

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

-bash: syntax error near unexpected token `do'

Running the for via sudo doesn't work as sudo expects a command. You can instead run the loop via bash:

sudo bash -c 'for i in {0..9}; do nohup command > log_$i.txt & done'

You wouldn't need to use seq command as bash has the {0..9} to support "range" loops.

See bash job control for more info on & (which puts the "job" - the command you run - in the background).

syntax error near unexpected token `do' in bash script

You have some issues with your formatting and syntax. sjsam's advice to use shellcheck is good, but the short version is that you should be using square brackets instead of round ones on the internal brackets of your if statement:

if [ ${file: -4} == "$1" ] || [ ${file: -4 } == "$2" ] {

And I don't think you need the 'do' before your ffmpeg line or the curly bracket at the end of the line above, so you end up with...

for file in *.*; 
do
#comparing the file types in the directory to the first 2 parameters passed
if [ ${file: -4} == "$1" ] || [ ${file: -4 } == "$2" ]
export extension=${file: -4}
#converting such files to the type of the first parameter using the FFMPEG comand
ffmpeg -i "$file" "${file%.extension}"$3;
fi
done

syntax error near unexpected token `(' with let command in bash script

Ah, here's the culprit: in an interactive session:

$ declare -p BASH_VERSINFO
declare -ar BASH_VERSINFO='([0]="4" [1]="2" [2]="45" [3]="1" [4]="release" [5]="i386-apple-darwin19.5.0")'

$ let C=1+(2) && echo $C
3

$ shopt -u extglob

$ let C=1+(2) && echo $C
bash: syntax error near unexpected token `('

+(pattern) is an extended glob pattern, and when extglob is turned off, it's apparently illegal syntax.

-bash: syntax error near unexpected token `crontab'

Your problem is that you are using the sudo command in front of it. Remove it, and it will work just fine.

syntax error near unexpected token `(' error with process substitution

Try using sudo:

  • sudo sort test.tsv > text1.tsv
  • sudo sort test2.tsv > text2.tsv
  • sudo comm -13 text1.tsv text2.tsv

syntax error near unexpected token `'

You get the error because process substitution (the <(some command) part) is not a standard feature (defined in POSIX) in sh, which means it may work on some OS but may not in others or in the same OS with different configuration.

You clarified that you have #!/bin/bash at the top of your script, but I guess you still run the script via sh foo.sh, as such, #!/bin/bash will be ignored and the script is interpreted by sh.

I assume your default shell is bash (run echo $SHELL), so all problems are gone if you paste the script in terminal and execute.

==== UPDATE ====

Possible solution if my assumption is correct:

Leave #!/bin/bash as it is, make your script an executable by chmod +x foo.sh. Then run it directly by ./foo.sh



Related Topics



Leave a reply



Submit