Bash Scripting - shell命令输出重定向

时间:2023-01-01 15:43:10

Can someone help explain the following:

有人可以帮助解释以下内容:

If I type:

如果我输入:

a=`ls -l`

Then the output of the ls command is saved in the variable a

然后ls命令的输出保存在变量a中

but if I try:

但如果我尝试:

a=`sh ./somefile`

The result is outputed to the shell (stdout) rather than the variable a

结果输出到shell(stdout)而不是变量a

What I expected was the result operation of the shell trying to execute a scrip 'somefile' to be stored in the variable.

我所期望的是shell尝试执行存储在变量中的脚本'somefile'的结果操作。

Please point out what is wrong with my understanding and a possible way to do this.

请指出我的理解有什么问题,以及可能的方法。

Thanks.

EDIT:

Just to clarify, the script 'somefile' may or may not exist. If it exsists then I want the output of the script to be stored in 'a'. If not, I want the error message "no such file or dir" stored in 'a'

只是为了澄清,脚本'somefile'可能存在也可能不存在。如果它存在,那么我希望脚本的输出存储在'a'中。如果没有,我想在'a'中存储错误消息“no such file or dir”

3 个解决方案

#1


I think because the shell probably attaches itself to /dev/tty but I may be wrong. Why wouldn't you just set execute permissions on the script and use:

我认为因为shell可能会将自己附加到/ dev / tty,但我可能错了。为什么不在脚本上设置执行权限并使用:

a=`./somefile`

If you want to capture stderr and stdout to a, just use:

如果你想将stderr和stdout捕获到a,只需使用:

a=`./somefile 2>&1`

To check file is executable first:

要检查文件是可执行的:

if [[ -x ./somefile ]] ; then
    a=$(./somefile 2>&1)
else
    a="Couldn't find the darned thing."
fi

and you'll notice I'm switching to the $() method instead of backticks. I prefer $() since you can nest them (e.g., "a=$(expr 1 + $(expr 2 + 3))").

你会注意到我正在切换到$()方法而不是反引号。我更喜欢$(),因为你可以嵌套它们(例如,“a = $(expr 1 + $(expr 2 + 3))”)。

#2


You can try the new and improved way of doing command substitution, use $() instead of backticks.

您可以尝试新的和改进的命令替换方法,使用$()而不是反引号。

a=$(sh ./somefile)

If it still doesn't work, check if somefile is not actually stderr'ing.

如果它仍然不起作用,检查某个文件是否实际上不是stderr'ing。

#3


You are correct, the stdout of ./somefile is stored in the variable a. However, I assume somefile outputs to stderr. You can redirect that with 2>&1 directly after ./somefile.

你是对的,。/ somefile的stdout存储在变量a中。但是,我假设某些文件输出到stderr。你可以在./somefile之后直接用2>&1重定向它。

#1


I think because the shell probably attaches itself to /dev/tty but I may be wrong. Why wouldn't you just set execute permissions on the script and use:

我认为因为shell可能会将自己附加到/ dev / tty,但我可能错了。为什么不在脚本上设置执行权限并使用:

a=`./somefile`

If you want to capture stderr and stdout to a, just use:

如果你想将stderr和stdout捕获到a,只需使用:

a=`./somefile 2>&1`

To check file is executable first:

要检查文件是可执行的:

if [[ -x ./somefile ]] ; then
    a=$(./somefile 2>&1)
else
    a="Couldn't find the darned thing."
fi

and you'll notice I'm switching to the $() method instead of backticks. I prefer $() since you can nest them (e.g., "a=$(expr 1 + $(expr 2 + 3))").

你会注意到我正在切换到$()方法而不是反引号。我更喜欢$(),因为你可以嵌套它们(例如,“a = $(expr 1 + $(expr 2 + 3))”)。

#2


You can try the new and improved way of doing command substitution, use $() instead of backticks.

您可以尝试新的和改进的命令替换方法,使用$()而不是反引号。

a=$(sh ./somefile)

If it still doesn't work, check if somefile is not actually stderr'ing.

如果它仍然不起作用,检查某个文件是否实际上不是stderr'ing。

#3


You are correct, the stdout of ./somefile is stored in the variable a. However, I assume somefile outputs to stderr. You can redirect that with 2>&1 directly after ./somefile.

你是对的,。/ somefile的stdout存储在变量a中。但是,我假设某些文件输出到stderr。你可以在./somefile之后直接用2>&1重定向它。