Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output redirection in shell script not working completely

Tags:

bash

shell

>cat test.sh
#!bin/bash
testf="/home/vidya/test/"
cd $testf
`make clean all` >/dev/null 2>&1
if [ -e test ]
then
`./test << end
source(scripts/all.src);
quit;
end` >/dev/null 2>&1
echo -e "\nLog Result is:`grep Tests test.log | tail -1`\n"
fi

When I run the above script below is the output. The problem here is even though I used output redirection while executing test case but still it is printing on console like "Opened file .... " for each file. It it redirecting most of the test case execution but not completely. Is there anyway to suppress this output on console ?

[vidya ]$ sh test.sh
Opened file </home/vidya/test/scripts/all.src>
Opened file </home/vidya/test/scripts/file1>
Opened file </home/vidya/test/scripts/file2>
.
.
.
Opened file </home/vidya/test/scripts/file10>

Log Result is:    Tests: 1005 PASSED / 48 FAILED
like image 563
vidya Avatar asked May 20 '26 09:05

vidya


1 Answers

The legacy back-ticks or modern $() in bash stands for command substitution ie the output of the what is enclosed in the back-ticks will be executed as it is a command

Change

`make clean all` 1>/dev/null 2>&1

to

make clean all >/dev/null 2>&1 # Removed the backticks

Instead of output redirection, you could also think about putting

exec 1>&3 #making a copy file descriptor 1(stdout) to 3.
exec 1>/dev/null 2>&1

in the beginning of test.sh just after the shebang to run the script silently. Then at the point where you need to start printing stuff again, revert the settings

exec 3>&1 Restoring file descriptor 1 to stdout.
like image 178
sjsam Avatar answered May 22 '26 01:05

sjsam