函数
#!/bin/bash
funWithParam(){
echo "The first parameter is $1 !"
echo "The second parameter is $2 !"
echo "The tenth parameter is $10 !"
echo "The tenth parameter is ${10} !"
echo "The eleventh parameter is ${11} !"
echo "The total number of parameters is $# !"
echo "Outputs all parameters as a string $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
10不能获取第十个参数,获取第十个参数需要{10}。当n>=10时,需要使用${n}来获取参数。
The first parameter is 1 !
The second parameter is 2 !
The tenth parameter is 10 !
The tenth parameter is 34 !
The eleventh parameter is 73 !
The total number of parameters is 11 !
Outputs all parameters as a string 1 2 3 4 5 6 7 8 9 34 73 !
计算脚本
求 0-100的偶数合
其中seq 2 2 100
指从2开始到100,中间2为 步长。
#!/bin/bash
cnt=0
sum=0
for cnt in `seq 2 2 100`
do
sum=$((cnt+sum))
done
echo $sum
三个数求最大
#!/bin/bash
max=0
a=8
b=4
c=5
for i in $a $b $c
do
if [ $i -gt $max ]
then
max=$i
fi
done
echo $max
其中
-eq 等于
-ne 不等于
-gt 大于
-ge 大于等于
-lt 小于
-le 小于等于
小游戏
猜数字游戏:
首先让系统随机生成一个数字,给这个数字一个范围,让用户猜数字,对输入作出判断,并且给出提示。
#!/bin/bash
function randNum(){
while :
do
read aNum
if test $aNum -eq $1
then
echo "right"
break 1
else
if [ $aNum -gt $1 ]
then
echo "The answer is smaller than yours."
else
echo "The answer is bigger than yours."
fi
fi
done
}
randNum $(($RANDOM%100+1))
部分脚本来自 https://www.shiyanlou.com

公众号
在号内与我交流,回复【资源】获取技术大礼包