Wednesday, February 17, 2010

ternary operator

$ test 1 -eq 1 && echo true || echo false
true

$ test 1 -eq 2 && echo true || echo false
false

&& -- operator will execute the second command only if the previous command ran successfully (exit status=0)

|| -- operator will execute the second command only if the previous command failed (exit status=1)

Tuesday, February 16, 2010

Total no. of ocurrence of word in file

We are searching the word phrase in the file rfc2119.txt

tr will split the space in newline and grep will search for the word in be beginning or end of the line and does not have any alphanumeric character around the word.


File: http://www.faqs.org/rfcs/rfc2119.txt
cat rfc2119.txt |tr '[[:space:]]' '\n' |grep -E "(^|\W)phrase(\W|$)" |wc -l

o/p: 5

Thursday, February 11, 2010

dc: base16 to base10

dc is a reverse-polish desk calculator

echo "2i 100 p"|dc
says: 100 is of base2 give o/p in base 10

echo "16i 100 p"|dc
says: 100 is of base16 give o/p in base 10

echo "16o 10i 100 p"|dc -- read 100 in base10 and o/p in base16
echo "2o 10i 100 p"|dc -- read 100 in base10 and o/p in base2

Friday, February 5, 2010

cp

cp * copies in ascii order
mkdir l; echo hello>b; echo ding> a
cp * will copy files a b to directory l,
if l is removed, a is copied to b

bash expansion

* -- is expanded in ascii
say you create files touch {e,b,a,t,f}
and you list it; it will list a b e f t
same thing happens if you do cp or mv

Monday, February 1, 2010

Linux Prompt

Avid Windows PROMPT C:\ lover on linux
try: export PS1="C:\$(pwd|tr '/' '\\\\\')> "