online写一个脚本,判定给定的IP列表中的主机哪些在线
#!/bin/bash
############################################################
# File Name: online_script.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-23 09:27:53
# Description:
############################################################
online(){
# for host in $1
# do
ping -c 2 $host &>/dev/null
if [ $? -eq 0 ]
then
echo "the host is active"
else
echo "the host is die"
fi
# done
}
read -p "please input IP table(XXXX.XXXX.XXXX.XXXX): " var_hosts
echo $var_hosts
online $var_hosts
用户判定函数
函数能够接受一个参数,参数为用户名; 判断一个用户是否存在 如果存在,就返回此用户的shell和UID;并返回正常状态值; 如果不存在,就说此用户不存在;并返回错误状态值;
#!/bin/bash
############################################################
# File Name: get_shell.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-23 10:28:36
# Description:
############################################################
get_uid(){
id $1 &> /dev/null
if [ $? -eq 0 ]
then
echo `grep ^$1 /etc/passwd | cut -d ":" -f3,7`
return 0
else
echo "this id is null"
return 1
fi
}
read -p "please input id: " id
until [ "$id" == "q" -o "$id" == "exit" ]
do
get_uid $id
if [ $? -eq 0 ]
then
read -p "please input id again: " id
else
read -p "the $id is null,please input true id: " id
fi
done
利用递归求n的阶乘
#!/bin/bash
############################################################
# File Name: rec_fac.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-23 11:17:10
# Description:
############################################################
rec(){
local n=$1
if [ $n -eq 1 ]
then
result=1
else
let "m=n-1"
rec $m
let "result=$n * $result"
fi
}
rec $1
echo "the fac of $1 is $result"
杨辉三角
#!/bin/bash
############################################################
# File Name: scr_sq.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-23 11:37:27
# Description:
############################################################
triangle(){
MAX=0
flag=true
read -p "Please input a number:" MAX
while [ $flag == "false" ]
do
expr $MAX+0 &> /dev/null
[ $? -eq 0 ] && flag=false || read -p "Please input a interger:"MAX
done
declare -a num
for(( i=0;i<MAX;i++ ));do
for(( j=0;j<=i;j++ ));do
if [ $j -eq 0 -o $i -eq $j ];then
num[$i$j]=1
else
let ii=i-1
let jj=j-1
let num[$i$j]=${num[$ii$jj]}+${num[${ii}${j}]}
fi
done
for(( j=0;j<=i;j++ ));do
echo -e "${num[$i$j]} \t\c"
done
echo
done
}
函数库文件:在一个脚本中调用另一个脚本中的函数
函数库文件定义:
创建一个函数库文件的过程非常类似于编写一个Shell脚本。脚本与库文件之间的唯一区别在于函数库文件通常只包括函数,而脚本中则可以既包括函数和变量的定义,又包括可执行的代码。此处所说的可执行代码,是指位于函数外部的代码,当脚本被载入后,这些代码会立即被执行,毋需另外调用。
函数库文件的调用
当库文件定义好之后,用户就可以在程序中载入库文件,并且调用其中的函数。在Shell中,载入库文件的命令为.,即一个圆点,其语法如下:
. filename /source filename
其中,参数filename表示库文件的名称,必须是一个合法的文件名。库文件可以使用相对路径,也可以使用绝对路径。另外,圆点命令和库文件名之间有一个空格。
#!/bin/bash
############################################################
# File Name: scr_import.sh
# Version: V1.0
# Author: Lc
# Email: lc1225@163.comm
# Organization: https://blog.csdn.net/qq_46777335?type=blog
# Created Time :2022-08-23 11:45:04
# Description:
############################################################
. scr_sq.sh
triangle
本文含有隐藏内容,请 开通VIP 后查看