Linux Utility Tools

1. Log tool

# note that the symbol around date is not single quotes
#
also that's a tool to invoke shell command, such as 'date', 'pwd', 'hostname' or something else
echoThis()
{
timestamp=`date`
#echo "[$timestamp] $@"
echo "[$timestamp] $@" > /dev/stderr
}
date '+%d.%m.%Y %H:%M'
date '+%Y-%m-%d_%H:%M'
date '+%Y-%m-%d_%H:%M:%S'
#for date's format
date --rfc-3339=seconds
#output: 2012-02-06 14:38:42+00:00

2. Blank line

betweenCommands()
{
echo
}

3. Show Linux Version

cat /etc/*release
#output: Red Hat Enterprise Linux Server release 5.7 (Tikanga)
uname -a
#output: Linux scp-vm-004 2.6.18-274.7.1.el5 #1 SMP Mon Oct 17 11:57:14 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

4. Function with return value

hello()
{
echo 'hello banny'
}
h=`hello`
echo $h

#output: hello banny
#
this is just one return value, what if I want more than one return value??
#
if you got more than one echo commands in the function, what you got are all the echo results combined together as string

5. While loop

while [ $# -ne 0 ]
do
case $1 in
-type*)
type=$2
;;
-profile_num*)
profile_num=$2
;;
-autostart*)
autostart=$2
;;
-dmgr_federate*)
dmgr_federate=$2
;;
*)
;;
esac
shift 1; shift 1

done

6. While loop with arithmetic

        max=999
index=0
while [ $index -lt $max ]
do
index=`expr $index + 1`
done

7. 文件中的换行

echo -e "\n"

new="a\n"

new=$new"b\n"

new=$new"c\n"

sed -i -e "s#$original#$new#g"

8. check last command's return code:

        hostStr=$(more /etc/hosts | grep $hostname)
        hostRtc=$?

if /etc/hosts contans '$hostname', $? = 0

else $?=1


9. floating-point solution

Built-in bash calculator (called bc) is a solution for overcoming the bash integer limitation.

9.1 using bc in scripts

#!/bin/bash
var1=`echo " scale=4; 3.44 / 5" | bc`
echo The answer is $var1
$

 

var1=10.46
var2=43.67
var3=33.2
var4=71
var5=`bc << EOF
scale = 4
a1 = ( $var1 * $var2)
b1 = ($var3 * $var4)
a1 + b1
EOF
`
echo The final answer for this mess is $var5

 It’s important to remember that any variables created within the bash calculator are only valid within
the bash calculator and can’t be used in the shell script. Such as a1, b1, c1 is not valid in shell script, but var5 is valid

10.

#!/bin/bash
IFSOLD=$IFS
IFS='\n'
echo '11'
echo $IFS
echo '11'
for entry in `cat /etc/passwd`
do
echo "value in $entry -"
IFS=:
echo '22'
echo $IFS
echo '22'
for value in $entry
do
echo "$value"
done

done
IFS=$IFSOLD
echo '333'
echo $IFS
echo '333'
#!/bin/bash
for (( a = 1 ; a <= 3; a++))
do
echo "outside loop $a"
for (( b = 1; b <=3; b++))
do
echo "inside loog $b"
echo "Produce $[$a * $b]"
tmp=$[$a*$b]
echo $tmp
done
done
#!/bin/bash
for (( i = 1 ; i < 10; i ++))
do
echo $i
done

list="a b c d e f g"
for a in $list
do
echo $a
done

for (( a=1, b=10; a<=10; a++,b--))
do
echo "$a - $b"
done
#!/bin/bash
a=`expr 1 + 1`
echo $a

b=$[ 8-1 ]
echo $b
var=10
while [ $var -gt 0 ]
do
echo $var
var=$[$var -1]
done

a=5
while echo $a
[ $a -ge 0 ]
do
echo 'this is inside '
a=$[$a-1]
done

b=5
while [ $b -ge 0 ]
do
echo $b
b=$[$b-1]
done

11. tool to send output both to the monitor and to a file for logging

method 1
./install.sh > install.log 2>&1
tail -f install.log
method 2
./install.sh 2>&1 | tee install.log

12. find

find /install/scripts/WODM8/ -name "*.py" -exec dos2unix {} \;
13. tool to clear content of file without removing it and recreate it

cat /dev/null > fileName

posted on 2012-02-06 10:44  bannyle  阅读(434)  评论(0)    收藏  举报

导航