Thursday, March 5, 2015

script for reading file or stream

I often wanted to pass the file to a pipeline some command, and also if the file is parameter to command it should accept


Voila!!

here is 'stream.sh'

#!/bin/bash

file=${1:--}

cat $file 


Usage and output

$ ./stream.sh underSTANDING.txt 

If you understand, say "understand".
If you don't understand, say "don't understand".
But if you understand and say "don't understand".
How do I understand that you understand? Understand!



$ cat underSTANDING.txt | ./stream.sh 

If you understand, say "understand".
If you don't understand, say "don't understand".
But if you understand and say "don't understand".
How do I understand that you understand? Understand!



$ ./stream.sh < underSTANDING.txt 

If you understand, say "understand".
If you don't understand, say "don't understand".
But if you understand and say "don't understand".
How do I understand that you understand? Understand!

bytes to gb/tb conversion

Often had to go to websites to do the bytes to KB.. TB.. GB conversions
here is the quick script for handy use

#!/bin/bash

# Give numeric value, which is in bytes
# will show all possible conversions 

call_bc() {
n1=$1
n2=$2
echo "scale=4; $n1/($n2)" |bc
}

nm=$1
pw=$2
pn=""

k_ilo=1024;
m_ega=$k_ilo*$k_ilo;
g_iga=$m_ega*$k_ilo;
t_era=$g_iga*$k_ilo;
p_eta=$t_era*$k_ilo;


[ -z "$nm" ] && exit

echo  "B: "$nm
for i in K M G T P; do
case $i in
K)pn=$k_ilo;;
M) let pn=$m_ega;;
G) let pn=$g_iga;;
T) let pn=$t_era;;
P) let pn=$p_eta;;
esac
echo -n "$i: "; call_bc $nm $pn
done

Usage and output
$ ./convert.sh 74274576
B: 74274576
K: 72533.7656
M: 70.8337
G: .0691
T: 0
P: 0