当前位置:网站首页>函数和数组
函数和数组
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边栏推荐
- [AAAI] attention based spatiotemporal graph convolution network for traffic flow prediction
- Zhongang Mining: four steps for sustainable and healthy development of fluorite industry
- C语言的传参方式(int x)(int *x)(int &x)
- mysql将部分表名统一转换为大写
- How to customize the opportunity closing form in dynamics 365online
- Webassembly 2022 questionnaire results are fresh
- Virtual machines use host graphics cards (Hyper-V and wsl2)
- RTMP supports h265 streaming
- 36. JS动画
- Unity guidance system. Click the target object and prompt the text to change color to enter the next step
猜你喜欢

远程连接windows版本服务器redis的配置文件设置

mysql 数据库 期末复习题库

Summary of introduction to unityshader (2): Beginner Level

View port occupancy

Pytest+allure generate test report

A little knowledge ~ miscellaneous notes on topics ~ a polymorphic problem

Anfulai embedded weekly report no. 273: 2022.07.04--2022.07.10

Why does the system we developed have concurrent bugs? What is the root cause of concurrent bugs?

User identity identification and account system practice

Implementation and verification logic of complex expression input component
随机推荐
div水平布局两边对齐
尹伊:我的学习成长路径
Use cpolar to publish raspberry pie web pages (improvement of cpolar function)
i.MX6ULL驱动开发 | 32 - 手动编写一个虚拟网卡设备
Fluent text editor
How to realize the isolation level between MySQL transactions and mvcc
View port occupancy
PyQt5快速开发与实战 6.1 好软件的三个维度 && 6.2 PyQt5中的布局管理 && 6.3 PyQt5的绝对位置布局
Sample is new and supported from API 8! Come and take it
pytest+allure生成测试报告
Unity Xchart3.0基本用法快速上手
Parameter passing mode of C language (int x) (int *x) (int & x)
MySQL事务与MVCC如何实现的隔离级别
Network security (5)
最新翻译的官方PyTorch简易入门教程(PyTorch1.0版本)
常用的DOS命令[逐渐完善]
Encyclopedia of introduction to machine learning - 2018 "machine learning beginners" official account article summary
Sublime Text3 set different indents for different files
系统架构师学习
核酸扫码登记体验有感(如何提高OCR的文字正确识别率)