Wednesday, April 20, 2011

Shell Script Useful Snippets

Returns todays date mysql style(2011-04-20)

#!/bin/sh

today=`eval date +%Y-%m-%d`
echo $today




Returns yesterdays date mysql style(2011-04-19)

#!/bin/sh

yesterday=`eval date --date=yesterday +%Y-%m-%d`
echo $yesterday



Returns last seven days dates mysql style(2011-04-20) (alternative method for greater number of dates in range http://www.linuxquestions.org/questions/programming-9/bash-script-date-range-876326/)

#!/bin/sh

export yesterday=`date --date="yesterday" +%Y-%m-%d`
export twodaysago=`date --date="2 days ago" +%Y-%m-%d`
export threedaysago=`date --date="3 days ago" +%Y-%m-%d`
export fourdaysago=`date --date="4 days ago" +%Y-%m-%d`
export fivedaysago=`date --date="5 days ago" +%Y-%m-%d`
export sixdaysago=`date --date="6 days ago" +%Y-%m-%d`
export sevendaysago=`date --date="7 days ago" +%Y-%m-%d`

echo yesterday was $yesterday
echo two days ago was $twodaysago
echo three days ago was $threedaysago
echo four days ago was $fourdaysago
echo five days ago was $fivedaysago
echo six days ago was $sixdaysago
echo seven days ago was $sevendaysago



bash script For Each Loop On mysql result -dont know why but the column name is returned in the results loop as the first result, so i put a condition to oly return if it was not the column name, that loops through all the values.

#!/bin/sh

##MYSQL QUERY##
variable=`
mysql -u username --password=password << eof
use databasename;
SELECT somecolumn FROM table WHERE anothercolumn='2011-04-19';
eof`

##for each##

for ARG in $variable
do
if [ "$ARG" != "somecolumn" ]; then
echo $ARG was updated yesterday
fi
done


No comments: