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.
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.
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
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)