Li’l bash scripts

After coming across Modernizr’s compress bash script, I’ve been adding my own li’l bash scripts here and there in my projects. They help automate any repetitive task, or provide a shortcut for a bash command I don’t want to remember.

Development

Make a sweet bash script, like helloworld.sh:

#!/bin/bash
echo 'Hello world!'

Make the script executable by changing the permissions:

$ chmod 755 helloworld.sh

Run the script from the command line

$ ./helloworld.sh
# > Hello World

Examples

metafizzy.co / newpost.sh and dropshado.ws / newpost.sh: creates a new post from a template

#!/bin/bash

# creates a new post
# format as _posts/YYYY/YYYY-MM-DD-filename.ext
# usage:
#   $ ./newpost.sh my-new-post-filename

COPY_FILE=_posts/template.mdown
# create file
POST_FILE=_posts/$(date "+%Y/%Y-%m-%d")-$1.mdown

echo new post: $POST_FILE
cp $COPY_FILE $POST_FILE
# open it
mate $POST_FILE

demo / deploy.sh and desandro / deploy.sh: rsyncs project to remote server

#!/bin/bash
rsync -avz --exclude '.git' --exclude '*.sh' ./ $BERNA:~/www/demo

neo-vision / build.sh: moves release files into sub directory, compresses JS, zips it all up

#!/bin/bash

BUILD_DIR='neo-vision'
ZIP='neo-vision.zip'
REMOVABLES=("build.sh" "prettify.js" "lang-css.js" "README.mdown" $ZIP)

rm -rf $BUILD_DIR/
mkdir $BUILD_DIR
cp *.* $BUILD_DIR

# minifies prettify js
cat prettify.js lang-css.js | uglifyjs -nc > $BUILD_DIR/prettify.min.js

for REMOVE in "${REMOVABLES[@]}"
do
    rm $BUILD_DIR/$REMOVE
done

zip -r -u $ZIP $BUILD_DIR

Coloring remote command line prompts

Picking up on a running theme on the dropshado.ws, I gots to have my command line prompts looking fresh. Locally, I’m now using bash-it, with its killer theming engine by John Schulz to make it happen.

Just recently I realized that I can style my prompt on my remote server when I SSH. Here’s what I added to ~/.bash_profile.

PS1="\[\e[0;32m\]\u@\h \[\e[0;34m\]\w \[\e[0;30m\]# \[\e[39m\]"

Which looks like

username@server ~/path/to/dir # writing sweet commands

Strings in terminal

I’ve finally clued in to setting and using simple strings within Terminal. Set a string:

name='David DeSandro'

No spaces around the =. Quotes are required if your string has spaces. Reference it with $

echo $name
# will output `David DeSandro`

For example, when creating a new post, I have to reference the same long filename several times.

dropfile='_posts/2011/2011-04-12-terminal-strings.mdown'
mate $dropfile
git add $dropfile
tumblr $dropfile

Coloring the Terminal prompt

Along with being the file to store your Terminal aliases, ~/.profile also allows you to color the Terminal prompt. This article has the particulars: BASH Shell change the color of my shell prompt under Linux or UNIX

First take a look at the current prompt with echo $PS1. It should return with \h:\W \u\$. The articles above detail what those variables represent, and it translates to host:directory username$. Color syntax starts with [\e[0;31m\] and ends with \[\e[m\]. The color code itself is 0:31, where 31 is the color (in this case red) and 0 being the equivalent of font-weight (bold is 1).

I am currently rocking:

export PS1="\[\e[0;31m\]\h:\[\e[m\]\[\e[0;34m\]\W\[\e[m\] \[\e[0;30m\]\u\[\e[m\]\[\e[0;33m\]\$\[\e[m\] "

Which is the same host:directory username$ but now red:blue darkgray and a beige $.

Colored BASH Terminal

UPDATE 16 Nov 2010 Revised opening and closing tags to \[\e[0;31m\] and \[\e[m\]. Previous version didn’t close tags properly and messed up line breaks. Removed link to erroneous article.