当前位置:网站首页>Shell - Chapter 6 exercise
Shell - Chapter 6 exercise
2022-07-25 11:23:00 【weixin_ fifty-one million eight hundred and eight thousand and 】
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
[[email protected] zuoye]# vim 6.1.sh
#!/bin/bash
sy() {
if [ $# -eq 0 ];then
echo -e "\033[32m FAILED \033[0m"
else
echo -e "\033[31m OK \033[0m"
fi
}
sy $1
~
2、 Write function , To judge whether there is no position parameter , If there are no parameters , Prompt error
[[email protected] zuoye]# vim 6.2.sh
#!/bin/bash
sy() {
if [ $# -eq 0 ];then
echo -e "\033[32m FAILED \033[0m"
else
echo -e "\033[31m OK \033[0m"
fi
}
sy $1
3、 Write a function to implement two numbers as parameters , Return maximum
[[email protected] zuoye]# vim 6.3.sh
#!/bin/bash
sy() {
if [ $1 -gt $2 ];then
echo $1
else
echo $2
fi
}
read -p "please input two number:" num1 num2
sy $num1 $num2
4、 Write function , Implement two integer digit parameters , Calculate addition, subtraction, multiplication and division .
[[email protected] zuoye]# vim 6.4.sh
#!/bin/bash
sy() {
expr $1 + $2 + 10 &>/dev/null
if [ $? -eq 0 ];then
echo "$1+$2=$(($1+$2))"
echo "$1-$2=$(($1-$2))"
echo "$1*$2=$(($1*$2))"
echo "$1/$2=$(($1/$2))"
else
echo "please input int number"
fi
}
read -p "please input two number:" num1 num2
sy $num1 $num2
5、 take /etc/shadow Each line of the file is assigned to the array as a primitive
[[email protected] zuoye]# vim 6.5.sh
#!/bin/bash
a=(`cat hostip`)
for((i=0;i<${
#a[*]};i++))
do
echo ${
a[$i]}
done
6、 Use associative array statistics file /etc/passwd Different types used by users in shell The number of
#!/bin/bash
declare -A sy
for i in `cut -d: -f7 /etc/passwd`
do
let sy[$i]++
done
for i in ${
!sy[*]}
do
echo "$i ${sy[$i]}"
done
7、 Use the associative array to count the number of files in the specified directory by extension
[[email protected] zuoye]# vim 6.7.sh
#!/bin/bash
read -p "pash: " pash
declare -A sy
for i in ` ls |fgrep . |awk -F. {'print $NF'}`
do
let sy[$i]++
done
for i in ${
!sy[*]}
do
echo "$i ${sy[$i]}"
done
边栏推荐
- Let sports happen naturally, and fire creates a new lifestyle
- SQL语言(二)
- Compressed list ziplist of redis
- 同事看了我的代码惊呼:居然是这么在Unity中用单例的
- [flask advanced] solve the classic error reporting of flask by combining the source code: working outside of application context
- Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
- BeautifulSoup的一些用法
- 玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
- [cloud enjoys freshness] community weekly · Vol 72 - the first opening ceremony of the 2022 Huawei developer competition in China was launched; Huawei cloud koomessage is in hot public beta
- Redis之压缩列表ziplist
猜你喜欢
随机推荐
C# Newtonsoft.Json 高级用法
BeautifulSoup的一些用法
A troubleshooting record of DirectShow playback problems
shell-第六章练习
从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计
syncronized锁升级的过程
Dataframe print 省略号问题
Learn Luzhi PHP -- tp5.0 uses Chinese as an alias and reports "unsupported data expression"
Only know that the preform is used to generate objects? See how I use unity to generate UI prefabs
同事看了我的代码惊呼:居然是这么在Unity中用单例的
LVS负载均衡之LVS-DR搭建Web群集与LVS结合Keepalived搭建高可用Web群集
[information system project manager] thought map series essence summary
Stm32cubemx learning record -- installation, configuration and use
shell-第四天作业
HCIA experiment (06)
Learn NLP with Transformer (Chapter 5)
Learn NLP with Transformer (Chapter 2)
上周热点回顾(7.18-7.24)
Redis sentry, high availability executor
Motivation of enterprises to practice open source








