前端之家收集整理的这篇文章主要介绍了
Bash Shell的学习,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
基本语法
@H_
404_3@1.
输出HELLO
echo HELLO
@H_
404_3@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
@H_
404_3@3.输入一个名字,
输出Welcome “名字”
read str
echo Welcome $str
read name
echo "Welcome $name"
echo "Welcome $(</dev/stdin)"
printf "Welcome "
cat
@H_
404_3@4.
输出1~50
for((i = 1;i <= 50;i++))
do
echo $i
done
@H_
404_3@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`
@H_
404_3@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
@H_
404_3@7.判断是’Y’、’y’还是’N’、’n’
read c
if [[ $c = "Y" || $c = "y" ]]#也可以去掉引号
then
echo "YES"
else
echo "NO"
fi
@H_
404_3@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
@H_
404_3@9.求一行四则运算表达式的值,精确到小数点后三位
read exp
printf "%.3f\n" `echo $exp | bc -l`
@H_
404_3@10.求一整数的平均值,精确到小数点后三位
read N
sum=0
for((i = 1;i <= $N;i++))
do
read x
let "sum+=$x"
done
printf "%.3f" $(echo $sum/$N | bc -l)