From Day to Day

The surprising results of

mkdir `date '+%Y-%m-%d' && cd `date '+%Y-%m-%d'

at around midnight should have been expected.
How embarrassing!
It is—of course!—a slightly better idea to do something in the line
of:

TMPDATE=`date '+%Y-%m-%d'`;mkdir $TMPDATE && cd $TMPDATE

Or get silly and do it with a full blown script:

##########################################################################
# Create an empty directory named after the current date in the          #
# format: the four digit year, a minus-sign, the numerical month started #
# at one (1), a minus sign, and the numerical day of the month started   #
# with one (1)                                                           #
# Returns 1 for failure and 0 for success                                #
# Place in ~/.bashrc (or some other startup file) to make it work        #
# If you have chosen ~/.bashrc re-read it with                          #
#  . ~/.bashrc                                                           #
# to get a sense of achievement                                          #
# Please note, that there is a period in front of ~/.bashrc!             #
##########################################################################

mkdatedir(){
  # Full path to the date(1) program
  DATEP="/bin/date"
  # Full path to the mkdir(1) program
  MKDIRP="/bin/mkdir"
  # Full path to the ls(1) program
  LSP="/bin/ls"
  # Arguments to ls(1)
  LSA=" -d"
  # Format of the date
  DATEF=' +%Y-%m-%d'
  
  #########################
  # Actual program logic: #
  #########################
  
  # Get the date as a string and keep it in the temporary variable named
  # TMPDATE
  
  TMPDATE=$($DATEP $DATEF)
  
  # Uncomment for debugging purposes
  # echo "TMPDATE= $TMPDATE"

  # Try to make the directory
  $($MKDIRP $TMPDATE > /dev/null 2>&1 )
  # save the return value of mkdir(1) in the variable MKDIRR
  MKDIRR=$?
  # Check for errors
  if [ $MKDIRR  -eq 1 ]  
  then
    echo "Something went wrong while trying to create the directory $TMPDATE"
    TMPLS=$($LSP $LSA)
    if [ $TMPLS  ]
    then
      echo "The directory already exists"
    fi
    return 1
  fi
  
  # Jump right into the directory just successfully build
  cd $TMPDATE
  
  # TODO: write a check to check if "cd" failed
  
  # Uncomment for debugging purposes
  # echo $($LSP $LSA ../$TMPDATE)
  
  # Brag about it
  # echo "Success!"
  return 0
}

Yes, you’re right, if I say “silly” I mean silly!
So, if you ever find this code,or something similar in weird- and/or silliness: send it to Alex Papadimoulis’ site if you haven’t done so already.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s