read command - using output from command substitution


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers read command - using output from command substitution
# 1  
Old 09-14-2010
read command - using output from command substitution

Hey, guys!

Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. Smilie

So, I wanted to use a command statement similar to the following.

This is kinda taken from another question I posted recently about string manipulation and extracting portions of a string (in order to store to variable, via command substitution).

$ TEMPFILE=`ls -1 *31AUG2010.txt`
yields $TEMPFILE=filetransfers_31AUG2010.txt
$ read DD MM YY<enter>
> `echo ${TEMPFILE:14:2}` `echo ${TEMPFILE:16:3}` `echo ${TEMPFILE:19:4}`<enter>
which yields:
$DD=`echo
$MM=${TEMPFILE:14:2}`
$YY=`echo ${TEMPFILE:16:3}` `echo ${TEMPFILE:19:4}`

As you can see, I had intended for each variable to store the output from each command substitution set, but instead stored the literal values between each space encountered on the following input lines. BOOOOO!!

I don't even know if what I'm attempting to do is possible, I wanted to do this in a for loop, storing a new corresponding value for each variable per command set iteration.

Anybody have any experience with this, or how to get command substitution output to work with the read command?

I know that I could just do the command substitution independently for each variable in the loop, but now I'm just curious to know if it's possible to accomplish in one line using the read command.

This is just for my curiosity, but I'd appreciate if somebody could educate me on this matter.

Last edited by ProGrammar; 09-14-2010 at 06:34 PM.. Reason: elaboration, clarification
# 2  
Old 09-15-2010
Your read command is backwards -
Code:
echo "${TEMPFILE:14:2}  ${TEMPFILE:16:3} ${TEMPFILE:19:4}" | read a b c

IFS sets the record separator for a read, by default it is space(s). This means the first text block gets put in a, second in b, third in the c variable.

You can also create arrays like that:
Code:
#!/bin/ksh
set -A arr ${TEMPFILE:14:2}  ${TEMPFILE:16:3} ${TEMPFILE:19:4}
echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}

bash uses declare -a arrayname value1 value2 ... valuen
plus you can assign values directly to each element of an array.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 09-15-2010
I was reading that because each segment of a pipeline is handled inna child shell of the parent, that setting multiple variables using output from preceding segments of a pipeline into a read command statement only stores the variables for the read arguments in the subshell, so the values assigned to the variables are not returned to the parent process.

If I pipe the output of a command into `read variable', why doesn't the output show up in $variable when the read command finishes?

I was however able to successfully get read to take the space-delimted output from a command substitution statement, using a Here-Document.

See below.

This was done using bourne shell.

$ read YY MM DD << EOF
> `echo "${TEMPFILE:14:2} ${TEMPFILE:16:3} ${TEMPFILE:19:4}"`
> EOF
$ echo $YY $MM $DD
10 09 06


I hope this helps anybody else trying to accomplish the same thing. It worked as intended...eventually, after several attempts to play with damm I/O redirection with file descriptors and such. WHY CAN'TCHU JUST DO WHAT I WANT, UNIX?! YOU STUPID ROBOT!

~another rant brought to you by your friendly neighborhod programmar
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read several variables from command output via SSH

Hi Folks, I'm currently trying to read several values into different variables. Actually, what I'm doing works, but I get an error message. My attempts are: read strCPROC strIPROC strAPROC <<<$(ssh -n -T hscroot@$HMC "lshwres -r proc -m $strIDENT --level sys -F \"configurable_sys_proc_units... (11 Replies)
Discussion started by: NKaede
11 Replies

2. Solaris

How to read the output of snoop command?

Hi! I have run the following command: snoop -q -d e1000g0 -o /var/tmp/optima0.txt & them I am trying to read the output of it with snoop -i /var/tmp/optima0.txt, which is giving me this: # snoop -i /var/tmp/optima0.txt | more 1 0.00000 AIOPTSVR -> 10.100.4.72 TCP D=1393 S=22 Push... (8 Replies)
Discussion started by: fretagi
8 Replies

3. Shell Programming and Scripting

Read 2 lines from File, Run Command based off output

Okay, so I have a file containing line after line of three digit numbers. I need a script that does an action based on the last two numbers in this list. So.... To get the last two numbers, I can have the script do tail -2 filename.txt But where I run into trouble is as follows. If... (6 Replies)
Discussion started by: UCCCC
6 Replies

4. UNIX for Advanced & Expert Users

ls output into a read command as a variable

I'm working on a short BASH script on my Ubuntu box that will run powerpoint scripts with MS Powerpoint Viewer 2007 via WINE. I can run the presentation when I run it manually but what i'd like to do is have the script look for the newest file then run it. #! /bin/sh # Start the newest... (2 Replies)
Discussion started by: binary-ninja
2 Replies

5. Shell Programming and Scripting

read line and run a different command according to the output

Hi. I'm trying to write a script that reads a line on a file and runs a different command for a different line output. For example, if it finds the word "Kuku" on the line it sends mail to Kuku@kuku.com. Otherwise, it sends mail to Lulu@lulu.com. TIA. (2 Replies)
Discussion started by: Doojek9
2 Replies

6. Shell Programming and Scripting

how to read tail -F command output in perl

Hi All, How I will read the output of the tail -F command in perl. I have text file with below contains file1.txt 1 2 3 4 $running=1; sub openLog($) { (my $log) = @_; (1 Reply)
Discussion started by: pravin27
1 Replies

7. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

8. Programming

How to read output of a shell command

Hello All, I have a an application written in C and runing on Red Hat Linux. In my code I have written a command that is fired on the linux shell by using system() function call. Now I need to read the output of this command in my c program and assign it to a variable. Can anyone... (1 Reply)
Discussion started by: shamik
1 Replies

9. UNIX for Dummies Questions & Answers

trouble using read to store values in variables from command output

I know there are caveats about using read in pipelines because read is treated by a subshell. I know this but I can't think of any way to accomplish this regardless, I'm still a rookie. I hope somebody will be able to interpret what it is that I'm trying to accomplish and correct me. ... (2 Replies)
Discussion started by: ProGrammar
2 Replies

10. UNIX for Advanced & Expert Users

Read the entire output fired by ps command

Actually I want to display the entire output fired by ps command. My output gets trucated after 80 chars. Thus, i am not able to see the entire command running when i give a ps -eaf .... Does anyone know how do i display the entire output fired by ps command .. (i.e the command along with... (5 Replies)
Discussion started by: vinithepoo
5 Replies
Login or Register to Ask a Question