函数与数组shell

简介: 函数与数组shell

函数与数组

定义函数 命令的集合,用来完成特定的功能; 提前定义函数,在脚本中任意调用函数名。 使用函数使代码模块化,便于重复使用,增加可读性。

函数定义格式:
 函数名(){
     shell命令
 }
 function 函数名 {
     shell命令
 }
 #定义menu函数内容
 menu(){
 #定义菜单时,应贴着左侧写
 cat << END
 1.创建用户
 2.删除用户
 3.退出程序
 END
 }
 #调用menu
 menu
 menu(){
 cat << END
 1.创建用户
 2.删除用户
 3.退出程序
 END
 }
 input(){
         while true
         do
                 read -p "请输入用户前缀:" name
                 if [[ $name =~ [0-9]+ ]];then
                         echo "前缀不能包含数字"
                         continue
                 fi
                 break
         done
         while true
         do
                 read -p "请输入用户数量" num
                 if [[ $num =~ ^[^0-9]+$ ]];then
                         echo "数量应为数字"
                         continue
                 fi
                 break
         done
 }
 function create {
 input
         read -p "将创建${name}1~${name}${num} {y/n}" m
         case ${m} in
         y)
                 for i in $(seq $num)
                 do
                         user=${name}${i}
                         id ${user} &> /dev/null
                         if [ $? -eq 0 ];then
                                 echo "用户:${user}已存在"
                         else
                         useradd ${user} &> /dev/null
                         pass=$(echo $((RANDOM))|md5sum|cut -c 2-10)
                         echo ${pass}|passwd --stdin ${user} &> /dev/null
                         echo "用户名:${user} 密码:${pass}" >> /tmp/user.txt
                         echo "${user}创建成功"
                         fi
                         sleep 1
                 done
                 ;;
         n)
                 ;;
         *)
                 echo "输入无效应为 y|n"
                 create
                 ;;
         esac
 }
 while true
 do
 menu
 read -p "请对菜单进行选择(1-3)" y
 case $y in
 1)
         create
         ;;
 2)
         echo 删除用户
         ;;
 3)
         ;;
 *)
         echo "选择无效,请选择 1|2|3"
         ;;
 esac
 done

小型计算器

 fun1(){
 case $2 in
 +)
         echo "$1 + $3=$(( $1 + $3 ))"
         ;;
 -)
         echo "$1 - $3=$(( $1 - $3 ))"
         ;;
 x)
         echo " $1 * $3=$(( $1 * $3 ))"
         ;;
 /)
         echo " $1 / $3=$(( $1 / $3 ))"
         ;;
 *)
         echo "只可运算 +|-|x|/"
         ;;
 esac
 }
 fun1 $1 $2 $3

echo返回任何字符串的结果,用于返回数据

return只能返回1-255 通常用来表示状态,0为成功1为失败

 fun1(){
 read -p "请输入文件名" name
 if [ -f ${name} ];then
         return 0
 else
         return 1
 fi
 }
 fun1
 if [ $? -eq 0 ];then
         echo " $name 已存在"
 else
         echo " $name 未找到"
 fi
 ***
 pid=$$
 fun1(){
         ps -ef |grep nginx |grep -v grep |grep -v $pid &> /dev/null
 if [ $? -eq 0 ];then
         return 0
 else
         return 1
 fi
 }
 fun1
 if [ $? -eq 0 ];then
         echo " nginx 运行中"
 else
         echo " nginx 未运行"
 fi
判断nginx是否在运行中
 pid=$$
 run(){
         ps -elf |grep nginx|grep -v $pid &> /dev/null
         if [ $? -eq 0 ];then
                 return 0
         else
                 return 1
         fi
 }
 run && echo "nginx已经运行" || echo "nginx已关闭"
 function fun1
 {
         read -p "请输入一个数字" num
         if [ $num -ge 0 -a $num -lt 10 ];
         then
                 return 0
         elif [ $num -ge 10 -a $num -lt 20 ];
         then
                 return 1
         elif [ $num -ge 20 -a $num -lt 30 ];
         then
                 return 2
         else
                 return 3
         fi
 }
 fun1
 echo $?

列出系统中所有用户

 cat /etc/passwd|cut -d: -f 1    #-d 指定分割符 -f 指定列
 function get
 {
         list=$( cat /etc/passwd |cut -d: -f 1)
         echo $list
 }
 users=$(get)
 n=1
 for i in $users
 do
         echo "第${n}个用户 $i"
         let n++
 done

库函数

 function chk_user
 {
         if [ $UID -eq 0 ];then
                 return 0
         else
                 return 1
         fi
 }
 source /sh/function1
 chk_user
 if [ $? -ne 0 ];then
         echo "非管理员无法调用此程序"
 fi
 case $2 in
 +)
         result=$(add $1 $3)
         echo " $1 + $3 = $result"
         ;;
 -)
         result=$(reduce $1 $3)
         echo " $1 - $3 = $result"
         ;;
 x)
         result=$(multiple $1 $3)
         echo " $1 x $3 = $result"
         ;;
 /)
         result=$(dev $1 $3)
         echo " $1 / $3 = $result"
         ;;
 *)
         echo "无效"
         ;;
 esac
数组
可以看成变量,变量一般只能存储一个值,数组可以存储多个值
普通数组:使用数字做索引
关联数组:是哟个字符串做索引 #索引也叫下标
下标:从0开始,元素个数是下一个要往里存的位置
定义数组
 names=(张三 李四 王五 陈六)     #定义数组,一个空格为隔开一个元素
 echo ${names[2]}    #[]中为要返回的元素的位置
 names[0]=guojing    #当这个数组已存在时,更改位置上的数据
 echo ${names[@]}    #获取数组中所有元素@可换*
 echo ${#names[@]}   #所有元素的总数
 echo ${!names[@]}   #获取组中所有元素的索引下标
 names[${#names[@]}]=xiangshaolong   #利用长度做下标添加元素
 #!/bin/bash
 names=(zhangsan lisi wangwu chenliu)
 index=-1
 function list 
 {
 for ((i=0;i<${#names[@]};i++))
 do
         echo ${names[i]}
 done
 }
 function add
 {
 read -p "请输入用户名:" name
 names[${#names[@]}]=$name
 }
 function find
 {
         read -p "请输入用户名:" name
         for ((i=0;i<${#names[@]};i++))
         do
                 if [ ${names[$i]} == $name ];then
                         index=$i
                         break
                 fi
         done
         echo "${index}"
 }
 function chage
 {
         i=$(find)
         read -p "请输入新的值" new
         names[$i]=${new}
         list
 }
 function del
 {
         i=$(find)
         names[$i]=
         list
 }
 while true
 do
 cat << END
 ----欢迎使用用户管理系统----
 ---------1.添加用户---------
 ---------2.查找用户---------
 ---------3.修改用户---------
 ---------4.删除用户---------
 -----------5.退出-----------
 END
 read -p "请选择操作" n
 case $n in
 1)
         list
         add
         ;;
 2)
         i=$(find)
         if [ $i -ne -1 ];then
                 echo "此用户位于${i}号位"
         else
                 echo "未找到此用户"
         fi
         ;;
 3)
         change
         ;;
 4)
         del
         ;;
 5)
         echo "byebye"
         break
         ;;
 *)
         echo "无效选项"
         ;;
 esac
 done
 echo ${#str}    #返回字符长度
 echo ${str:0:7} #截取str中的零到七号字符

例:输入一串字符,将字符逐个放入数组里并输出

read -p "请输入遗传字符串:" chars
 for ((i=0;i<${#chars};i++))
 do
         array[$i]=${chars:$i:1}
         echo ${array[$i]}
 done

例:输入一些数字,找出最大值与最小值,并求出所有数的总和

 read -a num -p  #定义一个数组num
 read -a nums -p "请输入一个数组:"
 if [ ${#nums[@]} -lt 2 ];then
         echo "数组个数应为两个以上"
 fi
 for i in ${nums[@]}
 do
         if [[ ${i} =~ ^[^0-9]+$ ]];then
                 echo "应为纯数字"
         fi
 done
 max=${nums[0]}
 min=${nums[0]}
 sum=0
 for i in ${nums[@]}
 do
         let sum+=$i #每次i的值累加到sum变量中
         if [ $i -gt $max ];then
                 max=${i}
         fi
         if [ $i -lt $min ];then
                 min=${i}
         fi
 done
 echo "最大值为${max},最小值为${min},累加和为:$sum"

关联数组

declare -A my #声明关联数组
 my([id]=1 [name]=zhangsan [age=21]) #给管理数组赋值
 my["性别"]=男  #给已有关联数组添加值
 echo ${my[id]}  #通过索引(键)得出值
 #!/bin/bash
 declare -A students
 students=([id]=1 [name]=zhangsan [age]=19 [score]=90)
 for i in ${!students[@]}
 do
         echo "$i:${students[$i]}"
 done
#例:观察80接口状态
 #!/bin/bash
 declare -A status
 while :
 do
         list=$(ss -an|grep :80|awk '{print $2}')
         for i in $list
         do
                 let status[$i]++
         done
         for a in ${!status[@]}
         do
                 echo "${a}:${status[$a]}"
         done
         sleep 2
         clear
 done
#例:统计访问次数与访问状态
 #!/bin/bash
 declare -A tcps
 while read line
 do
         ips=$(echo $line|awk '{print $1}')
         status=$(echo $line|awk '{print $9}')
         let tcps[$ips]++
         let tcps[$status]++
 done < /var/log/nginx/access.log
 for i in ${!tcps[@]}
 do
         echo "${i}:${tcps[$i]}"
 done


目录
相关文章
|
6天前
|
Shell 索引
shell脚本入门到实战(四)- 数组
shell脚本入门到实战(四)- 数组
|
6天前
|
Shell
Shell函数
Shell函数
27 1
|
6月前
|
运维 Shell C语言
运维(14)- shell函数
运维(14)- shell函数
29 0
|
7月前
|
Unix Shell Linux
|
6天前
|
Shell 应用服务中间件 nginx
shell学习(七) 【shell 函数】
shell学习(七) 【shell 函数】
13 1
|
6天前
|
人工智能 机器人 Shell
【shell】shell函数操作(有参、无参、有返回值、无返回值)
【shell】shell函数操作(有参、无参、有返回值、无返回值)
|
6天前
|
人工智能 机器人 Shell
【shell】shell数组的操作(定义、索引、长度、获取、删除、修改、拼接)
【shell】shell数组的操作(定义、索引、长度、获取、删除、修改、拼接)
|
5月前
|
存储 Shell
shell函数介绍
shell函数介绍
33 2
|
6天前
|
Shell Linux C语言
Linux中执行Shell的函数(popen,system,exec)介绍:分享一些常用的执行Shell的函数及其相关编程技巧和经验
Linux中执行Shell的函数(popen,system,exec)介绍:分享一些常用的执行Shell的函数及其相关编程技巧和经验
38 0