put this functions in the ~/.bashrc
# convert a number into decimal format
n2d() {
perl -e "printf \"%d\n\", $@"
}
# convert a number into hex format
n2h() {
perl -e "printf \"0x%X\n\", $@"
}
# convert a number into binary format
n2b() {
perl -e "printf \"0b%b\n\", $@"
}
Showing posts with label bash programming. Show all posts
Showing posts with label bash programming. Show all posts
Thursday, April 21, 2011
Thursday, April 29, 2010
Interesting set intersection by using 'uniq'
I was asked to find the intersection of two IP lists yesterday. I write a simple program with Perl. The program iterates the two lists to find the repeated IPs, which is a O(n^2) algorithm.
cat file-A | sort | uniq > tmp
cat file-B | sort | uniq >> tmp
cat tmp | sort | uniq -d
Basically, it is a O(nlog(n)) algorithm. (the sort operation).
When I wake up this morning, another way to do this pops onto my mind.
foreach $a (@A) {
foreach $b (@B) {
if ($a eq $b) {
...
}
}
}
cat file-A | sort | uniq > tmp
cat file-B | sort | uniq >> tmp
cat tmp | sort | uniq -d
Basically, it is a O(nlog(n)) algorithm. (the sort operation).
Monday, December 7, 2009
kill all child process
killtree () {
for child in $(ps -o pid= --ppid $1)
do
killtree $child
done
echo "kill -9 $1"
kill -9 $1 2>/dev/null
}
killtree {some pid}
for child in $(ps -o pid= --ppid $1)
do
killtree $child
done
echo "kill -9 $1"
kill -9 $1 2>/dev/null
}
killtree {some pid}
read line in bash script
It is very easy to read "words" in a bash script. But what if you want to read a line in a text file? I have this problem today, After some googling, I found a interesting way to do this.
while read l
do
echo $l
done < xxx.txt
It use the xxx.txt as input, and the read command in bash will read the input line by line.
while read l
do
echo $l
done < xxx.txt
It use the xxx.txt as input, and the read command in bash will read the input line by line.
Subscribe to:
Posts (Atom)