当前位置:网站首页>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边栏推荐
- 我的问题解决记录1:类上使用了@Component注解,想要使用这个类中的方法,便不能直接new,而应该使用# @Autowired进行注入,否则会报错(如空指针异常等)
- How to realize the isolation level between MySQL transactions and mvcc
- 系统架构师学习
- Commonly used DOS commands [gradually improved]
- Google Earth engine (GEE) -- calculate the location of the center point, the external boundary, the external polygon, fuse and simplify the boundary and return it to the vector set
- 机器学习之线性回归(最小二乘法手写+sklearn实现)
- 2021年CS保研经历(六):系统填报 + 一些感想
- CS assurance and research experience in 2021 (IV): pre promotion and exemption of Xijiao soft Research Institute and the third room of Information Technology Institute
- RTMP supports h265 streaming
- Webassembly 2022 questionnaire results are fresh
猜你喜欢

Network security (6)

SiC Power Semiconductor Industry Summit Forum successfully held

Sample is new and supported from API 8! Come and take it

MySQL converts some table names to uppercase

Which hero is the most difficult for lol in terms of code?

Vector implementation

NFA determination and DFA minimization based on C language

WebAssembly 2022 问卷调查结果新鲜出炉

Explanation of C value type and reference type

Nucleic acid scanning code registration experience (how to improve the correct character recognition rate of OCR)
随机推荐
23 postgraduate entrance examination people hold on! The first wave of exam abandonment peak has arrived!
QoS quality of service five traffic shaping of QoS boundary behavior
【C语言】三子棋(智能下棋 + 阻拦玩家)
机器学习入门的百科全书-2018年“机器学习初学者”公众号文章汇总
网络安全(6)
Logistic regression of machine learning
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
In simple terms, dependency injection and its application in Tiktok live broadcast
Behind 100000 visits...
First order traversal / second order traversal determines the approximate shape of the tree
Evaluation index of machine learning classification model and implementation of sklearn code
Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
Unity guidance system. Click the target object and prompt the text to change color to enter the next step
PyQt5快速开发与实战 6.1 好软件的三个维度 && 6.2 PyQt5中的布局管理 && 6.3 PyQt5的绝对位置布局
40余岁的边缘老技术,是未来乏力还是掘地而起?
Basic operations of OpenCV image processing
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
一知半解 ~题目杂记 ~ 一个多态问题
函数和数组
Network security (5)