![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
while read i do echo "$i" done < $server |
|
#3
|
||||
|
||||
|
This should get you started:
Code:
for i in 1 2 3 4 5 do echo "Welcome $i times" done Code:
for i in 3 do echo "Welcome $i times" done |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Code:
for (( j = 1 ; j <= 3; j++ )) do echo -n $j done |
||||
| Google The UNIX and Linux Forums |