如何将shell脚本的输出存储到Unix中的变量?

时间:2021-12-30 13:56:42

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}

我有一个shell脚本“script.sh”,当我在unix窗口中执行时,它输出为“success”或“Failed”。现在我想将script.sh的输出存储到unix命令变量中。说$ a = {script.sh的输出}

5 个解决方案

#1


66  

Two simple examples to capture output the pwd command:

捕获输出pwd命令的两个简单示例:

$ b=$(pwd)
$ echo $b
/home/user1

or

要么

$ a=`pwd`
$ echo $a
/home/user1

The first way is preferred. Note that there can't be any spaces after the = for this to work.

第一种方式是首选。请注意,在=之后不能有任何空格。

Example using a short script:

使用短脚本的示例:

#!/bin/bash

echo "hi there"

then:

然后:

$ ./so.sh
hi there
$ a=$(so.sh)
$ echo $a
hi there

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.

通常,更灵活的方法是从命令返回退出值并将其用于进一步处理,尽管有时我们可能只想捕获命令的简单输出。

#2


4  

You should probably re-write the script to return a value rather than output it. Instead of:

您可能应该重新编写脚本以返回值而不是输出它。代替:

a=$( script.sh ) # Now a is a string, either "success" or "Failed"
case "$a" in
   success) echo script succeeded;;
   Failed) echo script failed;;
esac

you would be able to do:

你可以这样做:

if script.sh > /dev/null; then
    echo script succeeded
else
    echo script failed
fi

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 printing success, and exit 1 instead of printing Failed. 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.

如果其他程序不必解析输出,则使用脚本可以更简单。这是一个简单的改变。只需退出0而不是打印成功,然后退出1而不是打印失败。当然,您也可以打印这些值以及以合理的返回值退出,以便包装脚本可以灵活地使用脚本。

#3


3  

Suppose you want to store the result of an echo command

假设您要存储echo命令的结果

echo hello   
x=$(echo hello)  
echo "$x",world!  

output:

输出:

hello  
hello,world!

#4


2  

export a=$(script.sh)

Hope this helps. Note there are no spaces between variable and =. To echo the output

希望这可以帮助。注意变量和=之间没有空格。回应输出

echo $a

#5


0  

You need to start the script with a preceding dot, this will put the exported variables in the current environment.

您需要使用前一个点启动脚本,这会将导出的变量放在当前环境中。

#!/bin/bash
...
export output="SUCCESS"

Then execute it like so

然后像这样执行它

chmod +x /tmp/test.sh
. /tmp/test.sh

When you need the entire output and not just a single value, just put the output in a variable like the other answers indicate

当您需要整个输出而不仅仅是单个值时,只需将输出放在变量中,就像其他答案所示

#1


66  

Two simple examples to capture output the pwd command:

捕获输出pwd命令的两个简单示例:

$ b=$(pwd)
$ echo $b
/home/user1

or

要么

$ a=`pwd`
$ echo $a
/home/user1

The first way is preferred. Note that there can't be any spaces after the = for this to work.

第一种方式是首选。请注意,在=之后不能有任何空格。

Example using a short script:

使用短脚本的示例:

#!/bin/bash

echo "hi there"

then:

然后:

$ ./so.sh
hi there
$ a=$(so.sh)
$ echo $a
hi there

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.

通常,更灵活的方法是从命令返回退出值并将其用于进一步处理,尽管有时我们可能只想捕获命令的简单输出。

#2


4  

You should probably re-write the script to return a value rather than output it. Instead of:

您可能应该重新编写脚本以返回值而不是输出它。代替:

a=$( script.sh ) # Now a is a string, either "success" or "Failed"
case "$a" in
   success) echo script succeeded;;
   Failed) echo script failed;;
esac

you would be able to do:

你可以这样做:

if script.sh > /dev/null; then
    echo script succeeded
else
    echo script failed
fi

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 printing success, and exit 1 instead of printing Failed. 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.

如果其他程序不必解析输出,则使用脚本可以更简单。这是一个简单的改变。只需退出0而不是打印成功,然后退出1而不是打印失败。当然,您也可以打印这些值以及以合理的返回值退出,以便包装脚本可以灵活地使用脚本。

#3


3  

Suppose you want to store the result of an echo command

假设您要存储echo命令的结果

echo hello   
x=$(echo hello)  
echo "$x",world!  

output:

输出:

hello  
hello,world!

#4


2  

export a=$(script.sh)

Hope this helps. Note there are no spaces between variable and =. To echo the output

希望这可以帮助。注意变量和=之间没有空格。回应输出

echo $a

#5


0  

You need to start the script with a preceding dot, this will put the exported variables in the current environment.

您需要使用前一个点启动脚本,这会将导出的变量放在当前环境中。

#!/bin/bash
...
export output="SUCCESS"

Then execute it like so

然后像这样执行它

chmod +x /tmp/test.sh
. /tmp/test.sh

When you need the entire output and not just a single value, just put the output in a variable like the other answers indicate

当您需要整个输出而不仅仅是单个值时,只需将输出放在变量中,就像其他答案所示