Posts tagged bash:

Aug posted 12 Aug 2009 and tagged ,

Add this to ~/.bash_profile, ~/.bashrc, or wherever you prefer. You can then recursively remove annoying .DS_Store files from a directory by executing the command rm_dss.

alias rm_dss='find . -name *.DS_Store -type f -exec rm {} \;'

Pop out

1
alias rm_dss='find . -name *.DS_Store -type f -exec rm {} \;'

Source: http://snippets.dzone.com/posts/show/4982

May posted 31 May 2009 and tagged , ,

This function pipes a javascript file to a Python implementation of Douglas Crockford’s handy JSMin, a javascript minifier.

minify() { if [ -f $1 ] then MIN=${1%.[^.]*}.min.js cat $1 | python ~/path/to/jsmin.py > ${1%.[^.]*}.min.js BEFORE=`wc -c <$1` AFTER=`wc -c <$MIN` echo "$BEFORE $1" echo "$AFTER $MIN ($(echo "scale=2; 100*$AFTER/$BEFORE" | bc)%)" else echo "$1 not found" fi }

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
minify() {
  if [ -f $1 ]
  then
    MIN=${1%.[^.]*}.min.js
    cat $1 | python ~/path/to/jsmin.py > ${1%.[^.]*}.min.js
    BEFORE=`wc -c <$1`
    AFTER=`wc -c <$MIN`
    echo "$BEFORE $1"
    echo "$AFTER $MIN ($(echo "scale=2; 100*$AFTER/$BEFORE" | bc)%)"
  else
    echo "$1 not found"
  fi
}

It also prints the minified version’s character count as a percentage of the original’s:

$ minify prototype.js 

  126127 prototype.js
   93689 prototype.min.js (74.28%)

Apr posted 27 Apr 2009 and tagged , ,

Disable:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

Pop out

$
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

Reenable:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

Pop out

$
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

Source: http://tech.karbassi.com/2007/11/06/leopard-turn-off-bonjour-mdnsresponder/

Mar posted 16 Mar 2009 and tagged

I wrote this bash function to print sequential lines starting from a random point in a text file. I use it to display a few lines of a song every time I open a new shell, but it’s general enough for many purposes.

lyrics() { S=$(( $(( RANDOM % $(( $(wc -l < $1)-1 )) )) + 1)) sed -n "$S,$(($S + $(($2-1)) ))"p $1 | awk '{print "+ "$0}' }

Pop out

1
2
3
4
lyrics() {
  S=$(( $(( RANDOM % $(( $(wc -l < $1)-1 )) )) + 1))
  sed -n "$S,$(($S + $(($2-1)) ))"p $1 | awk '{print "+ "$0}'
}

Add the function above to ~/.bash_login, ~/.bash_profile or ~/.bashrc, followed by this line calling it:

lyrics .my_lyrics 3

Pop out

1
lyrics .my_lyrics 3

Arguments: .my_lyrics is the name of the file containing the text, and 3 is the number of sequential lines to print. Here’s an example of what it produces in a shell:

Last login: Mon Mar 16 12:01:03 on ttys000
+ well maybe you should just drink a lot less coffee
+ and never ever watch the ten o'clock news
+ maybe you should kiss someone nice, or lick a rock, or both
heather@macbook:~ $

Mar posted 13 Mar 2009 and tagged

From Allan Odgaard’s excellent Working With History in Bash:

export HISTCONTROL=erasedups export HISTSIZE=10000 shopt -s histappend

Pop out

1
2
3
export HISTCONTROL=erasedups
export HISTSIZE=10000
shopt -s histappend

Mar posted 12 Mar 2009 and tagged , ,

Place in ~/.bash_login, ~/.bash_profile, or ~/.bashrc after changing lines 2 and 3:

my() { user="your_username"; pass="your_password"; if [ $# = 1 ] then mysql -u $user --password=$pass $1; else mysql -u $user --password=$pass --execute='show databases'; mysql -u $user --password=$pass; fi }

Pop out

1
2
3
4
5
6
7
8
9
10
11
my() {
  user="your_username";
  pass="your_password";
  if [ $# = 1 ]
  then
    mysql -u $user --password=$pass $1;
  else
    mysql -u $user --password=$pass --execute='show databases';
    mysql -u $user --password=$pass;
  fi
}

Before:

mysql -u [user] -p ([database])

Pop out

$
mysql -u [user] -p ([database])
Enter password:
mysql >

After:

my ([database])

Pop out

$
my ([database])
mysql >