Infodayne Infodayne - 1 month ago 10
Bash Question

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

I have two scripts, A and B.

I need to capture all the terminal output from script A into a file. I know that I can do this:

A |tee myfile.log
, or
A > myfile.log


However, when I do it from script B, it doesn't work (script A runs fine but output not redirected).

Ex. Script A:

echo "I'm script A"


Ex. Script B:

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

./B.sh


Output:

I'm script A

Answer

This seems to work well enough for me. You need ./ because script 'a' is not in your $PATH. The ./ says '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"