$bash_command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $bash_command
# 1  
Old 03-27-2017
$bash_command

Hi,

I am writing an error logging script where upon error, the erroneous line
Code:
$LINENO

and erroneous command are captured
Code:
$BASH_COMMAND

Code:
trap 'echo $LINENO $BASH_COMMAND' ERR

But I don't know how to suppress logging error on some code where I redirect the error to /dev/null/ as below -

Sample code -

Code:
 
 files_available=`ls ${dir}/${file_name} 2> /dev/null`

The trap is logging error line no. and the command for the above line when the files are not available. Is there a way to suppress the logging of the redirected errors?

-P
# 2  
Old 03-27-2017
Reset the trap, and set afterwards again?
# 3  
Old 03-27-2017
Quote:
Originally Posted by Panna
But I don't know how to suppress logging error on some code where I redirect the error to /dev/null/ as below -

Sample code -

Code:
 
 files_available=`ls ${dir}/${file_name} 2> /dev/null`

Replace the ls by echo, and set the shell option nullglob before the command (and unset it afterwards, unless you want to keep this setting).
# 4  
Old 03-27-2017
Quote:
Originally Posted by Panna
Sample code -

Code:
 
 files_available=`ls ${dir}/${file_name} 2> /dev/null`

The trap is logging error line no. and the command for the above line when the files are not available. Is there a way to suppress the logging of the redirected errors?

-P
In general, I agree with RudiC.

In the case above you are not actually using wildcards so ls should be producing one or zero files. Why not this instead?
Code:
if [ -f "${dir}/${file_name}" ]
then  files_available="${dir}/${file_name}"
else files_available=
fi

If a wildcard is involved (perhaps in your script filename is set to, say, *.c), you could try this:
Code:
shopt -s nullglob
files_available=`printf "%s\n" "${dir}/${file_name}" 2> /dev/null`

Note that this is a bashism that came from zsh so you would have to check if it works with your shell.

Andrew
# 5  
Old 03-27-2017
Quote:
Originally Posted by apmcd47
In general, I agree with RudiC.

In the case above you are not actually using wildcards so ls should be producing one or zero files.

You are right, Andrew! I don't know why I believed this would be a wildcard expression! I must have been out of my mind!

Thank you for pointing this out...
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question