Sunday, November 18, 2012

shell script: array

# space separated
alphabets=("a" "b" "c" "d")

#To access any element by index
${alphabets[1]}

# to loop
for f in "${alphabets[@]}"

Thursday, November 8, 2012

python: tips and tricks

1.

m = {'a': 1, 'b': 2}

m[ 'c' ] will throw error. Instead use m.get( 'c', 'default' )

2.

'foo'.index( 'bar' ) throws Exception

'foo'.find( 'bar' ) returns -1

Saturday, November 3, 2012

shell script: file information


file=/foo/bar/myfile.txt

File modify date and time

file_modify_date=$(stat -c %y $file | cut -d" " -f1)

file_modify_time="$(stat -c %y $file | cut -d" " -f2 | cut -d":" -f1,2 | tr -d ":")

File directory, name and extension


filename="${file##*/}"  # get filename

extn="${filename##*.}"

filename="${filename%.*}" # removing extension

if [[ $filename == $extn ]]; then
    extn=""
else
    extn=.$extn
fi

dirname="${file%/*}" # get dirname

basename is a good command too

References:

man stat
man basename

http://www.thegeekstuff.com/2010/07/bash-string-manipulation/