Read multiple arguments in for loop each time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read multiple arguments in for loop each time
# 1  
Old 01-15-2010
Read multiple arguments in for loop each time

Hi, Guys

I am new to shell programming and just get stuck with one simple question. please kindly help.

According to the tutorial here, we can do something like
Code:
   for NODE in "ABC 10" "EFG 20"
   do
      set -- $NODE
      echo "letter is $1, number is $2"
   done

And the result will be
Code:
   letter is ABC, number is 10
   letter is EFG, number is 20

But if I want to use a variable instead of hard-coded values like below, it doesn't work
Code:
   NODES='"ABC 10" "EFG 20"'
   for NODE in $NODES
   do
      set -- $NODE
      echo "letter is $1, number is $2"
   done

The result becomes
Code:
letter is "ABC, number is
letter is 10", number is
letter is "EFG, number is
letter is 20", number is

Can somebody please explain what is wrong here? and how to correct the error or point me to the right direction?
I am using Bourne Shell.

Many Thanks!

Last edited by pludi; 01-15-2010 at 09:55 AM..
# 2  
Old 01-15-2010
You're using the Bourne shell, or a Bourne-type shell?

For the former you could use something like this:

Code:
set -- "ABC 10" "EFG 20"
   
for NODE in "$@"; do
    set -- $NODE
    echo letter is $1, number is $2
done

Be aware that with the first line you loose the previously defined positional parametrs/arguments.

Some Bourne-type shells support arrays,
this is bash:

Code:
nodes=( "ABC 10" "EFG 20" )

for NODE in "${nodes[@]}"; do
  printf 'letter is %s, number is %s\n' $NODE
done


Anyway, using the shell word splitting mechanism this way is rather limited,
based on your real data, you may need to consider a different approach.

Last edited by radoulov; 01-15-2010 at 10:17 AM..
# 3  
Old 01-15-2010
The problem you are facing is the fact that IFS (Input Field Seperator) is set equal to a space. Therefore everytime a space is encountered in the variable's contents, the FOR loop treats it as a new value. You got around this problem in the first example by placing double quotes around the values you wanted:

Code:
for NODE in "ABC 10" "EFG 20"

The quotes allowed the space to be included as part of the value.

One way around this behavior is to change IFS to something else. Check this out:

Code:
export IFS="|"
NODES="ABC 10|EFG 20"
for NODE in $NODES
   do
      export IFS=" "
      set -- $NODE
      echo "letter is $1, number is $2"
   done

This results in :
Code:
letter is ABC, number is 10
letter is EFG, number is 20

But as you can see, I had to manipulate the IFS setting INSIDE the FOR loop to make it work.

What you need to get a better understanding on is how IFS impacts the parsing behavior of your script.

FYI - Unix in a Nutshell is a great deskside companion to have when shell scripting. Better than man pages...

Hope this helps!
# 4  
Old 01-26-2010
Test response
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to read multiple files at same time through UNIX scripting?

How to read multiple files at simultaneously? (1 Reply)
Discussion started by: Priyanka_M
1 Replies

2. Shell Programming and Scripting

Read multiple lines at a time from file

Hello All, I have a file like below.... dn: cn=user1,ou=org,o=org cn=user1 uid=user1 cn=user2,ou=org,o=org cn=user2 uid=user2 cn=user3,ou=org,o=org cn=user3 cn=user33 uid=user3 cn=user4,ou=org,o=org cn=user4 uid=user4 (6 Replies)
Discussion started by: s_linux
6 Replies

3. Shell Programming and Scripting

Passing multiple run time arguments

the scenario is - If I pass 3 three arguments( run time) , it should list all .txt files from a path to temp file if I pass 2 arguments ( run time) , it should list all .csv files from the same path to another temp file the above scenario should be handled in single code and also I dont know ... (2 Replies)
Discussion started by: Prashanth B
2 Replies

4. Shell Programming and Scripting

Multiple arguments to read

I am developing a script where 3 other scripts are included. This is a graph related script. COMPLETE IDEA: -There are 3 different graph scripts. I would like to create a master graph with all 3 in one. -User chooses the type of graph -User is asked to enter the required auguments (... (7 Replies)
Discussion started by: newkid.7955
7 Replies

5. Shell Programming and Scripting

[SOLVED] UNIX FOR loop to read a variable with multiple values

Hi, I have a variable which stores file names as a result of find command. I need to delete all these files one by one, i.e. by a loop. Can anyone tell me how can it be done? The variable f2d has the file names like these abc.txt bcd.txt fff.txt gef.txt Now I have used a loop as... (12 Replies)
Discussion started by: jhilmil
12 Replies

6. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

7. Shell Programming and Scripting

loop to read multiple username and password

Hello all, I am i am trying to read username password. Bassicaly, i have file called sidlist and it has my database name, username and password.... looks something like this.... db1, user1, pass1 db2, user2, pass2 db3, user3, pass4 but i dont know how to make it work, until i get... (4 Replies)
Discussion started by: abdul.irfan2
4 Replies

8. Shell Programming and Scripting

Read and edit multiple files using a while loop

Hi all, I would like to simply read a file which lists a number of pathnames and files, then search and replace key strings using a few vi commands: :1,$s/search_str/replace_str/g<return> but I am not sure how to automate the <return> of these vis commands when I am putting this in a... (8 Replies)
Discussion started by: cyberfrog
8 Replies

9. Shell Programming and Scripting

How to read and compare multiple fields in a column at the same time

Hi, Currently I am coding up a nasty way of reading file input using *cat* rather than *read*. My text input looks like TextA 100 TextB 110 TextC 120 Currently I am using cat |while read line to read the first column and second column fields. cat foo.txt|while read line do ... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. Programming

read arguments from shell

I have to write a C program using sys call (read, no fread) to read from shell all the parameters, without know how many are them. I tryed in some ways, but I have no success. Any Idea? Can I use read to read from stdin? (1 Reply)
Discussion started by: DNAx86
1 Replies
Login or Register to Ask a Question