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.
# 1  
Old 11-13-2014
Need output of script on screen and file with correct return status of the called script.

Hi,

I am trying to capture logs of the script in the file as well as on the screen. I have used exec and tee command for this. While using exec command I am getting the correct output in the file but, script output is not getting displayed on the screen as it get executed.

Below is my sample script. (I have deliberately introduced an error in the script to check exit status).

Code:
$ cat 1.sh
echo "Hi"
echo "how"
echo "are"
echo "you?"
echo "I am fine.."
cp l c
a=`echo $?`
echo $a
if [ $a -ne 0 ]
then
        echo "error in script1"
        exit 1

fi
$


$ cat 2.sh
#!/bin/bash
exec 1> ./2.log 2>&1
echo "Script 2 started"
sh 1.sh
echo $?
echo "This is part of script2"
$

Output:

Code:
$ sh 2.sh
$


$ ls -ltr 2.log
-rw-rw-r--  1 dbcomdev dbcomdev 137 Nov 13 02:48 2.log
$


$ cat 2.log
Script 2 started
Hi
how
are
you?
I am fine..
cp: cannot stat `l': No such file or directory
1
error in script1
1
This is part of script2
$

I have tried to use tee command with exec command but it is giving me error.

Code:
$ cat 4.sh
#!/bin/bash
exec 1> >(tee -a ./2.log) 2>&1
echo "Script 2 started"
sh 1.sh
echo $?
echo "This is part of script2"
$

Output:
Code:
$ sh 4.sh
4.sh: line 2: syntax error near unexpected token `>'
4.sh: line 2: `exec 1> >(tee -a ./2.log) 2>&1 '
$


I have also tried to use the tee command, but I am loosing exit status of the script called within main script.

Code:
$ cat 3.sh
#!/bin/bash
echo "Script 2 started"
sh 1.sh 2>&1 | tee -a ./2.log
echo $? | tee -a ./2.log
echo "This is part of script2" | tee -a ./2.log
$


Output:

Code:
$ sh 3.sh
Script 2 started
Hi
how
are
you?
I am fine..
cp: cannot stat `l': No such file or directory
1
error in script1
0
This is part of script2
$

Code:
$ cat 2.log
Hi
how
are
you?
I am fine..
cp: cannot stat `l': No such file or directory
1
error in script1
0
This is part of script2
$

Please advise how to get script output on screen as well as within file without loosing exit status.
I am using Linux machine.
# 2  
Old 11-13-2014
The exec > >(tee...) works with my GNU bash, version 4.3.30(1) - maybe your bash is older? To get at the exit codes of commands in a pipe, try (man bash)
Quote:
PIPESTATUS
An array variable (see Arrays below) containing a list of exit status values from the processes in the most-recently-executed
foreground pipeline (which may contain only a single command).
whose availability may also depend on your version.

Last edited by RudiC; 11-14-2014 at 04:12 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-13-2014
Quote:
Originally Posted by RudiC
The exec > >(tee...) works with my GNU bash, version 4.3.30(1) - maybe your bash is older? To get at the exit codes of commands in a pipe, try (man bash) whose avqailability may also depend on your version.

Thanks for your reply. But, it is not working for me.
My unix version is:

Code:
$ uname -a
Linux XXXXX 2.6.9-89.35.1.ELsmp #1 SMP Tue Jan 4 22:29:01 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

# 4  
Old 11-13-2014
What be your bash version?
# 5  
Old 11-13-2014
Quote:
Originally Posted by RudiC
What be your bash version?
Can you please advise, how can I check bash version?
# 6  
Old 11-13-2014
man bash
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-13-2014
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
This User Gave Thanks to sea 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