Thursday, January 22, 2009

Remove first 3 lines in a file

$cat bearish.htm | awk 'NR>3'

while statement

C-style while loop:

#!/bin/bash
# wh-loopc.sh: Count to 10 in a "while" loop.
LIMIT=10
a=1
while [ "$a" -le $LIMIT ]
do
echo -n "$a "
let "a+=1"
done           # No surprises, so far.
echo; echo
# +=================================================================+

# Now, repeat with C-like syntax.

# Double parentheses permit space when setting a variable, as in C.
((a = 1))      # a=1

# Double parentheses, and no "$" preceding variables.
while (( a <= LIMIT ))
do 
echo -n "$a "  
((a += 1))
# Double parentheses permit incrementing a variable with C-like syntax.
done
exit 0

# +=================================================================+
# reading a file line by line

while read line 
do 
echo "${line}"
done < ./straddle.prop



case statement

case $1 in
-s) sec=1; shift;;
-m) sec=60; shift;;
-h) sec=3600; shift;;
-d) sec=86400; shift;;
*) sec=86400;;
esac

Float/Real Number Comparison

if [ $( echo "5.5 > 10.5" | bc ) -eq 1 ]; then
echo 5.5 is greater than 10.5.
else
echo 5.5 is NOT greater than 10.5.
fi
=> 5.5 is NOT greater than 10.5.

Regular Expression in if in bash script

if echo "4567" | grep -q "^4"; then
echo "4567 is started with a 4."
elif echo "4567" | grep -q "^A"; then
echo "4567 is started with a A."
else
echo "4567 is NOT started with a 4 or A."
fi

Get Create Table statement from MySQL

$ sql "show create table my_table\G" | sed -e '1,2d;s/Create Table: //'
Note: sed -e '1,2d' removes the first 2 lines.

Change Prompt

$
$ PS1="myPrompt> "
myPrompt>
myPrompt>