LinuxQuestions.org
Review your favorite Linux distribution.
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-22-2009, 07:54 PM   #1
maryfran
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
Unhappy 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!
 
Old 01-22-2009, 08:45 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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.
 
Old 01-22-2009, 08:57 PM   #3
nflenz
Member
 
Registered: Feb 2006
Distribution: CRUX 2.4
Posts: 96

Rep: Reputation: 18
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.
 
Old 01-23-2009, 12:07 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 01-23-2009 at 12:09 AM.
 
Old 01-23-2009, 10:43 AM   #5
maryfran
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Smile

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
 
  


Reply

Tags
bash, foreach



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
BASH Shell Scripting -- foreach file in folder??? AC97Conquerer Programming 11 07-06-2011 12:25 AM
Bash Loops UnitedWeFall Programming 8 08-21-2007 06:33 AM
PERL: Help w/ "foreach" loops and building a Crontab entry - Just need some advice bpmee Programming 5 10-13-2006 02:12 PM
bash scripting and loops phoeniks Programming 5 01-24-2005 04:00 PM
foreach loops chunky Linux - General 2 07-02-2004 11:49 AM

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

All times are GMT -5. The time now is 01:47 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