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
Need to get pid of a process and have to store the pid in a variable samudha UNIX for Dummies Questions & Answers 7 08-05-2008 12:24 AM
How to store the sql query's output in a variable venkatesh_sasi Shell Programming and Scripting 4 01-17-2008 10:03 PM
store the first line of a file in a variable dineshr85 Shell Programming and Scripting 0 12-06-2007 01:41 AM
To store the output in a variable Sudhakar333 Shell Programming and Scripting 2 07-10-2007 05:45 AM
store awk results in a variable forever_49ers Shell Programming and Scripting 1 09-19-2006 07:51 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 08-17-2007
Registered User
 

Join Date: Aug 2007
Posts: 6
How to Store command O/P to a variable...

Hi all..

I got a problem..
Its easy to redirect o/p to a file.. But Is it possible to redirect the O/P to a variable?

For example:

I've a command in my script:

Code:
string1=cut -d ':' -f2 file.txt
When I do:

Code:
echo $string1
The value is empty... Pls suggest me how to store the value of the cut command in string1 variable..
Reply With Quote
Forum Sponsor
  #2  
Old 08-17-2007
Registered User
 

Join Date: May 2007
Posts: 211
Code:
#!/bin/ksh
string1=$(cut -d ':' -f2 file.txt)
I am not sure if you want to store the entire contents to a single vairable
Reply With Quote
  #3  
Old 08-17-2007
Registered User
 

Join Date: Aug 2007
Posts: 6
Quote:
Originally Posted by lorcan View Post
Code:
#!/bin/ksh
string1=$(cut -d ':' -f2 file.txt)
I am not sure if you want to store the entire contents to a single vairable
I'm getting the following error while execution:

syntax error at line 5: '(' unexpected
Reply With Quote
  #4  
Old 08-17-2007
Registered User
 

Join Date: May 2007
Posts: 211
what is the shell and the OS version
Reply With Quote
  #5  
Old 08-21-2007
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 288
Thumbs up

Quote:
Originally Posted by smartbuddy View Post
Hi all..

I got a problem..
Its easy to redirect o/p to a file.. But Is it possible to redirect the O/P to a variable?

For example:

I've a command in my script:

Code:
string1=cut -d ':' -f2 file.txt
When I do:

Code:
echo $string1
The value is empty... Pls suggest me how to store the value of the cut command in string1 variable..
You can do something like this--

#!/bin/ksh

str1=`cut -d":" -f2 file.txt
echo $str1

This will work i hope.

Thanks
Namish
Reply With Quote
  #6  
Old 08-24-2007
Registered User
 

Join Date: Aug 2007
Posts: 6
Sun OS 5.8 and bash

Quote:
Originally Posted by lorcan View Post
what is the shell and the OS version
My Unix Version is Sun OS 5.8 and working in bash.

Thanks.
Reply With Quote
  #7  
Old 08-24-2007
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 288
Quote:
Originally Posted by smartbuddy View Post
My Unix Version is Sun OS 5.8 and working in bash.

Thanks.

Did you try what i suggested above.That will work for you.

Thanks
Namish
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0