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; 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. |
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???; Code:
for file in $location$original??? ; do |
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 |
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.
|
To solve the 'cannot find directory' error, put a line saying
Code:
echo $location$original My guess is you're looking at /path/tofile rather than /path/to/file (ie. backslash missing between location and original :) ) |
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:
|
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 Code:
#!/bin/bash 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/ |
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. |