Sunday, April 11, 2010

Bash if statements

I'm constantly forgetting the bash if statement syntax, and it takes a while to find the right example on the web or to read the man page. Some examples below.

One liner to get the number of seconds since a file was modified:
if [ -f /var/lib/puppet/state/puppetdlock ] ; \
then echo $[`date +%s` - `stat -c %Y /var/lib/puppet/state/puppetdlock`] ; else echo 0 ; fi

The same if statement with line breaks:
if [ -f /var/lib/puppet/state/puppetdlock ]
then
echo $[`date +%s` - `stat -c %Y /var/lib/puppet/state/puppetdlock`]
else
echo 0
fi

No comments: