user3616128
user3616128

Reputation: 377

Read the error output of the sed command in shell script

In case the sed command below fails I want the reason of its failure i.e. error output should also be added as part of the email. How it can be done?

Code is :

sed -e  abc.txt > /net/abc.com/test/abc.txt

#Sending email

if [ -f /net/abc.com/test/abc.txt ]; then
       echo "content of abc.txt saved to server" | mail -s "Content of abc" [email protected]
else
       echo "File does not exist"
fi

After "File does not exist" reason of the failure of sed should be appended.

Upvotes: 0

Views: 909

Answers (1)

paxdiablo
paxdiablo

Reputation: 882626

The sed command sends its errors to (surprisingly enough) standard error. So you can catch it and treat it specially, as per the following transcript:

pax> echo hello | sed 's/missing/terminator' >outfile 2>errfile

pax> cat outfile

pax> cat errfile
sed: -e expression #1, char 20: unterminated `s' command

Upvotes: 3

Related Questions