11.read命令
1.等待输入的值,赋给read后面的变量
printf "请输入一个数:"
read num
printf "请再输入一个数:"
read num1
pirntf "num=%s\nnum1=%s\n" "$num" "$num1"
12.整数基本运算
1.算术运算+ ,- ,* , /, %
value1=1
value2=2
value3=`expr $value1 + $value2`
value4=`expr $value1 - $value2`
value5=`expr $value1 \* $value2` //乘法必须这么写
value6=`expr $value1 / $value2`
value=`expr $value1 % $value2`
num1=3
num2=4
#sum=$((num1+num2))
let sum1=num1+num2
echo $sum1
注意:$()和``代表取出数据的结果集
2.关系运算符 >、>=、<、<=、==、!=
注意:>、<、>=、<= 前面都需要加\才可以使用
value1=2
value2=3
result1=`expr $value1 \>= $value2`
result2=`expr $value1 \== $value2`
result3=`expr $value1 \!= $value2`
echo $result1
echo $result2
echo $result3
3.如果数字关系运算符与[]连用,建议使用下面的方式比较
-eq == equal
-ne != not equal
-gt > greater than
-lt < less than
-ge >=
-le <=
13.if语句的使用
(1)
if [ 条件 ]
then
语句1
fi
(2)
if [ 条件 ]
then
语句1
else
语句2
fi
(3)
if[ 条件 ]
then
语句1
elif[ 条件 ]
语句2
else
语句3
fi
****注意:
1.如果有else,else只能放在最后
2.if语句是可以嵌套的(建议最多3次)****
(4)
逻辑运算符 && 、|| 、
echo "please input num :"
read num
if [ `expr $num % 3` -eq 0 ] && [ `expr $num % 5` -eq 0 ]
then
echo "3,5 YES"
else
echo "NO"
fi
14.${},$(),$(())的区别
15.let命令
1.计算整数
2.除了可以运算+ - * / % ,还可以运算+= ,-=,/= ,*=,%=
let num2+=num1 #num2=num2+num1
let num2=num1-- #num2=num1,num1=num1-1
16 作业
- 手动键入3个数,求最大值,并输出
printf "input num1: "
read num1
printf "input num2: "
read num2
printf "input num3: "
read num3
if [ $num1 -ge $num2 ]
then
if [ $num1 -ge $num3 ]
then
printf "max=%d\n" "$num1"
else
printf "max=%d\n" "$num3"
fi
elif [ $num2 -ge $num3 ]
then
printf "max=%d\n" "$num2"
else
printf "max=%d\n" "$num3"
fi
printf "input 3 nums: "
read num1
read num2
read num3
if ((num1>num2))
then
if ((num1>num3))
then
echo "max=$num1"
else
echo "max=$num3"
fi
else
if ((num2>num3))
then
echo "max=$num2"
else
echo "max=$num3"
fi
fi
- 手动键入2个数,并输入运算符,根据运算符+ - / *的符号给出相应计算的 结果
printf "input num1: "
read num1
printf "yun suan: "
read ch
printf "input num2:"
read num2
ch1="+"
ch2="-"
ch3="*"
ch4="/"
if [ "$ch" == "$ch1" ]
then
man=`expr $num1 + $sum2`
elif [ "$ch" == "$ch2" ]
then
man=`expr $num1 - $num2`
elif [ "$ch" == "$ch3" ]
then
man=`expr $num1 \* $sum2`
elif [ "$ch" == "$ch4" ]
then
man=`expr $num1 / $sum2`
fi
echo $man
- 判断输入的数是否是偶数
printf "input num1: "
read num
if [ `expr $num % 2` -eq 0 ]
then
printf "YES\n"
else
printf "NO\n"
fi
- 创建当天文件夹
year=`date +%Y`
month=`date +%m`
day=`date +%m%d`
if [ $# == 0 ]
then
if [ -e ~/myshell/$year/$month/$day ]
then
printf "%s Folder already exists\n" $day
else
mkdir -p ~/myshell/$year/$month/$day
printf "%s Folder creation cuccess\n" $day
fi
elif [ $# == 1 ]
then
case $1 in
"-y")
if [ -e ~/myshell/$year ]
then
printf "%s Folder already exists\n" $year
else
mkdir -p ~/myshell/$year
printf "%s Folder creation cuccess\n" $year
fi
;;
"-m")
if [ -e ~/myshell/$year/$month ]
then
printf "%s Folder already exists\n" $month
else
mkdir -p ~/myshell/$year/$month
printf "%s Folder creation success\n" $month
fi
;;
"-d")
if [ -e ~/myshell/$year/$month/$dah ]
then
printf "%s Folder already exists\n" $day
else
mkdir -p ~/myshell/$year/$month/$day
printf "%s Folder creation success\n" $day
fi
;;
*)
printf "parameter error\n"
;;
esac
else
printf "parameter\n"
fi
然后 sudo cp myfile /bin/
万年历