1.online写一个脚本,判定给定的IP列表中的主机哪些在线
function online(){
if ping -c 1 $1 &> /dev/null
then
echo "$1 is online"
else
echo "$1 is not online"
fi
}
online 192.168.111.$1
[root@localhost 20220824]# bash func_online.sh 100
192.168.111.100 is not online[root@localhost 20220824]# bash func_online.sh 134
192.168.111.134 is 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 is not exist"
return 1
fi
}
read -p "please input username:" username
until [ "$username" = "quit" -o "$username" = "exit" -o "$username" = "q" ]
do
user $username
if [ $? -eq 0 ];then
read -p "please input username again:" username
else
read -p "$username is not exit,please input other username:" username
fi
done
[root@localhost 20220824]# bash func_users.sh
please input username:redhat
1000:/bin/bash
please input username again:xzl
1001:/bin/bash
please input username:jkl
jkl is not exist
jkl is not exit,please input other username:q
3.函数库文件:在一个脚本中调用另一个脚本中的函数
func_file1.sh:
test1 (){
echo "i am func_lib"
}
func_file2.sh:
. func_file1.sh
test1
结果:
[root@localhost 20220824]# bash func_file2.sh
i am func_lib
4.利用递归求n的阶乘
fact (){
num=$1
if [ $num -eq 1 ]
then
echo "1"
else
data=$[$num*$(fact $((num-1)))]
echo $data
fi
}
read -p "please input number:" num
fact $num
[root@localhost 20220824]# bash factorial.sh
please input number:1
1
[root@localhost 20220824]# bash factorial.sh 5
please input number:5
120
原文地址:https://www.jb51.cc/wenti/3280902.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。