niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
Get return value from script
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
Dave Farrance
*nix forums Guru Wannabe


Joined: 21 Feb 2005
Posts: 294

PostPosted: Wed Jul 19, 2006 3:53 pm    Post subject: Re: Get return value from script Reply with quote

"Chris \( Val \)" <chrisval@bigpond.com.au> wrote:

Quote:
How do I store the result of a bash script
in a user defined variable?

How about passing the variable name as a parameter. You can set it to
the error code value within the script, if that's what you want.

function settodate() { eval "$1='$(date)'"; }
settodate foo
echo '$foo is set to:' $foo

--
Dave Farrance
Back to top
Chris ( Val )
*nix forums addict


Joined: 18 Apr 2005
Posts: 99

PostPosted: Wed Jul 19, 2006 2:21 pm    Post subject: Re: Get return value from script Reply with quote

"Jeremiah DeWitt Weiner" <jdw@panix.com> wrote in message
news:e9ldsf$7pu$2@reader2.panix.com...
| "Chris \( Val \)" <chrisval@bigpond.com.au> wrote:
| > I can't work out why I am not storing a return value in my
| > variable ReturnCode.
|
| > ErrorCode=`./SomeScript`
| > echo $ErrorCode
|
| Because backticks give you the _output_ of the command, not the
| return value.

Yes, I realise that now :)

| > however, this works:
|
| > ./SomeScript
| > ErrorCode=$?
| > echo "$ErrorCode"
|
| > How do I store the result of a bash script
| > in a user defined variable?
|
| Eh? It looks to me like you just did! But the term "result" is
| ambiguous. As Chris said, do you want the standard output, or the
| return code? Or are you just confused by the fact that there's no
| syntax like backticks for capturing the exit status? Using $? would be
| the way to do it.

Yes, I wanted the return code, sorry, not the stdout(put).

I see now that $? is the way, I was just looking to get that
value directly via a different method :)

I'm coming from a C++ background, and just getting a little
confused, but I think I am starting to see some light at the
end of the tunnel.

Cheers,
Chris Val
Back to top
Chris ( Val )
*nix forums addict


Joined: 18 Apr 2005
Posts: 99

PostPosted: Wed Jul 19, 2006 2:16 pm    Post subject: Re: Get return value from script Reply with quote

"Bruce Barnett" <spamhater113+U060719073412@grymoire.com> wrote in message
news:yeku05domjk.fsf@grymoire.com...
| "Chris \( Val \)" <chrisval@bigpond.com.au> writes:
|
|
| > #!/bin/bash
| >
| > ErrorCode=`./SomeScript`
| > echo $ErrorCode
| >
| > I have also tried:
| > echo "$ErrorCode"
| >
| > ...without luck (a blank line is echoed
| > back to the screen),
|
|
| That's because your script did not generate any output.
|
| >however, this works:
| >
| > ./SomeScript
| > ErrorCode=$?
| > echo "$ErrorCode"
| >
| > How do I store the result of a bash script
| > in a user defined variable?
|
| There are two direct ways to get results from a script
| Standard output
| Error Code
| There are also indirect ways, such as creating files, using aliases, source the output, etc.
|
|
| > The script "SomeScript" is very simple:
| >
| > #!/bin/bash
| > exit 100
|
|
| Change this to
|
| #!/bin/bash
| echo "My output"
| exit 100
|
| Then get the results using
|
| StandardOutput=`./SomeScript`
| ErrorCode=$?
| echo "ErrorCode=$ErrorCode, StandardOutput=$StandardOutput"

Thanks Bruce.

I think I see what you mean now...

For example, in the following:
ErorCode=`./SomeScript`

I was actually expecting get the value of 100 in
the $ErorCode variable, rather than the output
echoed back from the called script :)

Thanks for your help.

Cheers,
Chris Val
Back to top
Jeremiah DeWitt Weiner
*nix forums Guru Wannabe


Joined: 08 Mar 2005
Posts: 140

PostPosted: Wed Jul 19, 2006 1:59 pm    Post subject: Re: Get return value from script Reply with quote

"Chris \( Val \)" <chrisval@bigpond.com.au> wrote:
Quote:
I can't work out why I am not storing a return value in my
variable ReturnCode.

ErrorCode=`./SomeScript`
echo $ErrorCode

Because backticks give you the _output_ of the command, not the
return value.


Quote:
however, this works:

./SomeScript
ErrorCode=$?
echo "$ErrorCode"

How do I store the result of a bash script
in a user defined variable?

Eh? It looks to me like you just did! But the term "result" is
ambiguous. As Chris said, do you want the standard output, or the
return code? Or are you just confused by the fact that there's no
syntax like backticks for capturing the exit status? Using $? would be
the way to do it.


--
Oh to have a lodge in some vast wilderness. Where rumors of oppression
and deceit, of unsuccessful and successful wars may never reach me
anymore.
-- William Cowper
Back to top
Bruce Barnett
*nix forums Guru


Joined: 21 Feb 2005
Posts: 324

PostPosted: Wed Jul 19, 2006 11:34 am    Post subject: Re: Get return value from script Reply with quote

"Chris \( Val \)" <chrisval@bigpond.com.au> writes:


Quote:
#!/bin/bash

ErrorCode=`./SomeScript`
echo $ErrorCode

I have also tried:
echo "$ErrorCode"

...without luck (a blank line is echoed
back to the screen),


That's because your script did not generate any output.

Quote:
however, this works:

./SomeScript
ErrorCode=$?
echo "$ErrorCode"

How do I store the result of a bash script
in a user defined variable?

There are two direct ways to get results from a script
Standard output
Error Code
There are also indirect ways, such as creating files, using aliases, source the output, etc.


Quote:
The script "SomeScript" is very simple:

#!/bin/bash
exit 100


Change this to

#!/bin/bash
echo "My output"
exit 100

Then get the results using

StandardOutput=`./SomeScript`
ErrorCode=$?
echo "ErrorCode=$ErrorCode, StandardOutput=$StandardOutput"


--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
Back to top
Chris ( Val )
*nix forums addict


Joined: 18 Apr 2005
Posts: 99

PostPosted: Wed Jul 19, 2006 11:32 am    Post subject: Re: Get return value from script Reply with quote

"Chris ( Val )" <chrisval@bigpond.com.au> wrote in message
news:Stovg.7613$tE5.2342@news-server.bigpond.net.au...
| Hi all,
|
| I'm new to scripting, and I am using bash on Mandrake 10.1.
|
| I can't work out why I am not storing a return value in my
| variable ReturnCode.
|
| For example:
|
| #!/bin/bash
|
| ErrorCode=`./SomeScript`
| echo $ErrorCode
|
| I have also tried:
| echo "$ErrorCode"
|
| ...without luck (a blank line is echoed
| back to the screen), however, this works:
|
| ./SomeScript
| ErrorCode=$?
| echo "$ErrorCode"
|
| How do I store the result of a bash script
| in a user defined variable?
|
| The script "SomeScript" is very simple:
|
| #!/bin/bash
| exit 100
|
| Thanks,
| Chris Val

With a little more searching on google, I'm
not certain that what I want is even possible,
is it?

Cheers,
Chris Val
Back to top
Chris ( Val )
*nix forums addict


Joined: 18 Apr 2005
Posts: 99

PostPosted: Wed Jul 19, 2006 11:12 am    Post subject: Get return value from script Reply with quote

Hi all,

I'm new to scripting, and I am using bash on Mandrake 10.1.

I can't work out why I am not storing a return value in my
variable ReturnCode.

For example:

#!/bin/bash

ErrorCode=`./SomeScript`
echo $ErrorCode

I have also tried:
echo "$ErrorCode"

....without luck (a blank line is echoed
back to the screen), however, this works:

../SomeScript
ErrorCode=$?
echo "$ErrorCode"

How do I store the result of a bash script
in a user defined variable?

The script "SomeScript" is very simple:

#!/bin/bash
exit 100

Thanks,
Chris Val
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Thu Dec 04, 2008 2:12 am | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts does squid 2.6 support setting cache_peer port in redirec... Victor Tsang Squid 0 Fri Jul 21, 2006 8:16 am
No new posts mail script eeb4u@hotmail.com shell 3 Fri Jul 21, 2006 5:50 am
No new posts A simple bash script JPB Suse 2 Fri Jul 21, 2006 2:19 am
No new posts Getting started in PXPerl, i.e how to run a script? Markus Hänchen Perl 5 Thu Jul 20, 2006 2:12 pm
No new posts Match pattern in ksh script lnrntx@gmail.com shell 6 Thu Jul 20, 2006 1:48 am

Remortgages | Gas Suppliers | Stag Weekend Nottingham | Mobile Phones | Home Loan
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 7.6790s ][ Queries: 20 (7.5824s) ][ GZIP on - Debug on ]