Archive for the ‘ Bash ’ Category

Just had a need to to represent the end of the current week in PHP. A quick Google search turned up a bunch of clumsy ways. The most common was to run the current date and day of the week through some math to get the date of Next Friday.

Fortunately thinking of those last two words reminded me of an easier way, and one that I use in BASH occasionally. PHP’s strtotime function takes a similar language that GNU Date does. So to find out the date of next friday, just use this line of code:

$week_ending = date('Y-m-d', strtotime( 'Next Friday'));

You can also use “This Friday” which will return today’s date on a Friday instead of a week from today.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

post-icon-sed.jpgSed – Stream Editor – Cheat Sheet – good coders code, great reuse

I present to you my cheat sheet of sed, the Superman of stream editing! It has come handy 101 times for me because sed is not what I use daily and after some time of not using the sed commands tend to fade away from my mind and I have to review them quickly. This cheat sheet is ideal for that!

Very cool cheat sheet for all the sed aficionados in the house.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

For a little test script I’m writing I needed to split a line on a ‘;’ but preservere the “s and ‘s, something that echo doesn’t like to do. Digging deeper into the bash docs I see that there are some handy string handling functions.

#!/bin/bash
line=’this “is” a command;this “is” a pattern’
COMMAND=${line%;*}
PATTERN=${line#*;}
echo $COMMAND
echo $PATTERN

And the output would be:

this “is” a command
this “is” a pattern

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

I’ve been asked this a number of times and always have to look it up, so here are 3 ways to convert a unix timestamp (seconds since Jan 1, 1970 GMT) to a real looking date.
Perl method 1: use the ctime module:

perl -e “require ‘ctime.pl'; print &ctime($EPOCH);”

Perl method 2: use the scalar and localtime functions:

perl -e “print scalar(localtime($EPOCH))”

Awk has a wrapper for the standard C strftime function:

echo $EPOCH|awk ‘{print strftime(“%c”,$1)}’

Here’s a sample script that uses all methods.

!#/bin/bash
EPOCH=1000000000
DATE=$(perl -e “require ‘ctime.pl'; print &ctime($EPOCH);”)
echo $DATE
DATE=$(perl -e “print scalar(localtime($EPOCH))”)
echo $DATE
DATE=$(echo $EPOCH|awk ‘{print strftime(“%c”,$1)}’)
echo $DATE

[update: Thanks to S. Maynard for reminding me of the proper use of quotes and how to avoid using the pipe...]
DATE=$(awk “BEGIN { print strftime(\”%c\”,$EPOCH) }”)

[UPDATE]

A reader found another way listed below. This doesn’t seem to be as portable (The mac ignores the –date and -d is an illegal option).
# date –date=’1970-01-01 1000000000 sec GMT’
Sat Sep 8 20:46:40 CDT 2001

[UPDATE]
# date -d @1000000042
Sun Sep  9 01:47:22 GMT 2001

But this only works on newer versions of date.  It fails on my FC2 server and my Debian Sarge machine, but works fine on Ubuntu Feisty and Debian Etch.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Open Tech Support – [Unix/Linux] Linux Shell One-Liners

A handy little list of some one liners.  I use stuff like this every day and should really get in the habit of posting them.

A short introduction to getting a little more from your shell with piping & redirection

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

functions and error handling

.!.

Last time I tweaked the parser, tonight I’m going to move some of the code into functions. Many people think of functions as a way to reuse the same code, but they can also be used to seperate logic into bite sized chucks. This can often lead to code that’s easier to read, and makes modifying things later easeir, both for you, and anyone else who wants to enhance the script.

#!/bin/bash shopt -s nocaseglob BASEDIR="/mnt/usb0/mp3/podCast" FEEDS="${BASEDIR}/feeds.lst" CACHEDIR="${BASEDIR}/cache" LOGFILE="${BASEDIR}/log"

Not much change here except I’ve added two config variables, CACHEDIR, and LOGFILE. The former will be used for caching the feed XML to help minimize network transfers, both for us and more importantly the feed provider. The later is for reporting errors, some of the functions will output lists of things to be piped into a loop, we can’t just echo the errors.

Read the rest of this entry

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS