November 10, 2010
Keep trying till you succeed with Bash

The following Bash snippet will repeat a command with the given delay until it succeeds.

while [ "$?" -gt "0" ] ; do
    sleep 120 # delay in seconds between retries
    # insert command to retry here
done

Unix processes return integers when they exit. These values can be read via $? in Bash. As a matter of convention, a process that completes without errors will return 0, so by checking for exit codes greater than zero we can detect errors and retry.

4:18pm  |   URL: http://tmblr.co/Zn_4by1Rb4ez
  
Filed under: bash cli 
October 23, 2010
Secure delete with rm on OS X

I discovered this gem in the rm man page on OS X the other day:

-P  Overwrite regular files before deleting them.  Files are
    overwritten three times, first with the byte pattern 0xff,
    then 0x00, and then 0xff again, before they are deleted.

So there you go rm -P will securely delete a file from your disk with minimum hassle (i.e. without having to through the Thrash folder). This might be one of those goodies OS X inherited from BSD. On Linux you have to use something like shred to achieve the same thing.

Addendum: santry on the HN Thread points out the same thing can done via the srm command. Several people also noted that secure delete interacts unpredictably with modern file systems. jrockway points out that full disk encryption should be used in place of secure delete for this reason.

9:02pm  |   URL: http://tmblr.co/Zn_4by1Ia1s9
  
Filed under: cli rm os x security 
March 9, 2010
Find file, cd to parent directory

For jumping around in elaborate source trees.

Usage:

cdto [search root directory] [case insensitive filename]

If you just want to search current directory:

cdto [case insensitive filename]

Automatically appends * to search filename.

Update October 10, 2011: There are some nifty cd related scripts in this assorted archive of shell scripts on GitHub. The CDPATH stuff is pretty neat.

5:48pm  |   URL: http://tmblr.co/Zn_4byQ5MlQ
  
Filed under: bash cli 
August 8, 2009
The poor man’s Bash Tab-completion

Bash has a built-in tab completion utility that sports an impressive array features and the ability to generate suggestions using complex logic. But what if you just want to add a static list of suggestions to a command? It took a little digging around the Bash manual but I finally found the magic words you need to add to you .bashrc file:

complete -o default -W "list of space separated words" [command]

Obviously, you need to replace list of space separated words and [command] with values of your choice. For the curious, let’s take this command apart and see what’s going on:

  • complete: Built in Bash command for controlling built-in tab completion behavior
  • -o default: Tell Bash to fall back on the default filename completion if no matches are found.
  • -W “list of space separated words”: This is where the magic happens. -W allows us to just provide a static list of suggestions. There are other flags that will dynamically evaluate suggestions at runtime, but we’ll leave that to a future post.
  • [command]: The command that tab-completion will apply to.

Say I want to just add the suggestions “all” and “clean” to the command make. The line I need to append to my .bashrc becomes:

complete -o default -W "all clean" make

Bonus: get a little dynamic

Using command substitution we can also generate the list of suggestions at Bash start time. This is very useful for completing names that can be found in other configuration files (like, say, SSH host-name completion).

complete -o default -W "`echo $(cat /path/to/file | grep 'lines i want')`" \
    [command]

(You might notice that we are using command substitution twice in the example above, once with back-ticks the other time using $(command). This is necessary because the -W argument does not accept new-line characters as delimiters. echoing will convert these new-lines in to spaces.)

Update: The friendly folks over at HN have pointed out that there non-stupid ways of putting together the aforementioned command substitution. Thanks for the correction folks! Here’s a better way to do it:

complete -o default -W "$(grep 'lines i want' /path/to/file | tr '\n' ' ')" \
    [command]

6:14pm  |   URL: http://tmblr.co/Zn_4by9TfRw
  
Filed under: bash cli howto 
Liked posts on Tumblr: More liked posts »