从脚本 B 调用脚本 A,并将脚本 A 的输出写入文件

Call script A from script B, and write output of script A to file

我有两个脚本,A 和 B。

我需要将脚本 A 的所有终端输出捕获到一个文件中。我知道我可以这样做: A |tee myfile.log,或A > myfile.log

但是,当我从脚本 B 执行此操作时,它不起作用(脚本 A 运行良好但输出未重定向)。

例如。脚本 A:

echo "I'm script A"

例如。脚本 B:

echo "Starting script B"
A |tee myfile.log
echo "Finished script B"

./B.sh

输出:

I'm script A

这对我来说似乎很管用。您需要 ./ 因为脚本 'a' 不在您的 $PATH 中。 ./ 表示 'look in the current directory for my script rather than looking at all the directories specified in $PATH'。

#!/bin/bash
echo "Starting script B"
./a.sh | tee myfile.log
echo "Finished script B"