Thursday, January 27, 2011

Shell Variables are Global by Default

All variables in a shell script are global unless specifically declared as local. Local variables can be used only inside a function.

#!/bin/bash
# Global and local variables

carwash()
{
local vin="17DIGITS"
echo "IN Carwash. Your windscreen is:" $1 "and your VIN is:" $vin
post_carwash_status="BEARY CLEAN"
}

status="DIRTY"
carwash $status
echo "After carwash. Your windscreen is:" $status "and your VIN: " $vin
echo "Post Carwash Status: " $post_carwash_status

OUTPUT

IN Carwash. Your windscreen is: DIRTY and your VIN is: 17DIGITS
After carwash. Your windscreen is: DIRTY and your VIN: 
Post Carwash Status:  BEARY CLEAN
Notice that the local variable vin is not available outside the function.

ps to list only ppid

I generally use ps to get ppid to kill a process and finally figured a way to list just that.

ps -o ppid
And to list ppid without header, indicate that it should have a value. To display more information, add the headings preceded by output formatter -o.

ps -o ppid=
ps -o ppid= args= | grep Firefox 

Friday, January 21, 2011

vi Search backwards

For some reason I couldn't ever remember how to do a backward search in vi. So I would use forward search command, "/pattern" and then do "N" which is fine except the clumsy usage of uppercase.

Backward search can be done with "?pattern" and then do "n" to iterate over all the occurrences. Committed to memory now.