Reputation: 1608
I have script which internally calls another shell script. There is a possibility that the second script does not exists. I want to write conditional executions after that. How can I store the message "No such file or directory" inside a variable?
OUTPUT=$(sh ../../bin/tools/myscript.sh)
sh: ../../bin/tools/myscript.sh: No such file or directory
[root@xxxx home]# echo $OUTPUT
The $OUTPUT should also print "No such file or directory" message.
Upvotes: 0
Views: 572
Reputation: 1608
The below code worked for me :
OUTPUT=$((sh ../../bin/tools/myscript.sh) 2>&1)
Upvotes: 0
Reputation: 22225
The message has been printed to stderr, so you have to catch stderr somehow:
OUTPUT=$(sh ../../bin/tools/myscript.sh 2>&1)
or something like
error_file=/tmp/stderr.$$
sh ../../bin/tools/myscript.sh 2>$error_file
OUTPUT=$(cat $error_file)
rm $error_file
depending on your concrete needs. Of course in your case, it would maybe make more sense to test the existence of myscript.sh
, before you try to run it.
Upvotes: 2