1.从命令行传参传入两个整数,整数1代表从整数几开始求和,整数2代表求和到整数几为止
ubuntu@ubuntu:04_branchstruct$ cat sumwork.sh
#!/bin/bash
#for i in $(seq $1 $2)
for i in `seq $1 $2`
do
((sum+=i))
done
echo sum=$sum
2.打印99乘法表
ubuntu@ubuntu:04_branchstruct$ cat multable99.sh
#!/bin/bash
for i in {1..9}
do
for j in $(seq 1 $i)
do
echo -n "$i*$j=$(($i*$j))"
echo -ne "\t"
((j++))
done
echo
((i++))
done
ubuntu@ubuntu:04_branchstruct$ bash multable99.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
3.输入年月日,计算是该年的第几天
ubuntu@ubuntu:04_branchstruct$ cat whichday.sh
#!/bin/bash
read -p "please input the year month day>>>" year month day
leap=0
if [ $(($year%4)) -eq 0 ] && [ $(($year%100)) -ne 0 ] || [ $(($year%400)) -eq 0 ]
then
echo "$year is leap year"
leap=1
fi
sum=0
for month in $(seq 1 $(($month-1)))
do
case "$month" in
1|3|5|7|8|"10")
day1=31
;;
[469]|"11")
day1=30
;;
2)
if [ $leap -eq 1 ]
then
day1=29
else
day1=28
fi
;;
esac
sum=$(($sum+$day1))
done
sum=$(($sum+$day))
echo sum=$sum
ubuntu@ubuntu:04_branchstruct$ bash whichday.sh
please input the year month day>>>2023 1 15
sum=15
ubuntu@ubuntu:04_branchstruct$ bash whichday.sh
please input the year month day>>>2023 5 31
sum=151