当前位置:网站首页>4、 Variable assignment method

4、 Variable assignment method

2022-06-24 01:40:00 jackxiao

  1. Definition a=1
  2. Parameter transfer
#!/bin/bash
a=$1
b=$2
echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"
  1. read Read in -p Tips -t Waiting time for user input read -t 30 -p " Please enter a number :" a

Please enter a number :11

Two 、read Enterprise case

Judge what you input , And carry out corresponding operations according to the entered serial number , The script is as follows

#!/bin/bash
cat <<EOF
  1.install lamp
  2.install lnmp
  3.exit
EOF
read -p " Please select a sequence number ( It has to be numbers ):" num
#1. Judge whether it is an integer 
expr 2 + $num &>/dev/null
if [ $? -ne 0 ]
then
    echo "Usage:$0 {1|2|3}"
    exit 1
fi

#2. Judgment execution processing 
if [ $num -eq 1 ]
then
    echo "install lamp..."
elif [ $num -eq 2 ]
then
    echo "install lnmp..."
elif [ $num -eq 3 ]
then
    echo "bye."
    exit 
else
    echo "Usage:$0 {1|2|3}"
    exit 1
fi
原网站

版权声明
本文为[jackxiao]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211116155619674S.html