当前位置:网站首页>shell---函数
shell---函数
2022-07-28 05:29:00 【小白啥都要懂】
1.编写函数,实现打印绿色OK和红色FAILED,判断是否有参数,存在为OK,不存在为FAILED;
脚本编写如下:
[[email protected] ~]# cat color.sh
#!/bin/bash
##############################################################
# File Name: color.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-18 14:50:00
# Description:
##############################################################
color()
{
if [ ! -z "$1" ];then
echo -e "\033[32m ok \033[0m"
else
echo -e "\033[31m FAILED \033[0m"
fi
}
color $1执行结果如下:
[[email protected] ~]# ./color.sh a
ok // 这里shell中显示是绿色的
[[email protected] ~]# ./color.sh
FAILED // 这里shell中显示是红色的#:字体颜色如何打印:

2.编写函数,实现判断是否无位置参数,如无参数,提示错误
[[email protected] ~]# cat wc.sh
#!/bin/bash
##############################################################
# File Name: wc.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-18 15:25:28
# Description:
##############################################################
weican()
{
if [ $# -eq 0 ];then
echo "error"
else
echo "right"
fi
}
weican $1执行结果如下:
[[email protected] ~]# chmod 755 wc.sh
[[email protected] ~]# ./wc.sh
error
[[email protected] ~]# ./wc.sh a
right
[[email protected] ~]##:位置参数:“所谓的位置参数便是 0,1,2,3,4,5,6,7,8,9...。使用时,用$0,$1,$2...。 位置参数是当 script 被载入时,後面所附加的参数。
3.编写函数实现两个数字作为参数,返回最大值
[[email protected] ~]# cat max.sh
#!/bin/bash
##############################################################
# File Name: max.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-18 15:38:14
# Description:
##############################################################
read -p "please input:" num1 num2
max_b()
{
if [ $num1 -gt $num2 ];then
echo "max is $num1"
elif [ $num1 -lt $num2 ];then
echo "max is $num2"
else
echo "$num1 equals $num2"
fi
}
max_b $num1 $num2结果如下所示:
[[email protected] ~]# chmod 755 max.sh
[[email protected] ~]# ./max.sh
please input:12 23
max is 234.编写函数,实现两个整数位参数,计算加减乘除。
[[email protected] ~]# cat js.sh
#!/bin/bash
##############################################################
# File Name: js.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-18 15:48:56
# Description:
##############################################################
read -p "please input your number:" num1 num2
cu()
{
expr $num1 + $num2 &> /dev/null
if [ $? -ne 0 ];then
echo "please input two interger numbers"
else
echo "$num1+$num2=$((num1+num2))"
echo "$num1-$num2=$((num1-num2))"
echo "$num1*$num2=$((num1*num2))"
echo "$num1/$num2=$((num1/num2))"
fi
}
cu $num1 $num2执行结果如下:
[[email protected] ~]# ./js.sh
please input your number:8 2
8+2=10
8-2=6
8*2=16
8/2=4
[[email protected] ~]# ./js.sh
please input your number:asd
please input two interger numbers
[[email protected] ~]# ./js.sh
please input your number:1
please input two interger numbers
[[email protected] ~]#5.将/etc/shadow文件的每一行作为元数赋值给数组
[[email protected] ~]# cat au.sh
#!/bin/bash
##############################################################
# File Name: au.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-18 16:06:22
# Description:
##############################################################
declare -a array
i=0
while read input
do
array[$i]=$input
echo ${array[$i]}
let i++
done < /etc/passwd执行结果如下:
[[email protected] ~]# chmod 755 au.sh
[[email protected] ~]# ./au.sh
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
6.使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量
[[email protected] ~]# cat gs.sh
declare -A shells
while read input
do
type=`echo $input | awk -F: '{print $NF}'`
let shells[$type]++
done < /etc/passwd
for i in ${!shells[*]}
do
echo "$i : ${shells[$i]}"
done执行结果如下所示:
[[email protected] ~]# chmod 755 gs.sh
[[email protected] ~]# ./gs.sh
/sbin/nologin : 22
/bin/sync : 1
/bin/bash : 4
/sbin/shutdown : 1
/sbin/halt : 17.使用关联数组按扩展名统计指定目录中文件的数量
declare -A array
num=`find /root/shell/test* -type d | wc -l`
for ((i=0;i<$num;i++))
do
type=`find /root/shell/test* -type d | head -$[$i+1] | tail -1`
array[$type]=`find $type -type f | wc -l`
done
for i in ${array[*]}
do
echo "$i : ${array[$i]}"
done
边栏推荐
- Clock tree analysis example
- Small turtle C (Chapter 6 arrays 1 and 2)
- win下安装nessus
- Si Han talks about the professional development of testers
- 爬虫学习总结
- What is a linear table?
- ES6 add -- > object
- 360 compatibility issues
- Pictures are adaptive to the screen
- Neo4j运行报错Error occurred during initialization of VM Incompatible minimum and maximum heap sizes spec
猜你喜欢

静态和浮动路由

DHCP service

MOOC Weng Kai C language week 6: arrays and functions: 1. Arrays 2. Definition and use of functions 3. Parameters and variables of functions 4. Two dimensional arrays

Applet creation component

RAID磁盘阵列

Shell script - "three swordsmen" awk command

Applets: lifecycle

Applet navigator cannot jump (debug)

三层交换和VRRP

Use powercli to create a custom esxi ISO image
随机推荐
DOM window related data, operations & BOM operations
Test interview questions collection (II) | test tools (with answers)
DHCP service
DOM operation cases
shell脚本——正则表达式
Es6--- > arrow function, class, modularization
Icc2 analysis timing artifact analyze_ design_ violations
[learning notes] tool
RAID disk array
MOOC Weng Kai C language week 5: 1. cycle control 2. multiple cycles 3. cycle application
raid磁盘阵列
SySeVR环境配置:joern-0.3.1、Neo4j-2.1.5、py2neo2.0
MOOC翁恺C语言 第四周:进一步的判断与循环:3.多路分支4.循环的例子5.判断和循环常见的错误
Custom components -- styles
VLAN的配置
[learning records of erudite Valley] Super summary, attentive sharing | collection
Difference between process and thread
DNS域名解析服务
MOOC Weng Kai C language week 3: judgment and cycle: 1. Judgment
VSphere esxi 7.0 update 3 release notes