nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Shell store or return output of a command in a variable

This is a discussion on Shell store or return output of a command in a variable within the Shell scripting forums, part of the Development/Scripting category; I can store command output using $(cmd) or `cmd`. But how do get stdout+stderr in a variable? Also, some script ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-07-2009, 11:40 AM
chimu's Avatar
Contributors
User
 
Join Date: Mar 2005
OS: Ubuntu
Posts: 79
Thanks: 23
Thanked 4 Times in 3 Posts
Rep Power: 6
chimu is on a distinguished road
Default Shell store or return output of a command in a variable

I can store command output using $(cmd) or `cmd`. But how do get stdout+stderr in a variable? Also, some script has $?
Code:
ret=$?
return $ret
ret stores $? but why?

Last edited by nixcraft; 06-07-2009 at 10:54 AM.
Reply With Quote
  #2 (permalink)  
Old 04-07-2009, 11:46 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
ret stores $? but why?
Exit status of last command:
Code:
cmd1
ret=$?
return $ret
Or
Code:
tar -zcvf /dev/st0 /home && echo "Backup done" || echo "Backup Failed"
Above will take an action based upon command status:

Quote:
But how do get stdout+stderr in a variable?
To copy output from both stdout and stderr use
Code:
output=$(cmd1 2>&1)
OR
Code:
output=`command-name 2>&1`
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 04-07-2009, 11:52 AM
chimu's Avatar
Contributors
User
 
Join Date: Mar 2005
OS: Ubuntu
Posts: 79
Thanks: 23
Thanked 4 Times in 3 Posts
Rep Power: 6
chimu is on a distinguished road
Default

Hmm.. what about just saving stderr and ignoring output (stdout)? I'm not interested in output but just wanted error messages.
Reply With Quote
  #4 (permalink)  
Old 05-07-2009, 07:42 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
Originally Posted by chimu View Post
Hmm.. what about just saving stderr and ignoring output (stdout)? I'm not interested in output but just wanted error messages.
Try to get both stdout and stderr:
Code:
 var=$(cmd1 2>&1)
Just get stderr but send stdout to /dev/null
Code:
var=$(cmd1 2>&1 >/dev/null)
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
The Following User Says Thank You to nixcraft For This Useful Post:
chimu (05-07-2009)
  #5 (permalink)  
Old 05-07-2009, 07:47 PM
chimu's Avatar
Contributors
User
 
Join Date: Mar 2005
OS: Ubuntu
Posts: 79
Thanks: 23
Thanked 4 Times in 3 Posts
Rep Power: 6
chimu is on a distinguished road
Default

Quote:
Originally Posted by nixcraft View Post
Try to get both stdout and stderr:
Code:
 var=$(cmd1 2>&1)
Just get stderr but send stdout to /dev/null
Code:
var=$(cmd1 2>&1 >/dev/null)
worked like a charm!
Reply With Quote
Reply

Tags
bash , csh , exit status , ksh , return value , scripting , shell store output , var=$(cmd1 2>&1 >/dev/null) , var=$(cmd1 2>&1)


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Parse XML file and store data in array in shell scripting Nishanthhampali Shell scripting 6 15-12-2009 07:37 PM
set shell variable uses which file in RHEL5.1?? knvpavan Shell scripting 2 19-05-2009 05:39 AM
save command and command output savook CentOS / RHEL / Fedora 1 08-05-2009 05:33 PM
How to Redirect OutPut of Shell Command to a text file Hadi Shell scripting 1 29-04-2009 07:27 PM
Shell Creates a variable that stores the results of the command blingbling Shell scripting 1 16-03-2009 07:41 PM


All times are GMT +5.5. The time now is 04:45 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38