我有一个脚本'myscript',输出如下:

abc
def
ghi

在另一个脚本中,我调用:

declare RESULT=$(./myscript)

$RESULT获取值

abc def ghi

是否有一种方法来存储结果与换行符,或与'\n'字符,所以我可以输出它与'echo -e'?


当前回答

在尝试了这里的大多数解决方案之后,我发现最简单的方法是使用临时文件。我不确定您想对多行输出做什么,但您可以使用read逐行处理它。你唯一不能做的就是把它们都放到同一个变量里,但对于大多数实际目的来说,这个更容易处理。

./myscript.sh > /tmp/foo
while read line ; do 
    echo 'whatever you want to do with $line'
done < /tmp/foo

快速hack使它做请求的动作:

result=""
./myscript.sh > /tmp/foo
while read line ; do
  result="$result$line\n"
done < /tmp/foo
echo -e $result

注意这增加了额外的一行。如果你在做它,你可以编码它,我只是太懒了。


编辑:虽然这种情况下工作得很好,读这篇文章的人应该意识到,你可以很容易地在While循环中压缩你的stdin,从而给你一个脚本,将运行一行,清除stdin,并退出。我认为ssh可以做到?我最近才看到它,其他代码示例在这里:https://unix.stackexchange.com/questions/24260/reading-lines-from-a-file-with-bash-for-vs-while

再来一次!这一次使用不同的文件句柄(stdin, stdout, stderr为0-2,所以我们可以在bash中使用&3或更高)。

result=""
./test>/tmp/foo
while read line  <&3; do
    result="$result$line\n"
done 3</tmp/foo
echo -e $result

您也可以使用mktemp,但这只是一个快速的代码示例。mktemp的用法如下:

filenamevar=`mktemp /tmp/tempXXXXXX`
./test > $filenamevar

然后像使用文件的实际名称一样使用$filenamevar。可能不需要在这里解释,但有人在评论中抱怨。

其他回答

这个怎么样,它将每一行读入一个变量,然后可以使用! 假设myscript输出被重定向到一个名为myscript_output的文件

awk '{while ( (getline var < "myscript_output") >0){print var;} close ("myscript_output");}'

如果你对特定的行感兴趣,使用result-array:

declare RESULT=($(./myscript))  # (..) = array
echo "First line: ${RESULT[0]}"
echo "Second line: ${RESULT[1]}"
echo "N-th line: ${RESULT[N]}"

在尝试了这里的大多数解决方案之后,我发现最简单的方法是使用临时文件。我不确定您想对多行输出做什么,但您可以使用read逐行处理它。你唯一不能做的就是把它们都放到同一个变量里,但对于大多数实际目的来说,这个更容易处理。

./myscript.sh > /tmp/foo
while read line ; do 
    echo 'whatever you want to do with $line'
done < /tmp/foo

快速hack使它做请求的动作:

result=""
./myscript.sh > /tmp/foo
while read line ; do
  result="$result$line\n"
done < /tmp/foo
echo -e $result

注意这增加了额外的一行。如果你在做它,你可以编码它,我只是太懒了。


编辑:虽然这种情况下工作得很好,读这篇文章的人应该意识到,你可以很容易地在While循环中压缩你的stdin,从而给你一个脚本,将运行一行,清除stdin,并退出。我认为ssh可以做到?我最近才看到它,其他代码示例在这里:https://unix.stackexchange.com/questions/24260/reading-lines-from-a-file-with-bash-for-vs-while

再来一次!这一次使用不同的文件句柄(stdin, stdout, stderr为0-2,所以我们可以在bash中使用&3或更高)。

result=""
./test>/tmp/foo
while read line  <&3; do
    result="$result$line\n"
done 3</tmp/foo
echo -e $result

您也可以使用mktemp,但这只是一个快速的代码示例。mktemp的用法如下:

filenamevar=`mktemp /tmp/tempXXXXXX`
./test > $filenamevar

然后像使用文件的实际名称一样使用$filenamevar。可能不需要在这里解释,但有人在评论中抱怨。

实际上,RESULT包含了您想要演示的内容:

echo "$RESULT"

你所展示的就是你所得到的:

echo $RESULT

As noted in the comments, the difference is that (1) the double-quoted version of the variable (echo "$RESULT") preserves internal spacing of the value exactly as it is represented in the variable — newlines, tabs, multiple blanks and all — whereas (2) the unquoted version (echo $RESULT) replaces each sequence of one or more blanks, tabs and newlines with a single space. Thus (1) preserves the shape of the input variable, whereas (2) creates a potentially very long single line of output with 'words' separated by single spaces (where a 'word' is a sequence of non-whitespace characters; there needn't be any alphanumerics in any of the words).

这样做的另一个缺陷是命令替换- $()-删除尾随换行符。可能并不总是重要的,但如果你真的想保留输出的内容,你必须使用另一行和一些引用:

RESULTX="$(./myscript; echo x)"
RESULT="${RESULTX%x}"

如果您想处理所有可能的文件名(以避免在错误的文件上操作等未定义的行为),这一点尤其重要。