shell脚本实例

it2023-07-24  72

数的比较输出

实例1

#!/bin/bash Num1=100 Num2=200 if [ $Num1 -gt $Num2 ] ; then echo "num1>num2" else echo "num2>num1" fi if [ $Num1 -eq $Num2 ]; then echo "num1=num2" else echo "num1!=num2" fi

实例2

#!/bin/bash read -p "请输入a的值:" a if [ "$a" -gt 3 ] then echo "a大于3" elif [ "$a" -eq 3 ] then echo "a等于3" elif [ "$a" -lt 3 ] then echo "a小于3" else echo "没有这个值" fi

成绩的if判断

#!/bin/bash read -p "Please input you score: " a if [ "$a" -lt 60 ] then echo "You didn'it pass the exam." elif [ "$a" -ge 60 ] && [ "$a" -lt 90 ] then # elif相对if,再做一次判断 echo "Good!You passed the exam." else echo "Very good!Your score is very high." fi

变量运算

a=1 b=2 sum=$(($a+$b)) echo "$a+$b=$sum"

用户交互DIY

read -p "请输入X的值:" x read -p "请输入Y的值:" y sum=$(($x+$y)) echo "X和Y的和为:$sum"
最新回复(0)