当前位置:网站首页>第四天作业
第四天作业
2022-07-26 08:12:00 【三俩两】
1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为Ok,不存在为FAILED
echo -e "\033[32m OK \033[0m"
echo -e "\033[31m Failed \033[0m"
vim 1.sh
#!/bin/bash
fun (){
if [ $# -eq 0 ]
then
echo "Failed"
else
echo "OK"
fi
}
read -p "please input :" i
fun $i
chmod a+x 1.sh
bash 1.sh
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
vim 2.sh
#!/bin/bash
fun (){
if [ $# -ne 0 ]
then
echo " parameters exist"
else
echo "error"
fi
}
read -p "please input parameter:" i
fun $i
chmod a+x 2.sh
bash 2.sh
3、编写函数实现两个数字做为参数,返回最大值
vim 3.sh
#!/bin/bash
read -p "please input two number:" num1 num2
fun (){
if [ $num1 -gt $num2 ]
then
echo "max is $num1"
elif [ $num1 -lt $num2 ]
then
echo "max is $num2"
else
echo "the number are equal"
fi
}
fun $num1 $num2
chmod a+x 3.sh
bash 3.sh
4、编写函数,实现两个整数位参数,计算加减乘除。
vim 4.sh
#!/bin/bash
read -p "please input two number:" num1 num2
fun (){
expr $num1+$mun2 >&/dev/null
if [ $# -ne 0 ]
then
echo "$num1+$num2=$[$num1+$num2]"
echo "$num1-$num2=$[$num1-$num2]"
echo "$num1*$num2=$[$num1*$num2]"
echo "$num1/$num2=$[$num1/$num2]"
else
echo "is not an integer, please re-enter"
fi
}
fun $num1 $num2
chmod a+x 4.sh
bash 4.sh
5、将/etc/shadow文件的每一行作为元数赋值给数组
vim 5.sh
#!/bin/bash
num=`wc -l </etc/shadow`
for i in `seq $num`
do
array[$i]=`head $i /etc/shadow | tail -l`
done
for i in ${array[*]}
do
echo $i
done
chmod a+x 5.sh
bash 5.sh
6、使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量
vim 6.sh
#!/bin/bash
declare -A array
for i in `cut -d: -f 7 /etc/passwd`
do
let array[$i]++
done
#echo ${array[*]}
#echo ${!array[*]}
for i in ${
!array[*]}
do
echo "$i: ${array[$i]}"
done
chmod a+x 6.sh
bash 6.sh
7、使用关联数组按扩展名统计指定目录中文件的数量
vim 7.sh
#!/bin/bash
read -p "pleace input filename:" filename
declare -A array
for i in `ls $filename | fgrep '.' | cut -d . -f 2`
do
let array[$i]++
done
for i in ${
!array[*]}
do
echo "$i: ${array[$i]}"
done
chmod a+x 7.sh
bash 7.sh
边栏推荐
- The difference between LinkedList and ArrayList
- 万字长文 | 深入理解 OpenFeign 的架构原理
- Sort sort IP addresses
- [xshell7 free download and installation]
- 2022/7/12 exam summary
- OSPF总结
- Stm8 official library file download
- Read and write of zip file
- Why is Google's internal tools not suitable for you?
- Matlab drawing black spots on two / three-dimensional drawings
猜你喜欢

一键部署LAMP和LNMP架构

Burp suite Chapter 4 advanced options for SSL and proxy

Burp Suite - Chapter 2 burp suite proxy and browser settings

全网最全:Mysql六种约束详解

R language foundation

JSP implicit object servlet object

Web side 3D visualization engine hoops communicator reads 10g super large model test | digital twin Technology

万字长文 | 深入理解 OpenFeign 的架构原理
分享高压超低噪声LDO测试结果(High Voltage Ultra-low Noise LDO)

JSP built-in object (implicit object)
随机推荐
2022/7/7 exam summary
2022/7/1
The first ide overlord in the universe, replaced...
Copy pcap file with producer consumer model
Common database commands (special for review)
If the thread crashes, why doesn't it cause the JVM to crash? What about the main thread?
The difference between overloading and rewriting
Libevent custom event (asynchronous)
2022/7/17 exam summary
JSP implicit object servlet object
Enterprise private network construction and operation and maintenance
"Door lock" ignites a heated discussion on the safety of living alone. The new poster picture is suffocating
要不你给我说说什么是长轮询吧?
Exam summary on June 27, 2022
The bigger the project is, the bigger it is. This is how I split it
Burp Suite-第二章 Burp Suite代理和浏览器设置
小组成员参加2022中国多媒体大会
Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合
BGP选路原则
table 固定特定行