Need output of script on screen and file with correct return status of the called script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need output of script on screen and file with correct return status of the called script.
# 8  
Old 11-13-2014
Quote:
Originally Posted by RudiC
man bash
Thanks.
My bash version is:
Code:
$ echo $BASH_VERSION
3.00.15(1)-release

---------- Post updated at 07:06 PM ---------- Previous update was at 06:58 PM ----------

Quote:
Originally Posted by sea
You can set the return code to a variable so you can reuse it at any later time again.

Example - 1.sh
Code:
#!/bin/bash
echo "I'm script: $0" | tee -a ./1.log
[[ $UID -eq 34567 ]]
RET=$?
echo "The exit code was $RET, and now it can be re-used"
echo "Returned: $RET" | tee -a ./1.log

echo "Cat'ing:"
cat 1.log

Output:
Code:
sh 1.sh 
I'm script: 1.sh
The exit code was 1, and now it can be re-used
Returned: 1
Cat'ing:
I'm script: 1.sh
Returned: 1

Hope this helps
Thanks for your reply.
I am calling child script from parent script and I want to capture the exit status of the child script into main script. Can I store exit status of child script in variable and use it in parent script?

I know that, we can export variable from parent script and use its value in child script but does it work other way?
# 9  
Old 11-13-2014
That case is about the same:
Code:
sh 2.sh
RET=$?

Or likewise:
Code:
sh 2.sh
RET2=$?

sh 3.sh
RET3=$?

or even:
Code:
for script in 2.sh 3.sh;do
    sh $script
    RET=$?
    echo "$script returned: $RET" | tee -a $script.log
    echo "$script returned: $RET" | tee -a allscripts.log
done

Quote:
Can I store exit status of child script in variable and use it in parent script?
Not unless you export the variable, in which case, they must not have the same name, or you can just fetch the latest 'output/export'.
However, you could read the logfile for sure.

hth

Last edited by sea; 11-13-2014 at 09:57 AM..
# 10  
Old 11-13-2014
Quote:
Originally Posted by sea
That case is about the same:
Code:
sh 2.sh
RET=$?

Or likewise:
Code:
sh 2.sh
RET2=$?

sh 3.sh
RET3=$?

or even:
Code:
for script in 2.sh 3.sh;do
    sh $script
    RET=$?
    echo "$script returned: $RET" | tee -a $script.log
    echo "$script returned: $RET" | tee -a allscripts.log
done


Not unless you export the variable, in which case, they must not have the same name, or you can just fetch the latest 'output/export'.
However, you could read the logfile for sure.

hth
I want to catch the output of the $script in log file (both stdout and stderr). How can I do that?

If I use tee command to capture both stdout and stderr like below, I loose the exit status.
Code:
sh $script 2>&1 | tee -a $script.log


Last edited by Prathmesh; 11-13-2014 at 10:12 AM.. Reason: included code to explain myself better
# 11  
Old 11-13-2014
I wrote a database backup script where I have exec in the script redirecting stdout and stderr in the script to a log file. Hence, when I run the script I don't see any output. But I can run the script as "nohup ./myscript.sh &" then I just tail -f on the log file to see exactly what it is doing. I tried using tee in the past to split the output, but could not get it working either. Maybe a newer bash version would work fine...
# 12  
Old 11-13-2014
What i ment was:
Code:
...
    sh $script
    RET=$?
    echo "$script returned $RET just fined... regular output to terminal"
    echo "$script returned $RET" >> common.log
    ....
    echo "$script returned: $RET" | tee -a $script.log
    echo "$script returned: $RET" | tee -a allscripts.log
...


Last edited by sea; 11-13-2014 at 11:28 AM..
This User Gave Thanks to sea For This Post:
# 13  
Old 11-13-2014
Quote:
Originally Posted by sea
What i ment was:
Code:
...
    sh $script
    RET=$?
    echo "$script returned $RET just fined... regular output to terminal"
    echo "$script returned $RET" >> common.log
    ....
    echo "$script returned: $RET" | tee -a $script.log
    echo "$script returned: $RET" | tee -a allscripts.log
...


Thanks.

Suppose we do not have control over the script which we are calling $script in this case. And it has only echo commands and it is not saving output to any file, only displaying output on terminal. But, we need to capture whatever output of echo command it is displaying in the main script (from where we are calling $script ) on screen terminal as well as in the file. Then how can we do it?
# 14  
Old 11-13-2014
Quote:
Originally Posted by gandolf989
I wrote a database backup script where I have exec in the script redirecting stdout and stderr in the script to a log file. Hence, when I run the script I don't see any output. But I can run the script as "nohup ./myscript.sh &" then I just tail -f on the log file to see exactly what it is doing.
This is really the preferred way to do it -- from the outside. Complicated schemes with pipes, tees, and fifos are liable to break down, subject to deadlocks, and can make your script dependent on a terminal: Untrustworthy and not worth the trouble. Even 99% success is not good enough. Putting in pipes also messes up the ordering, increases overhead manyfold, and can screw up your terminal prompt afterwards.

When redirecting it from the outside through tee, since you are using BASH, just check the script's return status in PIPESTATUS:

Code:
$ true | false | true | false
$ echo "${PIPESTATUS[0]} ${PIPESTATUS[1]} ${PIPESTATUS[2]} ${PIPESTATUS[3]}"
0 1 0 1

$

Code:
command 2>&1 | tee -a logfile
if [[ "${PIPESTATUS[0]}" -ne 0 ]]
then
        echo "command had an error"
fi

PIPESTATUS is even updated for a single command, so ${PIPESTATUS[0]} remains safe either way, as long as you're using BASH.

Last edited by Corona688; 11-13-2014 at 12:03 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

Output not in correct format - cd script

I have a script that looks like this: dirname2=/usr/tmp/filelist/*/* for dirname2 in /tmp/filelist/*/*; do (cd $dirname2/catalog ||echo "file does not exist" && echo "$dirname2" |cut -d '/' -f 7,8 && echo $i && ls -la |awk 'NR>3 {SUM += $5} END { print "Total number of kb " SUM }');done... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

4. Shell Programming and Scripting

Return value to shell script, depending on status of pl/sql udpate

Hi All, I need to return value to the main shell script, depending on whether the UPDATE command in the embedded pl/sql is successfu or not. #!bin/ksh updateStatus=`sqlplus --conn details-- << EOF DECLARE var_rows NUMBER; BEGIN update table_name set column_name =... (7 Replies)
Discussion started by: rituparna_gupta
7 Replies

5. Shell Programming and Scripting

Not the correct output, works fine via CLI, not inside the script.

Guys, I need you help please. The script below is not working correclty for checking via a awk/if statement . Can you tell me what i am doing wrong in the script code "if($1 == "$RETENTION_LEVEL") " Syntax RETENTION_LEVEL=`echo $LINE | cut -f2 -d" "` echo " ==============... (4 Replies)
Discussion started by: Junes
4 Replies

6. Shell Programming and Scripting

return status after run the shell script

Hello, I wanted to delete all files which are placed 14 days back. Here is my below script. My script works very well and it deletes all files 14 days back. I wanted to display message incase if the delete script is not successful. The below script returns always successful. But the directory... (6 Replies)
Discussion started by: govindts
6 Replies

7. Shell Programming and Scripting

How to return the value from the called shell script to the calling sh script

Hi all, I have two ksh scripts #sample1.sh #!/bin/ksh . ./sample2.sh echo $fileExist #sample2.sh #!/bin/ksh func() { i=1 return $a } func echo $? Here how should I return the value of sample2.sh back to sample1.sh? Thanks in advance. (2 Replies)
Discussion started by: gp_singh
2 Replies

8. UNIX for Dummies Questions & Answers

how to print script output to screen and file

Hi all, I have a script that bulk loads thousands of lines of data. I need to log the output during the execution of the script. I know I can redirect (">") the output to a file; however, I want the output going to both the screen and the log file. I thought I could use pipe to pipe the... (10 Replies)
Discussion started by: orahi001
10 Replies

9. UNIX for Dummies Questions & Answers

Return info from a called external script

New to Unix scripting and have written two scripts, one calling the other. When it returns to the calling script is it possible to return information, like a return code? :confused: (1 Reply)
Discussion started by: cathrop
1 Replies

10. Shell Programming and Scripting

Redirecting to standard output from within called script

Hi, How to achieve this? Let us assume the following: There are 2 scripts a.ksh and b.ksh $ cat a.ksh sh b.sh 2>&1 >> /work/log/a_log.txt $ cat b.sh echo "abcd" My requirement is, is there a way to display this abcd in standard output also alongside of writing into a_log.txt?... (8 Replies)
Discussion started by: vigneshra
8 Replies
Login or Register to Ask a Question