当前位置:网站首页>函数和数组
函数和数组
2022-07-29 09:43:00 【m0_58963135】
[[email protected] test]# echo -e "\033[31m FAILED \033[0m"
FAILED
[[email protected] test]# echo -e "\033[32m ok \033[0m"
ok
#!/bin/bash
read -p "please input arg:" arg
color() {
if [ $# -ne 0 ];then
echo -e "\033[31m OK \033[0m"
else
echo -e "\033[32m FAILED \033[0m"
fi
}
color $arg
[[email protected] test]# chmod a+x color.sh
[[email protected] test]# ./color.sh
please input arg:
FAILED
[[email protected] test]# ./color.sh
please input arg:1
OK
#!/bin/bash
read -p "please input arg:" arg
func() {
if [ $# -ne 0 ];then
echo $arg
else
echo "错误"
fi
}
func $arg
[[email protected] test]# chmod a+x 6.2.sh
[[email protected] test]# ./6.2.sh
please input arg:1
1
[[email protected] test]# ./6.2.sh
please input arg:
错误
[[email protected] test]# vim 6.3.sh
#!/bin/bash
read -p "please input two num:" a b
func() {
if [ $a -gt $b ];then
echo "最大值为$a"
elif [ $b -gt $a ];then
echo "最大值为$b"
else
echo "相等"
fi
}
func $a $b
[[email protected] test]# chmod a+x 6.3.sh
[[email protected] test]# ./6.3.sh
please input two num:5 8
最大值为8
[[email protected] test]# ./6.3.sh
please input two num:9 3
最大值为9
[[email protected] test]# ./6.3.sh
please input two num:6 6
相等
[[email protected] test]# vim 6.4.sh
#!/bin/bash
read -p "please input two num:" a b
func () {
[ $# -ne 2 ] && {
echo "usage: $0 num1 num2"
exit 1
}
expr $a + $b &> /dev/null
if [ $? -ne 0 ]
then
echo "you must input two number"
exit 2
fi
echo "$a+$b=$(($a+$b))"
echo "$a-$b=$(($a-$b))"
echo "$a*$b=$(($a*$b))"
echo "$a/$b=$(($a/$b))"
}
func $a $b
[[email protected] test]# chmod a+x 6.4.sh
[[email protected] test]# ./6.4.sh
please input two num:3 4
3+4=7
3-4=-1
3*4=12
3/4=0
[[email protected] test]# ./6.4.sh
please input two num:w r
you must input two number
[[email protected] test]# vim 6.5.sh
#!/bin/bash
declare -a array
i=0
while read line
do
array[$i]=$line
echo ${array[$i]}
let i++
done < /etc/shadow
[[email protected] test]# chmod a+x 6.5.sh
[[email protected] test]# vim 6.6_7.sh
#!/bin/bash
declare -A array
for i in `cut -d: -f 7 /etc/passwd`
do
let array[$i]++
done
for i in ${!array[*]}
do
echo "$i ${array[$i]}"
done
[[email protected] test]# chmod a+x 6.6_7.sh
[[email protected] test]# ./6.6_7.sh
/sbin/nologin 35
/bin/bash 23
/sbin/halt 1
/bin/sync 1
/sbin/shutdown 1
[[email protected] test]# vim 6.7_1.sh
#!/bin/bash
read -p "please iuput dir:" d
declare -A array
for i in `ls -F $d | grep -v /$ | fgrep . | cut -d . -f 2`
do
let array[$i]++
done
for i in ${!array[*]}
do
echo "$i ${array[$i]}"
done
[[email protected] test]# chmod a+x 6.7_1.sh
[[email protected] test]# ./6.7_1.sh
please iuput dir:/root/
cfg 1
sh 1
边栏推荐
- 系统架构师学习
- [Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed
- 怎么样的框架对于开发者是友好的?
- div水平布局两边对齐
- Custom configuration
- 高智伟:数据管理赋能交通行业数字化转型
- Opencv introductory basic learning
- Data type of MySQL
- User identity identification and account system practice
- Youboxun, the gold donor of the open atom open source foundation, joined hands with partners to help openharmony break the circle!
猜你喜欢
引入redis缓存出现的问题以及解决方式
综合设计一个OPPE主页--页面的底部
Anfulai embedded weekly report no. 273: 2022.07.04--2022.07.10
Window系统操作技巧汇总
MySQL事务与MVCC如何实现的隔离级别
Unity 引导系统.点击目标物体后提示文字变色进入下一步
Why does the system we developed have concurrent bugs? What is the root cause of concurrent bugs?
高智伟:数据管理赋能交通行业数字化转型
Vector implementation
A little knowledge ~ miscellaneous notes on topics ~ a polymorphic problem
随机推荐
redis命令[逐渐完善]
Div horizontal arrangement
Harmonyos 3.0 release!
PyQt5快速开发与实战 6.5 QGridLayout(网格布局)
Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
HarmonyOS 3.0 发布!
Logistic regression of machine learning
【微信小程序】接口生成自定义首页二维码
附录2-一些简单的练习
Source code analysis of senparc.weixin.sample.mp
Redis command [gradually improved]
C# 值类型和引用类型讲解
Basic operations of OpenCV image processing
智慧解决问题
pytest+allure生成测试报告
node(二)
Solve the problem of reading data garbled by redis visualization tool
Summary of introduction to unityshader (2): Beginner Level
待人宽容大度
Anfulai embedded weekly report no. 273: 2022.07.04--2022.07.10