27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Perl, sed, and awk have far superior string parsing facilities. A short sed or awk "subroutine" within a script<br />

(see Section 34.2) is an attractive alternative to using expr.<br />

See Section 9.2 for more on string operations.<br />

12.3. Time / Date Commands<br />

Time/date and timing<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

date<br />

Simply invoked, date prints the date and time to stdout. Where this command gets interesting is in<br />

its formatting and parsing options.<br />

Example 12−7. Using date<br />

#!/bin/bash<br />

# Exercising the 'date' command<br />

echo "The number of days since the year's beginning is `date +%j`."<br />

# Needs a leading '+' to invoke formatting.<br />

# %j gives day of year.<br />

echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."<br />

# %s yields number of seconds since "UNIX epoch" began,<br />

#+ but how is this useful?<br />

prefix=temp<br />

suffix=`eval date +%s` # The "+%s" option to 'date' is GNU−specific.<br />

filename=$prefix.$suffix<br />

echo $filename<br />

# It's great for creating "unique" temp filenames,<br />

#+ even better than using $$.<br />

# Read the 'date' man page for more formatting options.<br />

exit 0<br />

The −u option gives the UTC (Universal Coordinated Time).<br />

bash$ date<br />

Fri Mar 29 21:07:39 MST 2002<br />

bash$ date −u<br />

Sat Mar 30 04:07:42 UTC 2002<br />

zdump<br />

Echoes the time in a specified time zone.<br />

bash$ zdump EST<br />

EST Tue Sep 18 22:09:22 2001 EST<br />

time<br />

Chapter 12. External Filters, Programs and Commands 158

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!