Simple for loop question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple for loop question
# 1  
Old 07-23-2008
MySQL 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 04:42 PM..
# 2  
Old 07-23-2008
Code:
while read i 
do 
   echo "$i"
done <  $server

do not use a for loop, you are reading multiple lines from the file.
# 3  
Old 07-23-2008
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  
Old 07-23-2008
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 04:47 PM.. Reason: added sample code
# 5  
Old 07-23-2008
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  
Old 07-23-2008
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  
Old 07-23-2008
Code:
for (( j = 1 ; j <= 3; j++ )) 
do
echo -n $j
done

That works for me on CentOs. I get 123
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

2. Shell Programming and Scripting

Simple loop using for

Dear experts, I am writing a bash script. At some point of the program I need to have 'for' loop. For simplicity I tried with some other simple code. The format of the loop is given below. k=51 m=55 for j in {$k..$m};do w=$(($j+2)) z=$(($j+9)) echo "$w, $z" done But my... (4 Replies)
Discussion started by: vjramana
4 Replies

3. Shell Programming and Scripting

Simple while read line loop question

#!/bin/bash count=1 while read line do if (($count > 4)); then awk -v var1="$count" '{printf "%3s%8s%11s%11s%11s\n",var1,$2,$3,$4,$5}' else echo $line fi count=$((count+1)) done < posre_sub.itp > test cat test INPUT: ; position restraints for... (3 Replies)
Discussion started by: origamisven
3 Replies

4. Shell Programming and Scripting

really simple for loop question

apologies for the basicness of this question, but within a for loop how can i "list" my values as opposed to adding them to the same line for example if i use this it works fine for n in peter paul david do echo "my name is $n" done but i wanted to list my items ( i have quite a... (2 Replies)
Discussion started by: rethink
2 Replies

5. Shell Programming and Scripting

Simple using For loop

Hi expert, I'm using csh Code: #!/bin/csh set x = 0 set number = `awk '{array=$0} END {print array;}'` i want to use for loop to store data to $number repeatly untill x = 23 How to use c shell for loop? (2 Replies)
Discussion started by: vincyoxy
2 Replies

6. Shell Programming and Scripting

A simple (?) loop

I have what I believe is a simple programming question. I have a text file that looks like: mol 1 G:\stereo01.hin block text ... ... ... endmol 1 However, I would like a file that repeats this entire block of text several times over. The lines of text in the middle remain the same for each... (2 Replies)
Discussion started by: red baron
2 Replies

7. Shell Programming and Scripting

simple while loop

i have a script called file2 #!/bin/ksh i=0 while do echo $i >> result.txt i=`expr $i + 1` done echo "***********************" >> result ------------------------------------------------------------------- (10 Replies)
Discussion started by: ali560045
10 Replies

8. Shell Programming and Scripting

Simple while loop question

I have written a script that countsdown from 20 to 1 and has a sleep in between each count but I want to make it sleep for half a second but I get errors whenever I change the sleep from 1 second to half a second any ideas? I am using Sun OS 5.9 heres what I've got: X=0 while do echo... (3 Replies)
Discussion started by: Brokeback
3 Replies

9. Shell Programming and Scripting

Simple script loop question

Hey all. Thanks in advance for any help you can give, hopefully this is an easy one. I want to create a loop to run a simple performance monitor like vmstat and record it to a file, but have very limited scripting skills (obviously). Starting with this... date >> /var/log/perfmon.log vmstat... (2 Replies)
Discussion started by: mattlock73
2 Replies

10. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies
Login or Register to Ask a Question