bash – Print dates in a range.
Problem:
Needed to have a range of dates for a script.
Solution:
Opted to write it in bash.
How it works:
shell> bash list_dates.sh "2011-07-02" "2011-07-10" 2011-07-02 2011-07-03 2011-07-04 2011-07-05 2011-07-06 2011-07-07 2011-07-08 2011-07-09 2011-07-10
Source:
#!/bin/bash #============================================================================= # # FILE: list_dates.sh # # USAGE: list_dates.sh start_date end_date # # DESCRIPTION: List the dates between a given range of dates. # #============================================================================= start_date="$1" end_date="$2" #=== FUNCTION ================================================================ # NAME: usage # DESCRIPTION: Display usage information for this script. # PARAMETER 1: --- #============================================================================= function usage() { cat <<- EOT Lists the dates between a given range of dates. Dates must be in yyyy-mm-dd format usage: $0 start_date end_date [--help] start_date (YYYY-MM-DD) -- First date in the range. end_date (YYYY-MM-DD) -- Last date in the range. help -- display this message EOT } #----------------------------------------------------------------------------- # Display usage #----------------------------------------------------------------------------- if [ "$1" == "--help" ] then usage exit fi #----------------------------------------------------------------------------- # Check the date formats #----------------------------------------------------------------------------- start_date=$(date --date="$start_date" +"%F") # Defaults to current date if none supplied. if [ "$?" -eq "1" ] || [ -z "$start_date" ] then echo "Please provide a start date in this format (YYYY-MM-DD)" exit fi end_date=$(date --date="$end_date" +"%F") # Defaults to current date if none supplied. if [ "$?" -eq "1" ] || [ -z "$end_date" ] then echo "Please provide an end date in this format (YYYY-MM-DD)" exit fi # Make sure that the first day is always before the second date. # If a user enters 2012-09-08 2011-02-10 switch the dates to be # 2011-02-10 2012-09-08 file_=$(mktemp) printf "%s\n%s" $start_date $end_date | sort > $file_ start_date=$(head -n 1 $file_) end_date=$(tail -n 1 $file_) rm $file_ #----------------------------------------------------------------------------- # main #----------------------------------------------------------------------------- i=0 while [ "$date_" != "$end_date" ] do date_=$(date --date="$start_date +$i day" +"%F") echo $date_ i=$(expr $i + 1) done
Update 2012-11-06:
Changed
date_=$(date --date="$(date --date=$start_date +%F) + $i day" +"%F")
to
date_=$(date --date="$start_date +$i day" +"%F")
ssh login without a password
Problem:
ssh login without a password
Solution:
This is a common enough problem. There are a many of solutions on the net. This one worked for me.
http://maduraisenthilscorner.blogspot.com/2011/02/ssh-login-without-password.html
NB.
In my case the machine was already configured to ssh to some servers without specifying a password. I just needed to add another server to the list. So all I had to do from the instructions from Senthil Kumar’s blog was
senthil@linux-5xxh:~> cat .ssh/id_rsa.pub | ssh siva@192.168.2.20 ‘cat >> .ssh/authorized_keys’
Password:
Once I copied over the public key. I was able to ssh without a password.
Printing variables side by side – part 1
Problem:
Print variables side by side.
Solution:
printf command For the example below echo would be best. But for a better example of when you might want to use printf read this.
shell>val1="cat" shell>val2="dog" shell>echo "$val1,$val2" cat, dog shell>printf "%s, %s" $val1 $val2 cat, dog shell>printf "The values are %s and %s." $val1 $val2 The values are cat and dog.
Cases where you would want to use printf instead of echo are explained here
http://stackoverflow.com/questions/3652524/bash-echo-string-that-starts-with
Update. (2012-04-20)
MySQL – Remove a character from a string
Problem:
Had to strip out a plus sign at the beginning of some text in the database.
Solution:
Update command and replace function in MySQL. In this case I was getting rid of the plus sign.
UPDATE `table` SET db_field=REPLACE(db_field, ‘+’, ”) where db_field like ‘+3989%’ limit 5;
Source:
http://eisabainyo.net/weblog/2007/02/26/remove-the-first-character-in-mysql/