当前位置:网站首页>shell 函数和数组练习
shell 函数和数组练习
2022-07-27 06:50:00 【空丶白灬】
1、编写函数,实现打印绿色OK和红色FAILED
判断是否有参数,存在为Ok,不存在为FAILED
#!/bin/bash
func()
{
echo $#
if [ -n "$1" ];then #-n 后的参数需要" "
echo -e "\033[32m OK \033[0m"
else
echo -e "\033[31m failed \033[0m"
fi
}
func $1
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
#!/bin/bash
func()
{
echo $#
if [ -n "$1" ];then #-n 后的参数需要" "
echo "can shu wei $1"
else
echo -e "error"
fi
}
func $1
3、编写函数实现两个数字做为参数,返回最大值
#!/bin/bash
compare()
{
if [ $1 -gt $2 ] ; then
return $1
elif [ $1 -lt $2 ] ; then
return $2
else
echo "same"
fi
}
compare $1 $2
echo "$?"
4、编写函数,实现两个整数位参数,计算加减乘除。
#!/bin/bash
calculator()
{
if expr $1 + $2 >/dev/null ; then
echo "$1+$2= $(($1+$2))"
echo "$1-$2= $(($1-$2))"
echo "$1*$2= $(($1*$2))"
echo "$1/$2= $(($1/$2))"
else
echo "not zhengshu"
fi
}
calculator $1 $2
5、将/etc/shadow文件的每一行作为元数赋值给数组
#!/bin/bash
user=($(cat /etc/shadow))
for ((i=0;i<${#user[*]};i++));
do
echo ${user[$i]}
done
6、使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量
#!/bin/bash
declare -A a
num=`wc -l </etc/passwd`
for ((i=1;i<=$num;i++));
do
b[$((i-1))]=$(head -$i /etc/passwd | tail -1 )
done
for ((i=0;i<${#b[*]};i++));
do
type=`echo ${
b[$i]} | awk -F: '{print $NF}'`
let a[$type]++
done
for i in ${
!a[*]}
do
echo "$i : ${a[$i]}"
done
#2
for i in `cut -d: -f7 /etc/passwd
do
let a[$i]++
done
for i in ${
!a[*]}
do
echo "$i : ${a[$i]}"
done
7、使用关联数组按扩展名统计指定目录中文件的数量
#!/bin/bash
declare -A a
b=(`ls -l /shell/test | awk '{print $NF}'`)
for ((i=0;i<${#b[*]};i++));
do
type=`echo ${
b[$i]}|grep [a-z] | awk -F. '{print $NF}'`
let a[$type]++
done
for i in ${
!a[*]}
do
echo "$i : ${a[$i]}"
done
边栏推荐
- 2022 0726 Gu Yujia's study notes
- Am I delayed by the code... Unfortunately, I became a programmer
- STM32_ Find the cause of entering hardfault_ Handler's function
- QT连接sqlite数据库的错误及其修改办法
- Using soci to connect Oracle with PostgreSQL and SQLite on rhel8
- View the dmesg log before server restart
- flink1.14 sql基础语法(一) flink sql表查询详解
- (2022 Hangdian multi school III) 1011.taxi (Manhattan maximum + 2 points)
- Using loops to process data in tables in kettle
- Array method and loop in JS
猜你喜欢

JS regular expression implementation adds a comma to every three digits

杂谈:跟女儿聊为啥要学好文化课

漏风的小棉袄……

Regular expression foundation sorting

Temperature and humidity measurement and display device based on Arduino

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

C语言实现猜数字小游戏项目实战(基于srand函数、rand函数,Switch语句、while循环、if条件判据等)

基于Arduino的温度、湿度测量显示装置

TCP/IP协议分析(TCP/IP三次握手&四次挥手+OSI&TCP/IP模型)

ARP broadcasting practice cases
随机推荐
Analysis of memory structure of C program code
Okaleido ecological core equity Oka, all in fusion mining mode
(2022 Niuke multi school III) a-ancestor (LCA)
用户解锁SM04 SM12
Drconv pytorch is changed to the same size of output and input
Panabit SNMP configuration
Want to sink into redis hash and write in the attributes and values of the object. Do the bosses have a demo?
单元测试系统化讲解之Mockito
Port forwarding summary
Chapter 6 Shell Logic and Arithmetic
Essay: college entrance examination
User unlock sm04 sm12
Error when connecting to MySQL: public key retrieval is not allowed [solution]
在kettle使用循环来处理表中的数据
Cadence (XI) silk screen printing adjustment and subsequent matters
(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)
在mysql中同时使用left join on 和where 的查询结果分析
A priority SQL problem
Top ten interview questions for software testing (with answers and analysis)
Zabbix: 将收集到值映射为易读的语句