当前位置:网站首页>Functions and arrays
Functions and arrays
2022-07-29 09:47:00 【m0_ fifty-eight million nine hundred and sixty-three thousand o】
[[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 " error "
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:
error [[email protected] test]# vim 6.3.sh
#!/bin/bash
read -p "please input two num:" a b
func() {
if [ $a -gt $b ];then
echo " The maximum value is $a"
elif [ $b -gt $a ];then
echo " The maximum value is $b"
else
echo " equal "
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
The maximum value is 8
[[email protected] test]# ./6.3.sh
please input two num:9 3
The maximum value is 9
[[email protected] test]# ./6.3.sh
please input two num:6 6
equal [[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边栏推荐
- Div horizontal arrangement
- 综合设计一个OPPE主页--页面的底部
- Which hero is the most difficult for lol in terms of code?
- 智慧解决问题
- Will the modified data be updated when it is the same as the original data?
- Geeer's happiness | is for the white whoring image! Analysis and mining, NDVI, unsupervised classification, etc
- My problem solving record 1: the @component annotation is used on the class. If you want to use the methods in this class, you can't directly new, but should use @autowired for injection, otherwise an
- Source code analysis of senparc.weixin.sample.mp
- Visual Studio
- Use cpolar to publish raspberry pie web pages (improvement of cpolar function)
猜你喜欢

网络安全(5)

Behind 100000 visits...

Summary of introduction to unityshader (2): Beginner Level
![[Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed](/img/a7/12fd63f16ebca81a3dd2d1b97847d1.png)
[Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed

高智伟:数据管理赋能交通行业数字化转型

Pyqt5 rapid development and practice 6.4 qboxlayout (box layout)

引入redis缓存出现的问题以及解决方式

PyQt5快速开发与实战 6.1 好软件的三个维度 && 6.2 PyQt5中的布局管理 && 6.3 PyQt5的绝对位置布局

数仓项目踩坑记录与解决方法总结

Gao Zhiwei: data management enables the digital transformation of the transportation industry
随机推荐
分布式Session共享的4类技术方案,与优劣势比较
Will the modified data be updated when it is the same as the original data?
Virtual machines use host graphics cards (Hyper-V and wsl2)
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
Explanation of trie tree (dictionary tree)
Examples of specific usage of diagnostic instructions in s7-1200 and s7-1500 (led+devicestates+modulestates)
【微信小程序】接口生成自定义首页二维码
Use cpolar to publish raspberry pie web pages (improvement of cpolar function)
开放原子开源基金会黄金捐赠人优博讯携手合作伙伴,助力OpenHarmony破圈!
div水平布局两边对齐
shell编程之sed,正则表达式
Configuration file settings for remote connection to Windows version server redis
Behind 100000 visits...
Parameter passing mode of C language (int x) (int *x) (int & x)
OpenCV图像处理基础操作
Is the marginal old technology in its 40s weak in the future or rising from the ground?
系统架构师学习
综合设计一个OPPE主页--页面的底部
Fluent text editor
Use cpolar to publish raspberry pie web pages (improvement of cpolar tunnel)