当前位置:网站首页>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边栏推荐
猜你喜欢

7.9-7.17 new features and grammar of learning plan ES6

Excel tool for generating database table structure

C# 值类型和引用类型讲解

Appendix 2 – some simple exercises

Custom configuration
![[C language] minesweeping (recursive expansion + marking function)](/img/f6/835ac4ba6588fa61edb30446f4c708.png)
[C language] minesweeping (recursive expansion + marking function)

Unity xchart3.0 basic usage quick start

Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice

Evaluation index of machine learning classification model and implementation of sklearn code

使用cpolar发布树莓派网页(cpolar功能的完善)
随机推荐
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
系统架构师学习
vector实现
i.MX6ULL驱动开发 | 32 - 手动编写一个虚拟网卡设备
System architect learning
[Yunzhu co creation] [hcsd live broadcast] teach the interview tips of big companies in person
Random number setting and reference between parameters
Shutter -- use camera (continuously updating)
这是一份不完整的数据竞赛年鉴!
Commonly used DOS commands [gradually improved]
Senparc.Weixin.Sample.MP源码剖析
智慧解决问题
Excel tool for generating database table structure
OpenCV图像处理基础操作
Examples of specific usage of diagnostic instructions in s7-1200 and s7-1500 (led+devicestates+modulestates)
Gao Zhiwei: data management enables the digital transformation of the transportation industry
MySQL事务与MVCC如何实现的隔离级别
First order traversal / second order traversal determines the approximate shape of the tree
i. Mx6ull driver development | 32 - manually write a virtual network card device
WebAssembly 2022 问卷调查结果新鲜出炉