Register
It is currently Wed Jul 30, 2014 11:22 pm

Command return to variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Fri Mar 05, 2010 11:01 am   
User avatar

Joined: Fri Feb 19, 2010 12:38 pm
Posts: 4
I am trying to write a script for Novell ZenWorks scripting image that will take the return from
zisview -z ComputerName
zisview -z ObjectDN

into
$CN
$DN

my attempts so far have constantly failed. When I used '$?' after each command, it only returns '1' rather than the actual string I need.

any ideas on how to go about this?


Top
 Profile ICQ YIM  
 PostPosted: Sat Mar 06, 2010 12:04 am   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Assuming those commands write something to stdout (part of what shows up in the terminal when you run a command, along with stderr), you can just capture that directly:
Code:
CN=`zisview -z ComputerName`
DN=`zisview -z ObjectDN`

$? gives you the "exit status" of a program, so-to-speak. Typically, 0 is returned if everything goes well, and anything else indicates an error (sometimes specific values are tied to specific types of errors). Since you are getting 1 back, it might not be executing correctly.


Top
 Profile  
 PostPosted: Mon Mar 08, 2010 5:31 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
note that it's considered better to use the newer $(...) syntax rather than backquotes like so;

Code:
CN="$(zisview -z ComputerName)"


Top
 Profile  
 PostPosted: Mon Mar 08, 2010 5:33 am   
User avatar

Joined: Fri Feb 19, 2010 12:38 pm
Posts: 4
thobbs wrote:
Assuming those commands write something to stdout (part of what shows up in the terminal when you run a command, along with stderr), you can just capture that directly:
Code:
CN=`zisview -z ComputerName`
DN=`zisview -z ObjectDN`

$? gives you the "exit status" of a program, so-to-speak. Typically, 0 is returned if everything goes well, and anything else indicates an error (sometimes specific values are tied to specific types of errors). Since you are getting 1 back, it might not be executing correctly.


Tried that this morning with no luck. I used both ` and ' to make sure I wasn't missing anything
Enclosed with ` it returned the commands when I echo'ed it
Enclosed with ' it returned an empty string when echo'ed

FYI: I am using Novell ZENworks as the bash environment

Thanks for the help


Top
 Profile ICQ YIM  
 PostPosted: Mon Mar 08, 2010 5:44 am   
User avatar

Joined: Fri Feb 19, 2010 12:38 pm
Posts: 4
DarthWavy wrote:
note that it's considered better to use the newer $(...) syntax rather than backquotes like so;

Code:
CN="$(zisview -z ComputerName)"


Still echoes an empty string


Top
 Profile ICQ YIM  
 PostPosted: Mon Mar 08, 2010 11:26 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Quote:
Enclosed with ` it returned the commands when I echo'ed it
Enclosed with ' it returned an empty string when echo'ed

CN="$(zisview -z ComputerName)" ... still echoes an empty string

FYI: I am using Novell ZENworks as the bash environment

Well, that's confusing as hell. What's a ZenWorks "scripting image"? What happens when you redirect the output to a file? Like:
Code:
zisview -z Computername &> output.txt

or just
Code:
zisview -z Computername > output.txt

and cat the file:
Code:
cat output.txt


- thobbs


Top
 Profile  
 PostPosted: Tue Mar 09, 2010 6:11 am   
User avatar

Joined: Fri Feb 19, 2010 12:38 pm
Posts: 4
got it

Code:
zisview -z ComputerName > cn.txt
CN=`cat cn.txt`


Thanks everyone


Top
 Profile ICQ YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP