Can I use read to read content of a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can I use read to read content of a variable
# 1  
Old 03-25-2009
Can I use read to read content of a variable

Can I use the read command to read the contents of a variable? I'm trying by using the following code and getting nothing back. I'm in a Linux environment.

#!/bin/ksh
IFS=~
VAR1=1~2~3~4
echo $VAR1 | read a b c d
print "$a $b $c $d"
# 2  
Old 03-25-2009
this is working for me!!!
Code:
fnsonlu1-/home/fnsonlu1> cat v
IFS="~"
var="1~2~3~4"
echo "$var"|read -r a b c d
echo $a"\n"$b"\n"$c"\n"$d
fnsonlu1-/home/fnsonlu1> v
1
2
3
4

# 3  
Old 03-25-2009
You need to quote the ~ in the IFS assignment
Code:
IFS="~"

In Bash or Dash you need to group the commands following the pipe.
Code:
#!/bin/sh
IFS="~"
VAR1=1~2~3~4
echo $VAR1 | { read a b c d; echo "$a $b $c $d"; }

# 4  
Old 03-25-2009
mjd_tech : I tried both sets of replies and yours worked...except I could not reference the new variables outside of the brackets. In other words I got the values echoed for a,b,c and d from the echo within the brackets, but when I added the second echo line, I had lost the reference....do I need to do an eval or something to make it global?

IFS="~"
VAR1=1~2~3~4
echo $VAR1 | { read a b c d; echo "$a $b $c $d"; }
echo $a
# 5  
Old 03-25-2009
Code:
#!/bin/ksh
VAR1='1~2~3~4'
set -f
set -- $(IFS='~'; echo $VAR1)
set +f
echo "[$1] [$2] [$3] [$4]"

# 6  
Old 03-25-2009
Quote:
Originally Posted by nmalencia
mjd_tech : I tried both sets of replies and yours worked...except I could not reference the new variables outside of the brackets. In other words I got the values echoed for a,b,c and d from the echo within the brackets, but when I added the second echo line, I had lost the reference....do I need to do an eval or something to make it global?

IFS="~"
VAR1=1~2~3~4
echo $VAR1 | { read a b c d; echo "$a $b $c $d"; }
echo $a
output is:
Code:
1 2 3 4
1 2 3 4

# 7  
Old 03-25-2009
Oops, I forgot to quote ALL the vars...

Ksh version:
Code:
#!/bin/ksh
IFS="~"
VAR1="1~2~3~4"

echo "$VAR1" | read a b c d
echo "$a $b $c $d"
echo "$a"

# Result
1 2 3 4
1

Bash / Dash version (no pipe, no brackets)
Code:
#!/bin/sh
IFS="~"
VAR1="1~2~3~4"

read a b c d <<EOF
$(echo "$VAR1")
EOF
echo "$a $b $c $d"
echo "$a"

# Result
1 2 3 4
1

  • The Bash version works in ksh
  • The ksh version does not work in Bash.
This is because Bash creates a subshell for the last process in a pipeline, in this case the read statement.

Thus, the variables a b c d would only be visible to the subshell. I used brackets in the earlier Bash example to group read and echo commands in the same subshell.

In this example, I used a "here document" to feed the read command, instead of a pipe. Thus, the read command does not happen in a subshell, and the a b c d variables are visible to the rest of the script.

Hopefully this makes sense.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want to read the content of a specific folder

Why does not work a cd in a shell schript file. How do you get to run it? I use these code: #!/bin/sh cd workspace array=($(ls -d */)) echo ${array} But it doesn't change to workspace editby bakunin: please user CODE-tags as required by the rules. Thank you. (12 Replies)
Discussion started by: Linuxmann
12 Replies

2. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

3. Shell Programming and Scripting

Read a text file and print the content..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... ... (7 Replies)
Discussion started by: samupnl
7 Replies

4. Shell Programming and Scripting

How to read file and only output certain content

Hi - I have a file containing data like :- cn=tommy,cn=users,c=uk passwordexpirydate=20100530130623z cn=jane,cn=users,c=uk passwordexpirydate=20100423140734z cn=michael,cn=users,c=uk passwordexpirydate=20100331020044z I want to end up with a file that looks like:-... (6 Replies)
Discussion started by: sniper57
6 Replies

5. Shell Programming and Scripting

read file content

i have one file abhi.txt its contents are home8/mc09ats/UnixCw/backup/file1 home8/mc09ats/file2 i want to read this content of file using while loop.. in this i want to seperate the content as follows path=home8/mc09ats/UnixCw/backup file=file1 echo path echo file can you... (1 Reply)
Discussion started by: AbhijitIT
1 Replies

6. Shell Programming and Scripting

Read a file content with awk and sed

Hello , I have huge file with below content. I need to read the numeric values with in the paranthesis after = sign. Please help me with awk and sed script for it. 11.10.2009 04:02:47 Customer login not found: identifier=(0748502889) prefix=(TEL) serviceCode=(). 11.10.2009 04:03:12... (13 Replies)
Discussion started by: rmv
13 Replies

7. Shell Programming and Scripting

read a file and use the content for mapping

help me pls.. :( i want to read a mapping file. Below is the content of my mapping file. 6221,189,SMSC1,OMC1,WAP1 6223,188,SMSC2,OMC2,WAP2 so when my program running msisdn="622130302310" while not EOF if substring($msisdn,1,4) == "6221" -- > "6221" read from the file then echo... (0 Replies)
Discussion started by: voidmain
0 Replies

8. Shell Programming and Scripting

How to read the content of the particular file from tar.Z without extracting?

Hi All, I want to read the content of the particular file from tar.Z without extracting. aaa.tar.Z contains a file called one.txt, I want to read the content of the one.txt without extracting. Please help me to read the content of it. Regards, Kalai. (12 Replies)
Discussion started by: kalpeer
12 Replies

9. UNIX for Dummies Questions & Answers

How to read the content of the files in unix script

Hi I need help below is my textfile format look like PO Nbr Ln Item Number Description Qty Order Order Date Due Date Status Reply ID Reply Date Reply Qty P304802 1 K0220040 TSX-3225 C 16.367900 MHz 320379 07/01/2008 29/01/2008 REQ OP304802 02/02/2008 190000 P304802 2 K0220040 TSX-3225 C... (0 Replies)
Discussion started by: thila
0 Replies

10. UNIX for Advanced & Expert Users

Cannot read in variable using read on first try

Hi, My problem is : echo Division read vDivision variable1=`cut -c **something****' echo Do you want to proceed ? read ans I cant seem to read in ans on the first try and have to repeatedly enter the return key. If i remove the ` ` statement its ok but i need that line for... (1 Reply)
Discussion started by: normie
1 Replies
Login or Register to Ask a Question