really simple for loop question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting really simple for loop question
# 1  
Old 03-08-2010
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


Code:
for n in peter paul david
do
  echo "my name is $n"
done

but i wanted to list my items ( i have quite a few and dont want to go to an external file if possible)


This does not work

Code:
for n in 
peter 
paul 
david
do
  echo "my name is $n"
done

again sorry for this question, im sure its a really basic solution, but im missing it

I tried putting a backslash after each value but that didnt work

any help would be great
# 2  
Old 03-08-2010
Code:
for n in \
peter \
paul \
david 
do
  echo "my name is $n"
done

try this.
When you have lots of item in a for loop it becomes hard to maintain the code.
You should extract the list to a file:
peter
paul
david
etc.
then do something to put all of those names onto the "command line" for the for loop:
one way to do that:
Code:
 for nm in $( awk '{printf( "%s ")}' file )
 do
   echo "my name is $nm"
 done

# 3  
Old 03-08-2010
thanks for that Smilie
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

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

5. Shell Programming and Scripting

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 ... (7 Replies)
Discussion started by: arex876
7 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