The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
A simple (?) loop red baron Shell Programming and Scripting 2 07-10-2008 11:58 AM
simple while loop ali560045 Shell Programming and Scripting 10 12-26-2007 07:44 AM
simple for loop ali560045 Shell Programming and Scripting 3 12-16-2007 10:39 PM
Simple while loop question Brokeback Shell Programming and Scripting 3 07-21-2006 06:04 AM
Simple script loop question mattlock73 Shell Programming and Scripting 2 05-16-2006 09:55 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Posts: 2
Thumbs up Simple for loop question

Hello

I am a beginner of shell scripting and i am having trouble to do a for loop.

I want a for loop to do stuff 3 times.

i.e.

in visual basic i do this
for (counter = 0; counter < 3; counter++)

on my shell script i have something like this at the moment

server=/apps/scripts/server.txt
servercount=$(wc -l <$server) #It has 3 lines
for i in $servercount
do
Echo $i
done

When i run this shell script, it only shows me 3.. but i want it to show
1
2
3

I also tried

for (( j = 1 ; j <= 3; j++ ))
do
echo $j
done

but i got an error
test2.txt: line 4: syntax error near unexpected token `(('
test2.txt: line 4: `for ((i = 1;i<= 3;i++))'


done

Last edited by arex876; 07-23-2008 at 12:42 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 07-23-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
Code:
while read i 
do 
   echo "$i"
done <  $server
do not use a for loop, you are reading multiple lines from the file.
Reply With Quote
  #3  
Old 07-23-2008
Ikon's Avatar
Registered User
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 350
This should get you started:

Code:
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
your code is basically:
Code:
for i in 3
do
echo "Welcome $i times"
done
Reply With Quote
  #4  
Old 07-23-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 934
Question Are you sure about servercount?

I think your script ran fine because $servercount only contains one line entry.

sample code follows:
Code:
while read zf
  do
  echo $zf
done < server.txt

Last edited by joeyg; 07-23-2008 at 12:47 PM. Reason: added sample code
Reply With Quote
  #5  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Posts: 2
Hey guys

My script only shows the result "3" instead of "1" "2" "3" on each line.
and i know why it does not work because I used the wrong code at first.

I still don't know why the following code is not working for me... i got a syntax error

for (( j = 1 ; j <= 3; j++ ))
do
echo -n $j
done
Reply With Quote
  #6  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Posts: 9
Use While loop.

server=/apps/scripts/server.txt
servercount=`wc -l <$server`
typeset -i i=1
while [ $i -le $servercount ]
do
echo $i
(( i = $i + 1 ))
done
Reply With Quote
  #7  
Old 07-23-2008
Ikon's Avatar
Registered User
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 350
Code:
for (( j = 1 ; j <= 3; j++ )) 
do
echo -n $j
done
That works for me on CentOs. I get 123
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0