基本语法
1.输出HELLO
echo HELLO
2.输出1~100的奇数
for((i = 1;i <= 99;i+=2))
do
echo $i
done
for i in {1..99..2}
do
echo $i
done
seq 1 2 99
3.输入一个名字,输出Welcome “名字”
read str
echo Welcome $str
read name
echo "Welcome $name"
echo "Welcome $(</dev/stdin)"
printf "Welcome "
cat
4.输出1~50
for((i = 1;i <= 50;i++))
do
echo $i
done
5.输出X、Y的加减乘除
read X
read Y
echo $((X+Y))
echo $((X-Y))
echo $((X*Y))
echo $((X/Y))
read X
read Y
echo `expr $X + $Y`#变量之间注意空格
echo `expr $X - $Y`
echo `expr $X \* $Y`
echo `expr $X / $Y`
6.输出整数X与Y的关系,一个字符串
read X
read Y
if [ $X -gt $Y ]
then
echo "X is greater than Y"
elif [ $X -lt $Y ]
then
echo "X is less than Y"
else
echo "X is equal to Y"
fi
7.判断是’Y’、’y’还是’N’、’n’
read c
if [[ $c = "Y" || $c = "y" ]]#也可以去掉引号
then
echo "YES"
else
echo "NO"
fi
8.给出三边长度,判断是等边,等腰,还是不等边
read X
read Y
read Z
if [[ $X -eq $Y && $Y -eq $Z ]]
then
echo "EQUILATERAL"
elif [[ $X -eq $Y || $Y -eq $Z || $X -eq $Z ]]
then
echo "ISOSCELES"
else
echo "SCALENE"
fi
9.求一行四则运算表达式的值,精确到小数点后三位
read exp
printf "%.3f\n" `echo $exp | bc -l`
10.求一整数的平均值,精确到小数点后三位
read N
sum=0
for((i = 1;i <= $N;i++))
do
read x
let "sum+=$x"#也可以 sum=$((sum+x))
done
#var=`echo "scale=3;$sum/$N"|bc`
printf "%.3f" $(echo $sum/$N | bc -l)