i have a shell script "script.sh" which gives output as "success" or "Failed" when i execute in unix window.
Now i want to store the output of script.sh into a unix command variable. say $a = {output of script.sh}
相关问题
- How to get the return code of a shell script in lu
- Invoking Mirth Connect CLI with Powershell script
- Why should we check WIFEXITED after wait in order
- Emacs shell: save commit message
- “command not found” errors in expect script execut
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- Generate disk usage graphs/charts with CLI only to
Suppose you want to store the result of an echo command
output:
You should probably re-write the script to return a value rather than output it. Instead of:
you would be able to do:
It is much simpler for other programs to work with you script if they do not have to parse the output. This is a simple change to make. Just
exit 0
instead of printingsuccess
, andexit 1
instead of printingFailed
. Of course, you can also print those values as well as exiting with a reasonable return value, so that wrapper scripts have flexibility in how they work with the script.Two simple examples to capture output the
pwd
command:or
The first way is preferred. Note that there can't be any spaces after the
=
for this to work.Example using a short script:
then:
In general a more flexible approach would be to return an exit value from the command and use it for further processing, though sometimes we just may want to capture the simple output from a command.
You need to start the script with a preceding dot, this will put the exported variables in the current environment.
Then execute it like so
When you need the entire output and not just a single value, just put the output in a variable like the other answers indicate
Hope this helps. Note there are no spaces between variable and =. To echo the output