The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can i read text file and assign its values to variables using shell rosalinda Shell Programming and Scripting 4 08-26-2008 06:20 AM
How to store the values in a file into an array risshanth UNIX for Dummies Questions & Answers 3 01-22-2008 10:34 AM
Read the entire output fired by ps command vinithepoo UNIX for Advanced & Expert Users 5 01-19-2008 09:24 AM
read values from ps -ef into variables in ksh? JamesByars Shell Programming and Scripting 8 01-13-2008 08:12 AM
store output to a file and read from it afadaghi Shell Programming and Scripting 2 10-04-2005 01:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-05-2009
ProGrammar ProGrammar is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 13
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 (permalink)  
Old 01-05-2009
bobbygsk bobbygsk is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 90
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 (permalink)  
Old 01-09-2009
ProGrammar ProGrammar is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 13
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.
Closed Thread

Bookmarks

Tags
read command pipe variable can't

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0