Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.