Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Sunday, January 11, 2015

goto in bash

GOTO is frowned upon. Bash simply removed goto. this causes problems, particularly if you do have a need to skip to a particular section of code. Web search turned up various people recommending the use of functions instead. This still doesn't solve the problem of non-local resumption.

Closest I found was Bob Copeland's approach: http://bobcopeland.com/blog/2012/10/goto-in-bash/

It's an interesting use of sed to much up the script up to the "label" and then executing the script. Very Neat trick. I wanted something simpler within Bash itself.

My problem is pretty much the same as Bob indicates on his web-page: i have a script that must process things in several steps, each of which is time consuming and can fail. Re-running prior steps is prohibitive, so I need some mechanism to resume from where the script last failed (or close to it).

So here's what I came up with:



#!/bin/bash

function usage() {
  echo "$0 [step]"
}

label=$1;
if [ -z "$label" ]; then  label="step1"; fi

# do all common setup here. This stuff will be done each time the script is run

while true; do
 echo processing step [$label]; # to give a hint about where to restart from
 case "$label" in
  "step1")
      # add processing for step1 here
      label="step2"
      ;;
  "step2")
      # add processing for step2 here
      label="step3"
      ;;
  "step3")
     # add processing for step3 here
      label="end"
      ;;
  "end") echo done; break;;
  *)
    usage
    exit
    ;;
 esac

done

Here's how it runs:
 # /tmp/test_goto.sh 
processing step [step1]
processing step [step2]
processing step [step3]
processing step [end]
done

 # /tmp/test_goto.sh step2
processing step [step2]
processing step [step3]
processing step [end]
done


Wednesday, August 28, 2013

Getting the prompt to look right in MacOS X

New Macbook Air. Yessir! The Lenovo went back, and instead came along a Macbook Air, core i7, with 4GB of RAM and 512GB SSD sweetness. The battery life is amazing. Other things still need to be worked out, like you know, the immediate lack of Excel (and other Office products). Numbers is able to open and work with some basic Excel workbooks, but none of the shortcuts are around (obviously). And Pages is no Word (ya think?).

Also, one of the first things I did was get iTerm2 on there. I prefer it to the native Terminal application. E.g. it allows you to draw a window border so that you can see where overlapping terminals are. Plus I like the fact that I don't have to futz with the background (it comes with a dark background and white text by default).

And since I got terminal going, I need to colorize the bash prompt (naturally!). Here's a useful bash snippet.


cat ~/.bash_profile
PS1='\e[1;32m\u\e[m@\e[0;31m\h\e[m:\e[2;33m\w\e[m # \n\$ '
# PS1='\u@\h:\w # \n\$ '
alias ls='ls -G'
alias dir='ls'
alias grep='grep --color'
alias rebash='source ~/.bash_profile'

resultant output looks like below:

user@machine:/your/path #
$


The prompt colorization is in the first line of the script that describes the shell variable PS1. Colors are specified as \e[x;ym stuff you want to colorize \e[m
The \e[m are shell escape markers. Think of them as "quotes" for the shell.

More references here:
http://en.wikipedia.org/wiki/ANSI_escape_code
http://en.wikipedia.org/wiki/Control_character
http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html