Logging shell scripts

I read a lot of info from here.

However, I didn’t quite understood it, so I tried it out.

I made a small script, and made it run in several ways:

#!/bin/bash
COUNT=0
while [[ $COUNT -lt 10 ]]; do
ls -l /tmp
ls -l /non_existing_folder
sleep 5
let COUNT=$COUNT+1
done

This just shows stdout and stderr on screen:

$ ./out.tst.sh
total 0
ls: cannot access /non_existing_folder: No such file or directory
total 0
ls: cannot access /non_existing_folder: No such file or directory
total 0

This resulted in stdout being appended to the file, but stderr beng shown on screen:

$ ./out.tst.sh >> out.tst.log
ls: cannot access /non_existing_folder: No such file or directory
ls: cannot access /non_existing_folder: No such file or directory
ls: cannot access /non_existing_folder: No such file or directory

This resulted in all output being sent to the file, overwriting the old file:

./out.tst.sh &> out.tst.log
^C

This resulted in all output being appended to the file and the process being sent to the background:

./out.tst.sh &>> out.tst.log &
[1] 4662

This resulted in having stdout in one file, stderr in another one, and the process being sent to the background:

./out.tst.sh 1>> out.tst.ok.log 2>> out.tst.err.log &
[1] 4705

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s