Tech Blog :: bash


Jan 30 '12 11:39am
Tags

Git trick: Cleanup end-of-line changes

I was working on a site recently that was moved from one VPS hoster to another, and the ops team that moved it somehow introduced an end-of-line change to every file. I didn't want to commit these junk changes, so I wrote this little snippet to clean them. For each modified file (not new ones), it counts the number of changed lines, excluding EOL changes, and if there are none, it checks out a clean copy of the file:

git st --porcelain | grep -r "^ M" | awk '{ print $2 }' | while read FILE; do
  LINES=`git diff --ignore-space-at-eol "$FILE" | wc -l`;
  if [[ "$LINES" -eq "0" ]]; then
    git checkout "$FILE"; 
    echo "$FILE"; 
  fi;
done
Dec 28 '10 12:50am
Tags

Alias to validate PHP and stage to Git in one step

One of the biggest faux pas that can happen working with PHP apps like Drupal (or any interpreted language for that matter) is deploying invalid syntax that brings down a whole site. So I like to run my PHP files through the PHP validator before committing. I'm working with Git, so this alias (put in .bash_profile for example) validates PHP files and then stages them to Git for committing (or doesn't stage them if there are errors). I'm including the extensions .php, .module, .install, and .inc as PHP files (please let me know if I'm missing any common ones):

[Update 1/9/11: I replaced the original alias syntax with a new function syntax that works more reliably.]

function git-v-add {
 git st --short | while read FILE; do echo $FILE | awk '{ print $2 }' | grep -E "(\.module|\.php|\.install|\.inc)$" | xargs php -l || return 1;
 done && \
 git add -A && git st
}

Then just run git-v-add && git commit.

Dec 5 '10 12:15pm
Tags

Bash code to check free disk space

I have a script on a Linux server that downloads a bunch of very large files, and I want it to stop running if the disk is low on space. The way I found to do it is very verbose. Breaking it out line by line:

FREE=`df -H   # free space for all volumes
  | grep -E '^/dev/xvda3'    # the main disk
  | awk '{ print $4 }'    # the free space column
  | cut -d'G' -f1        # strip the G (for gigabytes)
  | awk -F '.' '{ print $1 }'`    # strip decimal parts
if [[ $FREE -lt 7 ]]; then
  echo "Less than 7GB free space left, stopping."
  exit 1
fi

There's gotta be a simpler way to do that... anyone know what that is?

Nov 14 '10 12:22am
Tags

Cron Wrapper Script

I was having issues with a server's crontab, but the complexity of the cron commands (with logging and error-catching added to each) made it hard to debug. So I wrote this general cron wrapper script. (It's built for Ubuntu Linux, should work in any bash shell, but haven't tested it in other environments.)
To use, extract cron-wrap.sh somewhere on the server. Then in your crontab, you call it like so (this example runs every minute):

WRAPPER=/path/to/cron-wrap.sh
CRON_LOG=/var/log/cron.log
 
* * * * * $WRAPPER -d "Doing something" -s "/usr/local/dosomething.sh" -l $CRON_LOG &>> $CRON_LOG

It'll collect all output (regular and error), then dump it as a block into the specified log file.

The trailing 2>> $CRON_LOG in the example will catch errors in the wrapper script itself; errors in the command are caught by the script.

Run cron-wrap.sh by itself to see the usage. (There are 3 one-letter arguments.)