landon.1111
landon.1111

Reputation: 1

Unexpected token

I am somewhat new to the Linux system so I would appreciate any help here. Maybe I am overlooking something easy. I am for looping through some variables, and I keep getting this error when executing

 line 8: syntax error near unexpected token `20_0_200-75-20_0_200.Ni10Nj11'
 line 8: `20_0_200-75-20_0_200.Ni10Nj11 20_0_200-80-20_0_200.Ni00Nj10 20_0_200-80-20_0_200.Ni11Nj01'

Here is the loop that is getting stuck.

#!/bin/sh
#
#
#
for s1 in new new1
do
for s2 in 20_0_200-75-20_0_200.Ni10Nj10 20_0_200-80-20_0_200.Ni00Nj00 20_0_200-80-20_0_200.Ni10Nj11
20_0_200-75-20_0_200.Ni10Nj11 20_0_200-80-20_0_200.Ni00Nj10 20_0_200-80-20_0_200.Ni11Nj01
20_0_200-75-20_0_200.Ni11Nj10 20_0_200-80-20_0_200.Ni10Nj10
do

I've used similar files like this, and they have all worked fine. (As well as changing the mode of the file).

Upvotes: 0

Views: 78

Answers (1)

markp-fuso
markp-fuso

Reputation: 34124

As presented here the for s2 line is broken across 3 lines leaving the string 20_0_200-75-20_0_200.Ni10Nj11 (from the error message) sitting on a line all by itself and since the parser is looking for a do (following the for s2 ... line) you get the error message re: unexpected token (ie, 20_0_200-75-20_0_200.Ni10Nj11 != do).

A couple options ...

Add a continuation character (\) on the end of the first 2 lines, eg:

for s2 in 20_0_200-75-20_0_200.Ni10Nj10 20_0_200-80-20_0_200.Ni00Nj00 20_0_200-80-20_0_200.Ni10Nj11 \
20_0_200-75-20_0_200.Ni10Nj11 20_0_200-80-20_0_200.Ni00Nj10 20_0_200-80-20_0_200.Ni11Nj01 \
20_0_200-75-20_0_200.Ni11Nj10 20_0_200-80-20_0_200.Ni10Nj10
do

Or you can pull all of those strings/names up into the main line, eg:

for s2 in 20_0_200-75-20_0_200.Ni10Nj10 20_0_200-80-20_0_200.Ni00Nj00 20_0_200-80-20_0_200.Ni10Nj11 20_0_200-75-20_0_200.Ni10Nj11 20_0_200-80-20_0_200.Ni00Nj10 20_0_200-80-20_0_200.Ni11Nj01 20_0_200-75-20_0_200.Ni11Nj10 20_0_200-80-20_0_200.Ni10Nj10
do

That should get you past the current issue. If you run into any other issues and have problems resolving consider cutting-n-pasting your code (along with shebang) into shellcheck.net; this does a pretty good job of picking up on common coding/syntax issues.


Another possible issue you'll want to review ... you've tagged the question bash but your shebang (/bin/sh) suggests you may not be using bash (ie, sh is not the same as bash); while it's possible your system has redefined /bin/sh as a symlink to, or copy of, bash that's not very common; you'll want to verify which shell you're using as this will dictate the syntax of some commands in future code you write.

Upvotes: 1

Related Questions