It's my Bash script homework, I am confused about the output of my programs. Suppose I need to use script debugger.sh
to invoke script program.sh
until the second one failed, and capture all the output of program.sh
user@user-machine:~$ cat program.sh
#!/usr/bin/env bash
n=$(( RANDOM % 100 ))
if [[ n -eq 42 ]]; then
echo "Something went wrong"
>&2 echo "The error was using magic numbers"
exit 1
fi
echo "Everything went according to plan"
user@user-machine:~$ cat debugger.sh
#! /bin/bash
touch info.txt
count=0
while [ 0 -eq 0 ]
do
./program.sh > info.txt
if [ $? -eq 1 ]
then
echo "failed at stage $count" > info.txt
break
fi
((count++))
done
When I type command ./debugger.sh in my Bash shell, the result I expect:
user@user-machine:~$ ./debugger.sh
Something went wrong
user@user-machine:~$ cat info.txt
Everything went according to plan
Everything went according to plan
........
The error was using magic numbers
failed at stage....
But the actual result is:
user@user-machine:~$ ./debugger.sh
The error was using magic numbers
user@user-machine:~$ cat info.txt
failed at stage 199
I can't figure out where some output goes. Why "The error was using magic numbers"
appears on my terminal, I think it should be redirected to the info.txt
file. And the "Everything went according to plan"
just vanish. It's so weird, can someone explain? My English is not so good, hope you guys understand. I'm really grateful to you guys.