LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-19-2011, 04:03 PM   #1
coach5779
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Rep: Reputation: 3
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.
 
Old 01-19-2011, 04:19 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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!
 
1 members found this post helpful.
Old 01-19-2011, 04:22 PM   #3
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
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.
 
Old 01-19-2011, 04:40 PM   #4
coach5779
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 3
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.
 
Old 01-19-2011, 05:06 PM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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 )
 
Old 01-19-2011, 05:20 PM   #6
coach5779
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 3
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;
 
Old 01-20-2011, 12:25 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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/
 
Old 01-20-2011, 09:41 AM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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?
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error near unexpected token `else' dnaqvi Linux - General 5 12-08-2010 03:37 AM
syntax error near unexpected token `then' snakernetb Linux - Server 4 02-16-2010 06:40 PM
syntax error near unexpected token `else' alex0 Programming 7 07-28-2009 08:01 AM
sh: syntax error near unexpected token `(' venkatesh_b Linux - Newbie 1 05-16-2009 05:44 AM
syntax error near unexpected token ` mattyspatty Programming 8 05-07-2006 05:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration