LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   syntax error near unexpected token `for file in (https://www.linuxquestions.org/questions/linux-newbie-8/syntax-error-near-unexpected-token-%60for-file-in-857376/)

coach5779 01-19-2011 04:03 PM

syntax error near unexpected token `for file in
 
I have been trying to get the following code to run.

Code:

cp $location$original $location$origcp;

wc -l $location$original;
wc -l $location$origcp;

split -l $rec -a 3 $location$original $location$original;

for file in $location$original???;
do;
wc -l $location$original??? ;
break;
done;


ini=161;
i=161;
for file in $location$original???;
do;
output=$(printf '%X' $i)$DT.$TM.custi;
  mv $file $prefix.$output;
  i=$((i + 1 + 7*($i-$ini==8)));
  ini=$((ini + 16*($i-$ini>8)));

However I am getting the following error:

syntax error near unexpected token `
`for file in $location$original???;

Is there something that I am missing in the script? I have it as a unix file and tried to make sure that I did not have any extra characters. This is really frustrating and I would appreciate any help.

Thanks.

colucix 01-19-2011 04:19 PM

Maybe you're used to C programming, but there is no need for semi-colon at the end of bash statements. The offending one is highlighted in red:
Code:

for file in $location$original???;
do;
  wc -l $location$original??? ;
  break;
done;

Anyway, you can safely remove them all. They are necessary if you have more command on the same line, or when you put for and do on the same line, e.g.
Code:

for file in $location$original??? ; do
  commands
done

Hope this helps!

xeleema 01-19-2011 04:22 PM

Greetingz!

For starters, I would suggest you wrap your variables in curlies ( "{" and "}" ). However, if "$location$original???;" is supposed to be a regular expression, I would say just bite the bullet and use command substitution;

for file in $( /bin/ls $location$original???);

On a side note, you know what would be just awesome? If you mentioned what shell you were attempting this in. :D

coach5779 01-19-2011 04:40 PM

Well I am using bash as my shell. I took the semicolon's out and tried to run the code again but now I am getting an error saying that it cannot find the directory (I am using the $location$original with substitution parameters). The reason for the semicolon's were because I had edited the file in UltraEdit and then saved it as a unix file. when I tried to run it gave the "cannot stat" error and ignored my substitution parameters. When I put the semicolon's in the script it worked down to the "syntax error near unexpected token" error is happening.

Snark1994 01-19-2011 05:06 PM

To solve the 'cannot find directory' error, put a line saying
Code:

echo $location$original
in your script, to see where it's looking, then check if it does actually exist ;)

My guess is you're looking at /path/tofile rather than /path/to/file (ie. backslash missing between location and original :) )

coach5779 01-19-2011 05:20 PM

If I put the semicolon's back in I no longer get the cannot find directory error. Here is the full code that I am using in my most recent try:

Quote:

#!/bin/bash -x

original=ALPHA.jan.R120110117.142731.txt;

origcp=$original.original;

location=/home/testfiles/;

rec=1000001;

DT=20110117;

TM=120000;

prefix=ALPHA.jan;



cp $location$original $location$origcp;



wc -l $location$original;

wc -l $location$origcp;



split -l $rec -a 3 $location$original $location$original;

chmod 775 $location$original???

for file in $location$original???

do

wc -l $location$original???

break

done;





ini=161;

i=161;

for file in $location$original???;

do

output=$(printf '%X' $i)$DT.$TM.txt;

mv $file $prefix.$output

i=$((i + 1 + 7*($i-$ini==8)))

ini=$((ini + 16*($i-$ini>8)))



print $file has been renamed to $prefix.$output

done;

chrism01 01-20-2011 12:25 AM

A few notes:

1. as above, forget the ';'s, they are unnecessary and may cause issues as mentioned.

2. If you've edited on MSWin (ultraedit) and the copied to Linux, you may end up with MS line endings (\r\n) instead of *nix line endings (\n).
Use the dos2unix tool to fix that.

3. Whenever you have a shell var immediately preceded or suffixed by another char or chars , always use the {} chars to separate it out so that the parser can figure out what you meant
Code:

# for
abc$d

# use instead
abc${d}

#similarly

$abc.d

#becomes
${abc}.d

$abc$def

#becomes
${abc}${def}

5. Try this at the top
Code:

#!/bin/bash

set -xv

more verbose; shows you exactly the before & after parser view of the code

6. why the 'break' in the loop; that will jump out of the loop immedaitely after the first item

I rely on these:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Snark1994 01-20-2011 09:41 AM

Everything Chris said is a good idea - however, I think we (or certainly I) will be able to help you a lot better if you tried to tell us what you're trying to do. You seem to be trying to do some mass renaming on files based on how many lines are with them, but I can't work out exactly what's going on... Could you try to explain? :)


All times are GMT -5. The time now is 12:13 PM.