trouble using read to store values in variables from command output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers trouble using read to store values in variables from command output
# 1  
Old 01-05-2009
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.

gist, trying to use output from command:

$ date -u '+%d %h %y' <enter>
05 Jan 09

##want to store 05 Jan 09 to variables DAY MONTH YEAR respectively; also want Jan to be all caps, so tr is used in pipeline##

$ date -u '+%d %h %y' | tr '[:lower:]' '[:upper:]' <enter>
05 JAN 09

##okay, so I got desired format for output, now I want those values stored, so just add a read command statement with the day month year as operands, right??##

$ date -u '+%d %h %y' | tr '[:lower:]' '[:upper:]' | read DAY MONTH YEAR <enter>
$ echo $DAY

$

##var DAY carries empty value--what am I doing wrong or what would be the correct way to achieve desired result of storing 05 JAN 09 output of date/tr commands to those variables??##

thanks in advance
# 2  
Old 01-05-2009
Quote:
$ date -u '+%d %h %y' | tr '[:lower:]' '[:upper:]' | read DAY MONTH YEAR <enter>
$ echo $DAY
I could able to result $DAY as "05" from the above command. I have no issues with it.
# 3  
Old 01-09-2009
update!

by the way, it is a known caveat that you can't use read in a pipeline, because of the way read is called in a subshell. Effectively the values stored to the named variables in read will only be stored in the subshell and not the parent.

I realized why it worked for you. Apparently the $-prompt for bourne and korn shell is the same. I was attempting to do the aforementioned in bourne and I suspected because of what I stated about the parent-child relationship of read when called in a shell, that possibly you were using a shell which compensated for this deficiency. suspected bash at first but obviously korn.

Will try to work it out from here, but thanks for your feedback anyway, bobby. I hope this helps others.
 
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. UNIX for Dummies Questions & Answers

Read in Multiple log files and output selected variables and values to cvs file

I have several problems with my problems: I hope you can help me. 1) the If else statement I am getting an error message. My syntax must be incorrect because the entire statement is throwing an error. For example in filew.log if these items don't exist Memsize, SASFoundation and also if... (0 Replies)
Discussion started by: dellanicholson
0 Replies

3. Shell Programming and Scripting

Store output in variables instead of files

Hi All, I have written a (bash) function which generates multiple files say file1 file2 file3 now I want to reduce the generation of these three files i.e. store the output of three files in variables, and generate the same results, in-order to avoid multiple creation of files how is that... (7 Replies)
Discussion started by: sam@sam
7 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

Shell script to read a file and store in variables

I have a input file like this. Sample.txt 30 | TXDatacenter | TXBackupDC 10 | UKDatacenter | UKBackupDC 0 | NLDatacenter | NLBackupDC ...... ...... ...... I need to get these values in different variables like this. Load1=30 PriCenter1=TXDatacenter... (5 Replies)
Discussion started by: Visha
5 Replies

6. Shell Programming and Scripting

How to read values and store in array?

I am reading a value from a file and want to store the value in a dynamic array as i don't know the number of occurrences of the value in that file. How can i do that and then later fetch that value from array (25 Replies)
Discussion started by: Prachi Gupta
25 Replies

7. UNIX for Dummies Questions & Answers

How to store/read multiple values from a varible

Hi, when I enter 'ps -ef| grep process_name'/'psu | grep process_name', i am getting multiple number of lines output( i mean multiple no of processes).how can i store it one by one and echo it in the same way(one by one). part of script is var1=$(remsh hostname -l username ps -ef|grep... (2 Replies)
Discussion started by: jeanzibbin
2 Replies

8. Shell Programming and Scripting

Store the output values in array

Hi, How to store the values in array from output result, EG: I have the result like this, ps, google, 1.txt, 1 sam, google, 2.txt, 2 These are the four values followed by comma in two sets. I need to store these values set by set. One set contains four values followed by comma. ... (2 Replies)
Discussion started by: KarthikPS
2 Replies

9. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

10. Shell Programming and Scripting

script to store comma separated values in different variables

Hello friends, I need ur help.I want to write a script. The script should read contents from a file namely xyz. e.g xyz abcd,1234,efgh,7854378 dhnsa,dsakjkdl,43432,ZXDsa the script should store comma (,) seperated values in different variables. Once pointer will reach end of line (\n), it should... (1 Reply)
Discussion started by: akhtar.bhat
1 Replies
Login or Register to Ask a Question