Read command acting erratically


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read command acting erratically
# 8  
Old 07-23-2014
Figure what out? What, exactly, are you trying to do?

We could show you far less convoluted ways if we knew your intent!

The <<< way works in BASH and only BASH, this should work in any shell:

Code:
read A B C <<EOF
string1 string2 string3
EOF

This is called a "here document" and allows you to put variables and text inside it.

Quote:
Thanks for the temporary IFS tip. Are there other commands you can combine without ; ?
This works for any variable. VARIABLE=whatever command temporarily sets a variable for the duration of the line.

Last edited by Corona688; 07-23-2014 at 02:40 PM..
# 9  
Old 07-23-2014
I figured out what you meant by "without ;"

Again though, this isn't something special -- this works for any command. It sets and exports a variable, any variable, only for a single line. You can do

Code:
HTTP_PROXY="proxy:port" wget ...

if you want wget to use a certain proxy, for example.
# 10  
Old 07-23-2014
Quote:
Originally Posted by Corona688
Figure what out? What, exactly, are you trying to do?

We could show you far less convoluted ways if we knew your intent!
I am trying to parse a string like "one:two:three:four" into 4 variables. Should be super simple but I ran into that subshell issue. It needs to be robust so that for "::three:" the first two and 4th variable are null.
I think I have it now.

Is this still convoluted?
Code:
IFS=":" read var1 var2 var3 var4 < <(echo "$string")

As an added benefit it is robust against missing delimiters.

Mike

---------- Post updated at 10:58 AM ---------- Previous update was at 10:51 AM ----------

Quote:
Originally Posted by Corona688
I figured out what you meant by "without ;".
I guess I never knew that variable assignments don't need ; to be on the same line:
Code:
$ a=1 b=2; echo $a; echo $b
1
2

That was a gap in my knowledge.

Mike
# 11  
Old 07-23-2014
Quote:
Originally Posted by Michael Stora
I am trying to parse a string like "one:two:three:four" into 4 variables.
And where do you get this string? Perhaps it could be parsed in the first place instead of later.

Quote:
I guess I never knew that variable assignments don't need ; to be on the same line
Actually, I didn't know that either.

That's something totally different from what I was showing you -- temporary assignments:

Code:
VAR="a" echo asdf
echo $VAR

The second 'echo' doesn't find the value of VAR because it was set for only that line.

(You can't echo $VAR in that line, either, since the line gets evaluated before the value of VAR exists. It's useful for built-in variables, and things you want to export.)
This User Gave Thanks to Corona688 For This Post:
# 12  
Old 07-23-2014
I had never hears of temporary assignments. Thanks again.
# 13  
Old 07-23-2014
Quote:
Originally Posted by Michael Stora
...

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

Mike
It is a common way to set specify a variable local to a command. But of course it only works if a command actually uses the variable

Here is list of environment variables that the read command can use:
read: environment variables

The LANG/LC locale environment variables are used by more utilities...

Last edited by Scrutinizer; 07-23-2014 at 05:51 PM..
# 14  
Old 07-28-2014
since you're using bash I'd say <<< "$string" is a much better option than < <(echo "$string").

Code:
$ IFS=: read -raa <<< "$PATH"
$ declare -p a
declare -a a='([0]="/home/mute/bin" [1]="/usr/local/bin" [2]="/usr/bin" [3]="/bin" [4]="/usr/local/games" [5]="/usr/games")'

This User Gave Thanks to neutronscott For This Post:
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