LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   for, foreach loops mystifying in bash! (http://www.linuxquestions.org/questions/linux-newbie-8/for-foreach-loops-mystifying-in-bash-699248/)

maryfran 01-22-2009 08:54 PM

for, foreach loops mystifying in bash!
 
I am a bash newbie, but an experienced programmer. I know four or five programming languages well, and have written shell scripts. I am missing something right off the bat. So far today, my first with this scripting language, I have had trouble with the Hello World (!!!!) and the for loop. I find the man bash page, and the internet tutorials were just plain wrong with Hello World

STRING="Hello World"
echo $STRING

did not work

set STRING="Hello World"
echo $STRING
did. Figured that out myself.

But the for (or should it be foreach?) loop has me completely stumped.

country_codes = `ls .`
for country_code in ${country_codes}
do
zip -ruq $ZIP_DIR/by_country/$country_code $country_code;
end

gives me the error messages

for: Command not found.
do: Command not found.


I have tried many variants -- putting in a semicolon on the first line,
putting in `ls .` for instead of the country_codes. I must be missing something really, really, really basic here. Please don't tell me that bash -like python - includes white space in its grammar!

bash man, an internet tutorial on for loops in bash, and this very forum have not been helpful. Each gives a variant of the syntax. None of these variants work!

weibullguy 01-22-2009 09:45 PM

Are you sure you're using a Bash shell and not some other shell (like csh or tcsh)? Your first example works in Bash, but using set won't. Using set to assign variable values is csh/tcsh syntax IIRC. Same with the second example. It works almost as written in Bash except the 'end' needs to be 'done'. It won't work in csh/tcsh IIRC. But I'm not familiar enough with csh/tcsh to be sure.

nflenz 01-22-2009 09:57 PM

Quote:

STRING="Hello World"
echo $STRING

did not work

set STRING="Hello World"
echo $STRING
did. Figured that out myself.

But the for (or should it be foreach?) loop has me completely stumped.
You are not using bash. Use chsh to change your shell.

jschiwal 01-23-2009 01:07 AM

This part won't work either:
country_codes = `ls .`

First, don't have spaces around the = sign.
Second I think you want something like:
country_codes=(`ls`)

This will create an array variable which you access like "${country_code[5]}" where 5 is the index.

An other option is to use the form:
for file in $(ls .)
do
something
done

None of the files can contain spaces. Zip codes don't, but it seems odd to have files named after zipcodes.

Consider a file of zipcodes.
for file in $(cat zipcodes)
do
something
done

Also notice that you end do with done and not end.

The www.tldp.org website has an "Advanced Bash Scripting Guide" which I would highly recommend. It is composed entirely of commented examples. Most people are better are learning by examples than reading a manual. Also you can try them out yourself, print out variables, etc. to study the example code.

In the bash shell, you can enter "help command" for many built in commands such as "for". This can help as a reminder of what you have already learned.

maryfran 01-23-2009 11:43 AM

Thanks to all. My system runs tcsh. I had correctly placed a #!/bin/bash as the first line of the file. BUT I sourced it instead of executing it from the command line. So everything was running under tcsh instead of bash. If I had just kept at it for awhile I would have learned tcsh the hard way as well as bash!

Mary


All times are GMT -5. The time now is 07:01 PM.