Read command acting erratically


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read command acting erratically
# 1  
Old 07-23-2014
Read command acting erratically

I have been trying to use read in a script with issues so I tried some things on the command line.

Code:
$ echo "testing 123" | read x ; echo $x

and
Code:
$ echo "testing 123" | read -r x ; echo $x

are only producing any output after being invoked the first time after rebooting the machine. I also got into some state where echo was producing "unbound variable" errors.

Can anyone point me in the right direction?

Mike

PS. This has something to do with subshells but I'm not exactly clear what is going on.

Last edited by Michael Stora; 07-23-2014 at 01:48 PM..
# 2  
Old 07-23-2014
Consider what happens when you do a |

Code:
echo a b c | some program

If "some program" were cat, would it change any memory or variables in your current shell? Of course not, it doesn't have access to them -- it's a separate process. All commands run after a pipe, including shell built-ins, have to run in a new process.

So read is run in a brand-new, independent shell, which happily sets the variable x in that shell -- then immediately dies, leaving the original shell unchanged.

Try this:

Code:
echo A | ( read X ; echo $X )

Here it will work because the read and the echo are grouped in the same shell.

ksh does pipes in the opposite order so "echo | read" will actually work -- but this isn't something you can count on unless you know you have KSH.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-23-2014
Thanks. I had no idea built-ins also did this!

Mike
# 4  
Old 07-23-2014
It has to, to avoid deadlocks. Which runs first, the echo or the read? Will either of them hang, waiting for the other? Make them independent and it doesn't matter.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 07-23-2014
read is a little unpredictable
Code:
$ var1=
$ var2=
$ var3=

$ echo "a:b:c" | { IFS=":"; read var1 var2 var3 ; echo "var1 is: ""$var1"; echo "var2 is: ""$var2"; echo "var3 is: ""$var3"; }
var1 is: a
var2 is: b
var3 is: c

$ var1=
$ var2=
$ var3=

$ IFS=":"; read var1 var2 var3 <<<$(echo "a:b:c"); echo "var1 is: ""$var1"; echo "var2 is: ""$var2"; echo "var3 is: ""$var3"
var1 is: a b c
var2 is:
var3 is:

Mike
# 6  
Old 07-23-2014
That is not because of read, but because the right hand side of the pipe is executed in a subshell in bash and some other bourne type shells. It works in ksh though...

--
Note that IFS=":"; read ..... permanently changes IFS to a colon. To use IFS local to the read command (in bash / ksh93 / zsh ) you can use:

Code:
IFS=":" read var1 var2 var3 <<< "a:b:c"

After execution of the command the IFS remains unchanged..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 07-23-2014
I just figured it out. You need to create a temporary FIFO which < will treat as a file.

Code:
$ IFS=":" read var1 var2 var3 < <(echo "a:b:c"); echo "var1 is: ""$var1"; echo "var2 is: ""$var2"; echo "var3 is: ""$var3"
var1 is: a
var2 is: b
var3 is: c

Passed this way rather than on a pipe, read does not create a new subshell.

Thanks for the temporary IFS tip. Are there other commands you can combine without ; ?

Mike

Last edited by Michael Stora; 07-23-2014 at 02:53 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

2. UNIX for Dummies Questions & Answers

Acting on results from a grep command

Hi, I am currently reading a tar file and searching for a particular word using grep e.g. Plane. At the moment, if a sentence is found with the word "Plane" the sentence itself is piped to another file. Here is the code i am using; for jar in 'cat jar_file.tar'; do tar -tvf... (3 Replies)
Discussion started by: crunchie
3 Replies

3. IP Networking

DHCP server also acting as relay

Hi. I want to use the DHCP server that comes with vxWorks 6.4. The DHCP server implementation has a table that contains addresses of DHCP servers that will receive a copy of all the client requests that the local server gets, thus the server acts as a dhcp relay at the same time. Can anyone... (4 Replies)
Discussion started by: tomwolf
4 Replies

4. UNIX and Linux Applications

Firefox35 displays pngs erratically

I have been using firefox3.5 now for some months and noticed that some images, notably in the png format, do not display correctly: the images are not displayed at all or display in part whereby the rest of the image shows a black rectangle. Does anybody else suffer from this problem? Desktop:... (0 Replies)
Discussion started by: figaro
0 Replies

5. Shell Programming and Scripting

script acting weird..

Hi Guys, I have this script which is being called from another script, sh +x SCRIPTNAME. Now this script is failing saying the source file is missing. But i was able to see the source file was present. It was renamed and but somehow the source file is removed. There is no remove command in the... (1 Reply)
Discussion started by: mac4rfree
1 Replies

6. Shell Programming and Scripting

bash-function with array acting bizarre, bug?

Hello, basically what this script is supposed to do is showing a list of hosts that is given a number, that you will be able to choose from a list. A check is made to verify that the chosen number is within the array and this is where things go bad and I don't know why, bizarre. I've spent... (5 Replies)
Discussion started by: gand
5 Replies

7. Shell Programming and Scripting

Array in loop is acting up

Hello! I have a question about loops and arrays. I'm trying to go through this: for aa in 01 02 03 OrigNum=$(grep ${Orig} Ba3In2F12.prepos | wc -l) OrigNum=$((${OrigNum} - 1)) echo ${OrigNum} etc It gets stuck on the second line. The error reads: ./asdf: line 30:... (5 Replies)
Discussion started by: RisingSun
5 Replies

8. Solaris

Passwd Changing Acting Strange

Hello. I'm getting very odd behavior when I try to change a user's password in Solaris 8. The shell, where it used to ask for a new passwd and a confirmation, now asks FOUR times, with two success message. This is what happens every time: # passwd myusername New Password: xxxxxxxx New... (2 Replies)
Discussion started by: rockusa
2 Replies

9. Shell Programming and Scripting

Why for loop is acting weird

Hey i have a small script in which i check if a file with that pattern exists or not. If present then i go ahead with further processing. In the present situation i have only one file with that name and for loop is reading twice. Here is the script. And the output of debug mode. Please help.... (5 Replies)
Discussion started by: dsravan
5 Replies

10. UNIX for Advanced & Expert Users

ftp application behaving erratically

Hi, I am working on a custom made FTP application. The application is behaving erratically for the "ls" command. Wild card character passed to the "ls" command (like "ls *temp") is giving inconsistent results. On debuggin I have found that the "ls" command is implemented as shown below in the... (7 Replies)
Discussion started by: diganta
7 Replies
Login or Register to Ask a Question