capture the output of a shell script which is invoked from another shell script
I have a script called a.sh
, contents of which are:
//a.sh:
#!/bin/bash
temp=0
while [ "$temp" -ne 500 ]
do
echo `date`
temp=`echo "$temp+1" | bc`
sleep 1
done
----------------------------------
Another script namedb.sh
, contents of which are:
// b.sh:
#!/bin/bash
`a.sh`
exit
----------------------------------
When i execute a.sh
separately, i'm able to see the output.. but, when i execute b.sh
, i'm not able to see the output on the console.. (i tried a few times - to redirect the output of a.sh
- but not being successful).
So, what i need is the redirection, which will enable me to see the output of a.sh
's content开发者_开发知识库s when i execute b.sh
- on the console.
Thanks, Ravi.
`a.sh`
in your b.sh
means take the output of a.sh and use it as a command with arguments. you just have to execute a.sh in b.sh
$A_SH_PATH/a.sh
instead of
`a.sh`
This should "just work".
Is a.sh on your $PATH ?
If not, you need to invoke it with a path, e.g. ./a.sh
Try removing the backquotes from b.sh like below. this works for me.
#!/bin/bash
a.sh
exit
The following lines in a.sh:
echo `date` temp=`echo "$temp+1" | bc`
can be rewritten as:
date : $(( temp += 1 ))
It seems that the root problem you are having is a misunderstanding of backticks, and you should understand why "echo `date`" is (almost) exactly the same as just "date" (they differ in whitespace only). The change I made to the second line is just personal preference (it is also more efficient).
精彩评论