Bash shell 编程之基础知识概述 (if语句)

简介:

一、Bash shell是什么

shell是什么,Bash与shell又有什么关系。(以前我也不是特别清楚~~~~)

shell 是一个交互性命令解释器。shell独立于操作系统,这种设计让用户可以灵活选择适合自己的shell。shell让你在命令行键入命令,经过shell解释后传送给操作系统(内核)执行。

shell是一个命令处理器(command processor)--是一个读入并解释你输入的命令的程序。除了是一个命令中断器以外,shell还是一个程序设计语言。你可以编写shell可以解释的程序(被称为源程序),这些源程序可以包含shell程序设计命令等等。shell除了解释命令以外,还有其他工作,它也可以配置和编程。  

shell拥有自己的语言允许用户编写程序并以一种复杂方式运行。shell编程语言具有许多常用的编程语言的特征,例如:循环和控制结构等。用户可以生成像其他应用程序一样复杂的shell程序。

以下是shell功能的一个汇总:

查找命令的位置并且执行相关联的程序;

为shell变量赋新值;

执行命令替代;

处理 I/O重定向和管道功能;

提供一个解释性的编程语言界面,包括tests、branches和loops等语句。

bash是borne again shell的缩写,它是shell的一种,Linux上默认采用的是bash。当然还有sh,dash,tcsh和ksh等

1、读入变量(read)

read命令是用于从终端或者文件中读取输入的内建命令,read命令读取整行输入,每行末尾的换行符不被读入。在read命令后面,如果没有指定变量名,读取的数据将被自动赋值给特定的变量REPLY。下面的列表给出了read命令的常用方式:

read 从标准输入中读取一行并赋值给特定的变量ERPLY(没有指定变量名的话)
read one 从标准输入读取并赋值给变量one
read first last 从标准输入读取输入到第一个空格或者回车,将输入的第一个单词放到变量first中,并将该行其他的输入放在变量last中。
read -p 打印提示信息,等待输入,并赋值给默认的变量REPLY
read -t 设置超时时间(单位:秒)
read -a arry_name
将输入在清单存入到"arry_name"数组中

read示例:

1
2
3
4
5
#无变量,默认存于特定变量REPLY中
[root@localhost apache] # read    #等待控制台输入,并将结果赋值给特定内置变量REPLY。
hello            #控制台输入Hello
[root@localhost apache] # echo $REPLY  #打印变量
hello
1
2
3
4
5
[root@localhost apache] # read one two three
3 9 8   #输入1 2 3,它们之间用空格隔开。
[root@localhost apache] # echo "one is:$one,two is:$two three is:$three"
#打印结果
one is:3,two is:9 three is:8
1
2
3
4
5
[root@localhost apache] # read -p "Please input your name:" name
#输出"Please input your name"文本提示,同时等待输入,并将结果赋值给name
Please input your name:scott
[root@localhost apache] # echo "$name"
scott
1
2
3
4
[root@localhost apache] # read -t 4  -p "Enter your nmber:"
#限时4秒输入,如果过了4秒,将退出不再输入
Enter your nmber:[root@localhost apache] # echo $REPLY
#结果为空

2.  状态判断:

test是Shell中提供的内置命令,主要用于状态的检验,如果结果为0,表示成功,否则表示失败。

1
2
3
4
[root@localhost apache] # name=scott
[root@localhost apache] # test $name != scoot
[root@localhost apache] # echo $? #测试上一条命令执行状态的返回值,0表示功
0

注意的是test命令不支持Shell中提供的各种通配符

1
2
3
4
[root@localhost apache] # name=tom
[root@localhost apache] # test $name = [Tt]om
[root@localhost apache] # echo $?
1

test命令还可以中括号予以替换,其语义保持不变

1
2
3
4
[root@localhost apache] # name=tom
[root@localhost apache] # [ "$name" = tom  ]
[root@localhost apache] # echo $?
0

在Shell中还提供了另外一种用于状态判断的方式:` expr `,和test不同的是,该方式中的表达式支持通配符

1
2
3
[root@localhost apache]# [[ $name==[tT]om ]]
[root@localhost apache]# echo $?
0

在` expression `中,expression可以包含&&(逻辑与)和||(逻辑或)。

1
2
3
4
[root@localhost apache] # friend=Jack
[root@localhost apache] # [[ $name==[tT]om && $friend == "Jack" ]]
[root@localhost apache] # echo $?
0

在Shell中还提供了let命令的判断方式: (( expr ))

1
2
3
4
5
6
7
8
[root@localhost apache] # a=45
[root@localhost apache] # b=34
[root@localhost apache] # (( a > b ))
[root@localhost apache] # echo $?
0
[root@localhost apache] # (( a == 45 && b == 34 ))
[root@localhost apache] # echo $?
0

下面的表格是test命令支持的操作符:

表达式

判断为真的条件

字符串判断 结果
[ StringA=String ] StringA等于StringB
[ StringA==StringB ] StringA等于StringB
[ StringA!=StringB ] StringA不等于StringB
[ String ] String不为空
[ -z String ] String长度为0
[ -n String ] String长度不为0
逻辑判断
[ StringA -a StringB ] StringA和StringB都是真
[ StringA -o StringB ] StringA或StringB是真
[ !String ] String不为真
逻辑判断(复合判断)
[[ pattern1 && pattern2 ]] pattern1和pattern2都是真
[[ pattern1 || pattern2 ]] pattern1或pattern2是真
[[ !pattern ]] pattern不为真
整数判断
[ intA -eq intB ] intA等于intB
[ intA -ne intB ] intA不等于intB
[ intA -ge intB ] intA大于等于intB
[ intA -lt intB ] intA小于intB
[ intA -le intB ] intA小于等于intB
[ intA -gt intB ] intA大于intB
文件判断中的二进制操作
[ fileA -ot fileB ] fileA比fileB旧
[ fileA -ef fileB ] fileA和fileB有相同的设备或者inode值
[ fileA -nt fileB ] fileA比fileB新
文件检验
[ -d $file ] or [[ -d $file ]] file为目录且存在时为真
[ -e $file ] or [[ -e $file ]] file为文件且存在时为真
[ -f $file ] or [[ -f $file ]] file为非目录普通文件存在时为真
[ -s $file ] or [[ -s $file ]] file文件存在, 且长度不为0时为真
[ -L $file ] or [[ -L $file ]] file为链接符且存在时为真
[ -r $file ] or [[ -r $file ]] file文件存在且可读时为真
[ -w $file ] or [[ -w $file ]] file文件存在且可写时为真
[ -x $file ] or [[ -x $file ]] file文件存在且可执行时为真
[ -S $file ] or [[ -S $file ]] 测试文件是否存在在并且是否是一个套接字文件
[ -h $file ] or [[ -h $file ]] file为链接符且存在时为真
[ -p $file ] or [[ -p $file ]] 测试文件是否存在并且是否是一个管道文件

注:在逻辑判断(复合判断中),pattern可以包含元字符,在字符串的判断中,pattern2必须被包含在引号中。

3.流程控制语句:

if语句格式如下:

if语句的后面是Shell命令,如果该命令执行成功返回0,则执行then后面的命令。

if command;then
       command
       command
 fi

用test命令测试其后面expression的结果,如果为真,则执行then后面的命令。
   if test expression
   then
       command
   fi

下面的格式和test expression等同
   if [ string/numeric expression ]
   then
       command
   fi

下面的两种格式也可以用于判断语句的条件表达式,而且它们也是目前比较常用的两种。
   if ` string expression `
   then
       command

       .........
   fi

   if (( numeric expression ))           
   then
       command

        .......
   fi

示例:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost tmp] # cat test2.sh
#!/bin/bash
read  -p   "Are you OK(y/n)?"  answer
#这里的$answer变量必须要用双引号扩住,否则判断将失败
    if  "$answer"  = y -o  "$answer"  = Y ]
     then
         echo  "Glad to see it."
     fi
[root@localhost tmp] # bash test2.sh
Are you OK(y /n )?y
Glad to see it.

上面的判断还可以替换为:

1
2
3
4
5
6
7
8
9
10
[root@localhost tmp] # cat test2.sh
#!/bin/bash
read   -p  "Are you OK(y/n or Maybe)?"  answer
#` `复合命令操作符允许其中的表达式包含元字符,这里输入以y或Y开头的任意单词,或Maybe都执行then后面的echo。
if  [[ $answer == [yY]* || $answer = Maybe ]]; then
         echo  "Glad to hear it."
     fi
[root@localhost tmp] # bash test2.sh
Are you OK(y /n  or Maybe)?yadfadsf
Glad to hear it.
1
2
3
4
5
6
7
8
9
10
11
[root@localhost tmp] # cat test3.sh
#!/bin/bash
  answer= "not really"
     if  [[ $answer = [Nn]o?( way |t really) ]]
      then
         echo  "I am sorry."
      fi
[root@localhost tmp] # bash -n test3.sh
[root@localhost tmp] # bash test3.sh
I am sorry.
#对于本示例中的扩展通配符,这里需要给出一个具体的解释。[Nn]o匹配No或no,?( way|t really)则表示0个或1个( way或t really),因此answer变量匹配的字符串为No、no、Not really、not really、No way、no way。

if/elif/else语句的使用方式和if语句极为相似,其格式如下:

   if command

   then

       command

   elif command

   then

       command

   else

       command

   fi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost tmp] # cat test4.sh
  read   -p  "How old are you?"  age
     if  [ $age -lt 0 -o $age -gt 120 ]                 #(( age < 0 || age > 120 ))
     then
         echo  "You are so old."
     elif  [ $age - ge  0 -a $age - le  12 ]                #(( age >= 0 && age <= 12 ))
     then
         echo  "You are child."
     elif  [ $age - ge  13 -a $age - le  19 ]             # (( age >= 13 && age <= 19 ))
     then
         echo  "You are 13--19 years old."
     elif  [ $age - ge  20 -a $age - le  29 ]            # ((age >= 20 && age <= 29 ))
     then
         echo  "You are 20--29 years old."
     elif  [ $age - ge  30 -a $age - le  39 ]              # (( age >= 30   && age <= 39 ))
     then
         echo  "You are 30--39 years old."
     else
         echo  "You are above 40."
     fi
[root@localhost tmp] # bash test4.sh
How old are you?60
You are above 40.

=======================================完================================================










本文转自 jinlinger 51CTO博客,原文链接:http://blog.51cto.com/essun/1362838,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
移动开发 Shell Linux
百度搜索:蓝易云【Shell错误:/bin/bash^M: bad interpreter: No such file or directory】
将 `your_script.sh`替换为你的脚本文件名。运行此命令后,脚本文件的换行符将被转换为Linux格式,然后就可以在Linux系统上正常执行脚本了。
34 8
|
5月前
|
Shell
在Shell脚本中的if语句
在Shell脚本中的if语句
43 1
|
19天前
|
Unix Shell Linux
第一章 Shell基础知识
第一章 Shell基础知识
|
23天前
|
存储 Shell 数据安全/隐私保护
ZooKeeper【基础知识 04】控制权限ACL(原生的 Shell 命令)
【4月更文挑战第11天】ZooKeeper【基础知识 04】控制权限ACL(原生的 Shell 命令)
27 7
|
1月前
|
分布式计算 Hadoop Shell
Hadoop【基础知识 04】【HDFS常用shell命令】(hadoop fs + hadoop dfs + hdfs dfs 使用举例)
【4月更文挑战第4天】Hadoop【基础知识 04】【HDFS常用shell命令】(hadoop fs + hadoop dfs + hdfs dfs 使用举例)
33 5
|
1月前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
1月前
|
Shell
shell 命令(一)概述【别名、 bash重定向 、定义变量】
shell 命令(一)概述【别名、 bash重定向 、定义变量】
23 0
|
3月前
|
Shell
在Shell脚本中,使用`if`语句进行复杂的条件判断
在Shell脚本中,使用`if`语句进行复杂的条件判断
38 2
|
4月前
|
Shell
Shell 编程快速入门 之 函数基础知识
Shell 编程快速入门 之 函数基础知识
71 0
Shell 编程快速入门 之 函数基础知识
|
4月前
|
存储 Shell
Shell编程自动化之if、for、while和函数
本文主要介绍了Shell编程自动化之if、for、while和函数,并结合实例测试。
28 3