The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 02:58 PM
simple while loop ali560045 Shell Programming and Scripting 10 12-26-2007 10:44 AM
simple for loop ali560045 Shell Programming and Scripting 3 12-17-2007 01:39 AM
Simple while loop question Brokeback Shell Programming and Scripting 3 07-21-2006 09:04 AM
Simple script loop question mattlock73 Shell Programming and Scripting 2 05-17-2006 12:55 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-23-2008
arex876 arex876 is offline
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 03:42 PM..
  #2 (permalink)  
Old 07-23-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,754
Code:
while read i 
do 
   echo "$i"
done <  $server
do not use a for loop, you are reading multiple lines from the file.
  #3 (permalink)  
Old 07-23-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
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
  #4 (permalink)  
Old 07-23-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
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 03:47 PM.. Reason: added sample code
  #5 (permalink)  
Old 07-23-2008
arex876 arex876 is offline
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
  #6 (permalink)  
Old 07-23-2008
arunsoman80 arunsoman80 is offline
Registered User
  
 

Join Date: Jul 2008
Location: New York
Posts: 45
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
  #7 (permalink)  
Old 07-23-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool for loop is fine

Code:
> for (( j = 1 ; j <= 3 ; j++ )) ; do echo $j; done
1
2
3
below added extra echo since the -n suppresses line feeds
Code:
> for (( j = 1 ; j <= 3 ; j++ )) ; do echo -n $j; done ; echo
123
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0