当前位置:网站首页>Shell--- function
Shell--- function
2022-07-28 07:10:00 【Xiaobai needs to know everything】
1. Write function , Achieve green printing OK And red FAILED, Determine whether there are parameters , Existence as OK, There is no such thing as FAILED;
The script is written as follows :
[[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 $1The results are as follows :
[[email protected] ~]# ./color.sh a
ok // here shell The display in is green
[[email protected] ~]# ./color.sh
FAILED // here shell The display in is red #: How to print font colors :

2. Write function , To judge whether there is no position parameter , If there are no parameters , Prompt error
[[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 $1The results are as follows :
[[email protected] ~]# chmod 755 wc.sh
[[email protected] ~]# ./wc.sh
error
[[email protected] ~]# ./wc.sh a
right
[[email protected] ~]##: Positional arguments :“ The so-called position parameter is 0,1,2,3,4,5,6,7,8,9.... When using , use $0,$1,$2.... The position parameter is when script When loaded , The parameters attached later .
3. Write a function to implement two numbers as parameters , Return maximum
[[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 $num2The results are shown below :
[[email protected] ~]# chmod 755 max.sh
[[email protected] ~]# ./max.sh
please input:12 23
max is 234. Write function , Implement two integer digit parameters , Calculate addition, subtraction, multiplication and division .
[[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 $num2The results are as follows :
[[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. take /etc/shadow Each line of the file is assigned to the array as a primitive
[[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/passwdThe results are as follows :
[[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. Use associative array statistics file /etc/passwd Different types used by users in shell The number of
[[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]}"
doneThe results are shown below :
[[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. Use the associative array to count the number of files in the specified directory by extension
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
边栏推荐
猜你喜欢
随机推荐
Shell--第一天作业
PXE无人值守安装管理
Wangeditor (@4.7.15) - lightweight rich text editor
NAT network address translation
Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)
easypoi一对多,合并单元格,并且根据内容自适应行高
Forward and backward slash notes
Three cache technologies -- localstorage, sessionstorage, cookies
metasploit渗透ms7_010练习
登录进oracle10g的oem,想管理监听程序却总是弹出帐号密码输入页面
DNS domain name resolution
freemarker合并单元格,if、else标签的使用,null、空字符串处理
[learning notes] thread creation
MOOC翁恺C语言 第六周:数组与函数:1.数组2.函数的定义与使用3.函数的参数和变量4.二维数组
Custom component -- pure data field & component life cycle
Operation document tree
Custom component -- data listener
Implementation method of Bert
360 compatibility issues
Custom components -- styles









