1.online写一个脚本,判定给定的IP列表中的主机哪些在线
方式一:使用函数传参(有参数,无返回值)
online() {
if ping -c 1 $1 &>/dev/null
then
echo "$1 is up"
else
echo "$1 is down"
fi
}
for i in {120..150}
do
online 172.16.200.$i
done
方式二:直接使用函数实现(无参数,无返回值)
online(){
for i in {128..200};do
if ping -c 1 172.25.254.$i $>/dev/null
then
echo "172.25.254.$i is up"
else
echo "172.25.254.$i is down"
fi
done
}
online
2.函数能够接受一个参数,参数为用户名;判断一个用户是否存在
如果存在,就返回此用户的shell和UID;并返回正常状态值;
如果不存在,就说此用户不存在;并返回错误状态值;
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
3.函数库文件:在一个脚本中调用另一个脚本中的函数
使用 . filename来调用
eg:. msd.sh
4.利用递归求n的阶乘
f()
{
local n="$1"
if [ "$n" -eq 0 ]
then
result=1
else
let "m=n-1"
fact "$m"
let "result=$n * $result"
fi
}
f "$1"
echo "Factorial of $1 is $result"