|
|
|
|
|
|
| Author |
Message |
Dave Farrance *nix forums Guru Wannabe
Joined: 21 Feb 2005
Posts: 294
|
Posted: Wed Jul 19, 2006 3:53 pm Post subject:
Re: Get return value from script
|
|
|
"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
|
Posted: Wed Jul 19, 2006 2:21 pm Post subject:
Re: Get return value from script
|
|
|
"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
|
Posted: Wed Jul 19, 2006 2:16 pm Post subject:
Re: Get return value from script
|
|
|
"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
|
Posted: Wed Jul 19, 2006 1:59 pm Post subject:
Re: Get return value from script
|
|
|
"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
|
Posted: Wed Jul 19, 2006 11:34 am Post subject:
Re: Get return value from script
|
|
|
"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
|
Posted: Wed Jul 19, 2006 11:32 am Post subject:
Re: Get return value from script
|
|
|
"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
|
Posted: Wed Jul 19, 2006 11:12 am Post subject:
Get return value from script
|
|
|
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 |
|
 |
|
|
The time now is Thu Dec 04, 2008 2:12 am | All times are GMT
|
|
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
|
|