#!/bin/bash ## In this script we will use variables. ## Writen by lzx 2018-7-27
d=`date +%H:%M:%S` #反引号的作用是将引号内的字符串当成shell命令执行,返回命令的执行结果 echo "The script begin at $d." echo "Now we'll sleep 2 seconds." sleep 2 d1=`date +%H:%M:%S` echo "The script end at $d1."
上面d和d1作为变量出现。
执行上面脚本,查看执行结果:
# sh variable.sh The script begin at 10:27:22. Now we'll sleep 2 seconds. The script end at 10:27:24.
3.1 数字运算
在脚本中进行数字运算,也会用到变量。
下面再自定义一个脚本:
# vim sum.sh #写入下面内容
#! /bin/bash ## For get the sum of two numbers. ## Writen by lzx 2018-7-27
a=1 b=2 sum=$[$a+$b] echo"$a+$b=$sum"
数学计算要用[]括起来,并且前面要加上符号$。
# sh sum.sh 1+2=3
3.2 与用户交互
自定义一个脚本:
# vim read.sh
#!/bin/bash ## Using 'read' in shell script. ## Writen by lzx 2018-7-27
read -p "Please input a number:" x read -p "Please input another number:" y sum=$[$x+$y] echo "The sum of two numbers is:$sum"
read命令用于和用户交互,它把用户输入的字符串作为变量值
# sh read.sh Please input a number:5 Please input another number:11 The sum of two numbers is:16
加上 -x选项查看执行过程:
# sh -x read.sh + read -p 'Please input a number:' x Please input a number:5 + read -p 'Please input another number:' y Please input another number:11 + sum=16 + echo 'The sum of two numbers is:16' The sum of two numbers is:16
# sh if1.sh Please input your score: 90 #没有设置else条件,所以没有结果 # sh if1.sh Please input your score: 50 You didn't pass the exam.
4.2 带有else
具体格式如下:
if 判断语句 then command else command fi
下面做if1.sh脚本做修改:
# vim if1.sh #修改为下面内容
#!/bin/bash
read -p "Please input your score: " a if ((a<60));then echo "You didn't pass the exam." else echo "Good!You passed the exam." fi
执行上面脚本:
# sh if1.sh Please input your score: 90 Good!You passed the exam. #设置了else条件之后,这里返回了结果 # sh if1.sh Please input your score: 50 You didn't pass the exam.
4.3 带有elif
具体格式如下:
if 判断语句1;then command elif 判断语句2;then command else command fi
下面自定义一个脚本:
# vim if2.sh #写入下面内容
#!/bin/bash
read -p "Please input you score: " a if ((a<60));then echo "You didn't pass the exam." elif ((a>=60)) && ((a<90));then # elif相对if,再做一次判断 echo "Good!You passed the exam." else echo "Very good!Your score is very high." fi
上面的 &&表示“并且”,另外还有||表示“或者”。
执行上面脚本:
# sh if2.sh Please input you score: 40 You didn't pass the exam. # sh if2.sh Please input you score: 85 Good!You passed the exam. # sh if2.sh Please input you score: 95 Very good!Your score is very high.
#if [ -d /home/ ]; thenecho ok; fi ok #if [ -f /home/ ]; thenecho ok; fi
/home/是目录而非文件,所以在判断它是否为文件时并不会显示ok。
#if [ -f /root/test.txt ]; thenecho ok; fi ok #if [ -r /root/test.txt ]; thenecho ok; fi ok #if [ -w /root/test.txt ]; thenecho ok; fi ok #if [ -x /root/test.txt ]; thenecho ok; fi #if [ -e /root/test123.txt ]; thenecho ok; fi
4.5 case逻辑判断
在shell脚本中,除了使用if来判断逻辑外,还可以使用case。
具体格式如下:
case 变量 in value1) command ;; value2) command ;; value3) command ;; *) command ;; esac
上面的结构中,不限制value的个数,*代表其它任意值。
下面自定义一个脚本:
# vim case.sh
#!/bin/bash
read -p "Input a number: " n a=$[n%2] case $a in 1) echo "The number is odd." # odd:奇数 ;; 0) echo "The number is even." # even:偶数 ;; *) echo "It's not a number!" esac
脚本中的$a的值为1或0,执行结果如下:
# sh case.sh Input a number: 100 The number is even. # sh case.sh Input a number: 13 The number is odd.