一:online写一个脚本,判定给定的IP列表中的主机哪些在线
[root@shell test]
online( ) {
if ping - c 1 $1 &> / dev/null
then
echo "$1 is up"
else
echo "$1 is down "
fi
}
for i in { 200. . 254}
do
online 192. 168. 0. $i
done
[root@shell test]
192. 168. 0. 200 is down
192. 168. 0. 201 is down
192. 168. 0. 202 is down
. . . . . .
二:函数能够接受一个参数,参数为用户名;判断一个用户是否存在如果存在,就返回此用户的shell和UID;并返回正常状态值;如果不存在,就说此用户不存在;并返回错误状态值
[root@localhost test6]
user( ) {
if id $1 &>/ dev/null
then
echo "`grep ^$1 /etc/passwd | cut -d: -f3,7`"
return 0
else
echo " $1 does not exist"
return 1
fi
}
read -p " please input username:" username
until [ " $username " = " quit" -o " $username " = " exit " -o " $username " = " q" ]
do
user $username
if [ $? == 0 ];then
read -p " please input again:" username
else
read -p " no $username , please input again:" username
fi
done
[root@localhost test6]
please input username:redhat
1000:/ bin/bash
please input again:jkl
jkl does not exist
no jkl, please input again:exi
三:函数库文件:在一个脚本中调用另一个脚本中的函数—main.sh编写九九乘法表 使用sub.shj解释器
[root@shell test]
for i in `seq 9`
do
for j in `seq 9`
do
[ $j -le $i ] && echo - n "$i *$j = `echo $( ( $i * $j ) )` "
done
echo " "
done
[root@shell test]
1*1= 1
2*1= 2 2*2= 4
3*1= 3 3*2= 6 3*3= 9
4*1= 4 4*2= 8 4*3= 12 4*4= 16
5*1= 5 5*2= 10 5*3= 15 5*4= 20 5*5= 25
6*1= 6 6*2= 12 6*3= 18 6*4= 24 6*5= 30 6*6= 36
7*1= 7 7*2= 14 7*3= 21 7*4= 28 7*5= 35 7*6= 42 7*7= 49
8*1= 8 8*2= 16 8*3= 24 8*4= 32 8*5= 40 8*6= 48 8*7= 56 8*8= 64
9*1= 9 9*2= 18 9*3= 27 9*4= 36 9*5= 45 9*6= 54 9*7= 63 9*8= 72 9*9= 81
sub. sh
四:利用递归求n的阶乘
1
9 fact( )
10 {
11 local n="$1 "
12 if [ "$n " -eq 0 ]
13 then
14 result=1
15 else
16 let "m=n-1"
17 fact "$m "
18 let "result=$n * $result "
19 fi
20 }
21 fact "$1 "
22 echo "Factorial of $1 is $result "