shell腳本練習一
#!/bin/bash
read -p "請輸入100米賽跑秒數:" i
if [ $i -lt 10 ] && [ $i -gt 0 ]
then
echo "進入決賽"
read -p "輸入男女" a
if [ $a = 男 ]
then
echo "進入男子組"
elif [ $a = 女 ]
then
echo "進入女子組"
else
echo "error"
fi
elif [ $i -ge 10 ]; then
echo "淘汰"
else
echo "錯誤"
fi
case語句練習,根據文件名後綴來自動解壓壓縮文件
vim untar.sh
#!/bin/bash
case $1 in
*.gz)
if [ -f $1 ]
then
tar -zxvf $1 -C /opt
fi
;;
*.bz2)
if [ -f $1 ]
then
tar -jxvf $1 -C /opt
fi
;;
*)
echo "文件格式錯誤"
esac
求小於100的整數和
#!/bin/bash
sum=0
read -p "
if [ $n -lt 100 ]; then
for i in $(seq 1 $n)
do
let sum=$sum+$i
done
echo "從1到$n之間的所以整數的和為$sum"
else
echo "輸入錯誤"
fi
求小於100的奇數和、偶數和
#for語句格式
#!/bin/bash
sum=0
for i in {1..100..2}
do
let sum=$sum+$i
done
echo "基數和=$sum"
SUM=0
for i in $(seq 0 2 100)
do
let SUM=$SUM+$i
done
echo "偶數和=$SUM"
#while語句格式
#!/bin/bash
a=1
b=0
while [ $a -le 100 ]
do
if [ `expr $a % 2` -eq 1 ]; then
# if [ $(($a%2)) -eq 1 ]; then
let b=$b+$a
fi
let a++
done
echo "基數和=$b"
A=1
c=0
while [ $A -le 100 ]
do
# if [ `expr $A % 2` -eq 0 ]; then
if [ $(($A%2)) -eq 0 ]; then
let c=$c+$A
fi
let A++
done
echo "偶數和=$c"
shell腳本練習一