generating the variable list for WHILE READ statement at runtime


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting generating the variable list for WHILE READ statement at runtime
# 8  
Old 09-09-2010
In ksh the syntax is:
Code:
while read -A A

It seems to me your original request was space separated, not : separated. Then you can leave out the IFS=":" statement.
If you need the : as field separator a better alternative is to use:
Code:
while IFS=: read -a A

So that IFS is not changed globally and you do not have to set it back to the original value.

A solution that works in both ksh and bash is to use:
Code:
while read line
do 
  A=( $line )
  for i in "${A[@]}"
  do
    printf "$i "
  done
  echo
done < infile

# 9  
Old 09-09-2010
Dear Gopal,
I tried below at my end seems to be working.. Smilie

Inputs:

$ cat source.txt
aaaa="$col1,$col2,$col3,$col4"

$ var_file_name="1 2 3 4"
$ echo ${var_file_name}
1 2 3 4

$ list="col1 col2 col3 col4"
$ echo $list
col1 col2 col3 col4

Code:

$ echo ${var_file_name} | while read `echo $list`
> do
> . source.txt
> echo $aaaa
> done
1,2,3,4


Last line is the output. (1,2,3,4)

I hope thats what you are looking for.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Runtime variable extraction

Hi There, var1=value1 var2=value2 var3=value3 for (( i=1; i<4; i++ )) do temp=var$i echo "$temp" done OUTPUT var1 var2 var3 I want the output to be value1 value2 (5 Replies)
Discussion started by: jagpreetc
5 Replies

2. Shell Programming and Scripting

Evaluate Variable At Runtime

Hi, I am trying to set a variable that has time the format desired. And my intention is to echo variable (instead of actual date command) everytime I like to echo date. Please take a look at below code. $NOW='' echo $NOW After 5 minutes $echo $NOW Issue here is , I am not... (2 Replies)
Discussion started by: vinay4889
2 Replies

3. Shell Programming and Scripting

FORTRAN read statement

Hi, I've been having trouble figuring out what the following read statement is doing. DO I = 1, natom k = 3 * (i - 1) + 1 READ(8,*) AtNum(I), (X_2(J),J=k,k+2) END DO The part I don't understand is: (X_2(J),J=k,k+2) the file it is reading is simply a set of... (8 Replies)
Discussion started by: butson
8 Replies

4. Shell Programming and Scripting

Read statement not working

hello guys, i am having the below piece of code error () { echo"Press y /n" read ans case $ans in y) main;; n) exit esac } In the abve code, read statement is not working i.e not waiting for user to enter input. ,i tested exit status its 1. could anyone help me to do this ... (11 Replies)
Discussion started by: mohanalakshmi
11 Replies

5. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

6. Shell Programming and Scripting

using read to enter the input at runtime

Hi I am stucked in the below script .I want to input with yes/no from the user and then execute the code inside if but it is not working .I just need the logic as where I am wrong so that i can use the same in my work . then echo "Hi All" fi ]. Please suugest . (4 Replies)
Discussion started by: mani_isha
4 Replies

7. Shell Programming and Scripting

Read SQL statement in Script

Hi Guys.. need some urgent help... I am stuck in something badly I need to write a script which would read a sql statement (which might be a join/inner join/select/sub select etc. ) I need to read that sql statement ... and in the output I want all the table names and columns (doesn't... (4 Replies)
Discussion started by: freakygs
4 Replies

8. Shell Programming and Scripting

Read from user inside a while statement

Hi there I want to ask to a user something about each line of a file. This is my code: #cat test.txt first line second line third line #cat test.sh #!/bin/ksh read_write () { echo "Do you want to print the line (YES/NO)?\n" read TEST case ${TEST} in YES) return 0;; ... (3 Replies)
Discussion started by: tirkha
3 Replies

9. Shell Programming and Scripting

Read statement not working in a script

I have a script consisting of certain functions whose input is a file at same location. In that file i have written the name of anothe file at same location. The third file contains a word which act as a function in the first script.Let me give an example i have a scrip file say 1.sh in which i am... (7 Replies)
Discussion started by: sumitdua
7 Replies

10. Shell Programming and Scripting

Generating a list of choices in a menu

Hello all, I have the below script and I'm a little stuck on the best way to continue. Essentially I'm creating a text file (systems.txt) with a list of servers in it by hostname. Then I would like to echo a menu with each hostname and a number to use to pick it from the list. It's somehow... (7 Replies)
Discussion started by: sysera
7 Replies
Login or Register to Ask a Question