Linux and Unix Shell - 一些容易的Shell脚本

Linux and Unix Shell -- 一些简单的Shell脚本
1, 控制流结构
<1> if then..else
#!/bin/sh 
# 3Paras.src ###需要携带三个参数###
if test $# -lt 3;
then echo "Usage: `basename $0` arg1 arg2 arg3">&2
exit 1
fi
echo "arg1: $1"
echo "arg2: $2"
echo "arg3: $3"

<pre name="code" class="plain">#!/bin/sh
#create file ###创建文件###
Dir=$1
if test "$Dir" = "";
then echo "Usage: `basename $0` directory to create" >&2
exit 1
fi
if test -f $Dir
then :
else
echo "The directory does not exist"
echo -n "Create it now? [Y..N]:"
read CH
if [ "$CH" = "Y" ] || [ "$CH" = "y" ]
then echo "Creating now"
touch $Dir >/dev/null 2>&1
if [ $? != 0 ];
then
echo "Errors creating the directory $Dir" >&2
exit 1
fi
else :
fi
fi

#!/bin/sh
# findname ###在一个List中查找名字###
echo -n "Enter a list of names:"
read list
if echo $list | grep "Peter"
then echo "Peter is here"
else
echo "Peter is not in the list."
fi

<pre name="code" class="plain">#!/bin/sh
###输入名字显示名字### 
echo "Please Enter Your Name: "
read Name
if test "$Name" = "";
then echo "You did not enter any information"
else echo "Your Name is $Name "
fi

<pre name="code" class="plain">#!/bin/sh
#copy file ###复制文件###
if cp $1 $2 > /dev/null 2>&1
then :
else
echo "`basename $0`: ERROR failed to copy $1 to $2"
exit 1
fi

<2> case
#!/bin/sh
#weekly report ###创建一个编辑Weekly Report的提示系统###
echo "Weekly Report"
echo -n " What day do you want to run report [Saturday]: "
read day
echo "validating..${day:="Saturday"}"
case $day in
Monday|MONDAY|monday) ;;
Sunday|SUNDAY|sunday) ;;
Saturday|SATURDAY|saturday) ;;
*) echo "This weekly report can only be run on Saturday, Sunday, Monday" >&2
exit 1
;;
esac
echo "Please choose the weekly report type: "
echo "1. FULL"
echo "2. INCR"
echo -n "Enter the type of report: "
read type
case $type in
1) type=FULL ;;
2) type=INCR ;;
*) echo "`basename $0`: Unknown type." >&2
exit 1
;;
esac
echo "$type report to run on $day"

<3> for
#!/bin/sh
#file counter ###返回一个路径下文件的个数###
counter=0
for files in *
do
counter=`expr $counter + 1`
done
echo "There are $counter files in `pwd`"

<4> while
#!/bin/sh
#While Read ###一次性读入多个参数###
echo "Type <CTRL -D> to terminate"
echo -n "Enter the film names which you have watched: "
while read filename
do
echo "Yeah, that is a great film :$filename"
done

2,函数
#!/bin/sh
#Check Str Length ###查看字符串长度的函数###
str_length()
{
_str=$1
if [ $# -ne 1 ];
then echo "need a string to check its length!"
return 1
fi
echo $_str|awk '{print length($0)}'
}
echo -n "please input a string: "
read string
str_length "$string"

#!/bin/sh
#File to Upper ###将文件内容修改为大写的函数###
file_upper()
{
_filename=$1
if [ $# -ne 1 ];
then echo "I need a filename to upper"
return 1
fi
while read line
do
echo $line|tr '[a-z]' '[A-Z]'
done<$_filename
}
echo -n "Please input the file name you want to upper: "
read file_name
file_upper $file_name

#!/bin/sh
###只允许字符类型的输入###
func_name()
{
_letters_only=$1
_letters_only=`echo $1|awk '{if($0~/[^a-z A-Z]/) print "1"}'`
if [ "$_letters_only" != "" ]
then
return 1
else
return 0
fi
}

err_name()
{
echo " $@ contains errors, it must contain only letters"
}

################Functions End#######################

while :
do
echo -n "What is your surname: "
read s_name
if func_name $s_name
then
break
else
err_name $s_name
fi
done

while :
do
echo -n "What is your firstname: "
read f_name
if func_name $f_name
then
break
else
err_name $f_name
fi
done

###################Script End#########################

<pre name="code" class="plain">#!/bin/sh
#number file ###给参数文件标出行号的函数###
number_file()
{
_filename=$1
if [ $# -ne 1 ];
then echo "I need a filename to number"
return 1
fi
loop=1
while read line
do
echo "$loop: $line"
loop=`expr $loop + 1`
done<$_filename
}
echo -n "Please input the file name you want to number: "
read file_name
number_file $file_name


#!/bin/sh
#String to Upper ###将字符串转换为大写的函数###
str_upper()
{
_str=$1
if [ $# -ne 1 ];
then
echo "Need a string to convert please"
return 1
fi
echo $@|tr '[a-z]' '[A-Z]'
}
echo "Please Input a string you want to convert: "
read string
str_upper "$string"


3, 创建屏幕输出
#!/bin/sh
#cup ###坐标函数###
xy()
{
_r=$1
_c=$2
tput cup $_r $_c
}
clear
xy 1 5
echo -n "Enter your name: "
read name
xy 2 5
echo -n "Enter your age: "
read age


<pre name="code" class="plain">#!/bin/sh
#OUTPUT ###使用颜色,坐标,居中等函数的屏幕输出###
tput init
MYDATE=`date +%D`
colour()
{
case $1 in
black_green)
echo -e "\033[40;32m"
;;
black_yellow)
echo -e "\033[40;33m"
;;
black_white)
echo -e "\033[40;37m"
;;
black_cyan)
echo -e "\033[40;36m"
;;
black_red)
echo -e "\033[40;31m"
;;
esac
}
#######color function################
xy()
{
_r=$1
_c=$2
_text=$3
tput cup $_r $_c
echo -n $_text
}
#######cup function##################
center()
{
_str=$1
_row=$2
strlength=`echo $_str | wc -c`
COLS=`tput cols`
HOLD_COL=`expr $COLS - $strlength`
NEW_COL=`expr $HOLD_COL / 2`
tput cup $_row $NEW_COL
echo -n $_str
}
########center function################
tput clear
colour black_yellow
xy 2 3 "USER: $LOGNAME"
colour black_cyan
center "THE BOOK OF PERSONAL INFORMATION DETAILS" 3
center "-----------------------------------------------" 4

colour black_yellow
xy 5 1 "_______________________________________________________________________________________________________________________________________________________________________________________________"
xy 7 1 "_______________________________________________________________________________________________________________________________________________________________________________________________"
xy 21 1 "_______________________________________________________________________________________________________________________________________________________________________________________________"
center "DATE: $MYDATE" 22
xy 23 1 "_______________________________________________________________________________________________________________________________________________________________________________________________"

colour black_green
xy 6 6 "NAME: "
read name
xy 8 14
echo -n "Phone Number is: "
read number
xy 10 14
echo -n "Home Address is: "
read address
xy 12 14
echo -n "Postal Number: "
read postal

colour black_red
center "Save this record [Y..N]: " 18
read ans
case $ans in
Y|y)
center "This record has been saved!" 19
;;
N|n)
center "This record has been discarded!" 19
;;
*)
center "Please input vaild value [Y..N]" >&2 19
;;
esac

echo -e "\n\n\n\n\n\n"
colour black_white




4, Shell工具

#!/bin/sh
#trap <CTRL-C> signal ###捕捉中断信号###
trap "p_exit" 2
loop=0
p_exit()
{
echo "you just hit <CTRL-C>"
echo "I will now exit"
exit 1
}
while :
do
loop=`expr $loop + 1`
echo $loop
done

5, 脚本案例
#!/bin/sh
#ping all ###ping hosts文件中包含的所有主机地址
cat /scratch/ziwzhang/Documents/File_Test/hosts| grep -v '^#' | while read line
do
addr=`awk '{print $1}'`
for host in $addr
do
ping -s -c1 $host
done
done