就像其他的编程语言一样,shell也有三种基本的结构:顺序结构、分支结构、循环结构。顺序结构就是按照命令的出现顺序依次执行,比较简单。如下分别介绍分支结构和循环结构。

分支结构

格式1

if command
then
commands
fi
# 或者
if command: then
commands
fi

说明:在其他编程语言中,if后面的command是一个等式,用来判断是true还是false;但是在bash shell中,该command是一个命令,if语句根据command执行结果的退出状态码是否为0来判断then部分是否需要执行[返回0表明命令执行成功,则执行then部分]。

如下使用grep命令在/etc/passwd文件中查找某个特定用户名是否在当前系统上使用,如果存在该用户,则显示该用户主目录的bash文件:

#! /bin/bash
# function: This script is to test structured commands
# author: benxintuzi
# date: -- user=benxintuzi if grep $user /etc/passwd
then
  echo "The bash files for user $user are:"
  ls -a /home/$user/.b*
fi

格式2

if command
then
  commands
else
  commands
fi
user=123456

if grep $user /etc/passwd
then
  echo "The bash files for user $user are:"
  ls -a /home/$user/.b*
else
  echo "The user $user does not exist on this system"
fi

格式3:

if command1
then
  commands
elif command2
then
  commands
elif ...
then
  ...
fi

注:if-then语句不能测试与命令退出码无关的条件,这个任务可以通过test命令来完成。

test命令的格式非常简单:test condition,用在if-then语句块中时,格式如下:

if test condition
then
commands
fi # 或者 # bash shell中可以使用方括号:
if [ condition ]
then
commands
fi
说明:
方括号定义了test命令,但是括号内两侧必须有空格

test命令可以用于3类条件:数值比较、字符串比较、文件比较。

1 数值比较

n1 –eq n2

判断n1是否等于n2

n1 –ge n2

判断n1是否大于或等于n2

n1 –gt n2

判断n1是否大于n2

n1 –le n2

判断n1是否小于或等于n2

n1 –lt n2

判断n1是否小于n2

n1 –ne n2

判断n1是否不等于n2

val=

if [ $val -gt  ]
then
  echo "The test value $val is greater than 5"
else
  echo "sorry, you are so little"
fi

2 字符串比较

str1 = str2

是否相同

str1 != str2

是否不同

str1 < str2

是否小于

str1 > str2

是否大于

-n str1

str1的长度是否非0

-z str1

str1的长度是否为0

2.1 【=、!=

user=benxintuzi

if [ $USER = $user ]
then
  echo "Welcome $user"
else
  echo "The user $user is not exist"
fi

2.2 【>、<这两个比较操作使用时必须转义,否则shell将其解释为重定向符号。

val1=Testing
val2=testing if [ $val1 \> $val2 ]
then
  echo "The $val1 is greater than $val2"
else
  echo "The $val1 is less than $val2"
fi

2.3 【-n、-z

val1=testing
val2='' if [ -n val1 ]
then
  echo "The string '$val1' is not empty"
else
  echo "The string '$val1' is empty"
fi if [ -z val2 ]
then
  echo "The string '$val2' is not empty"
else
  echo "The string '$val2' is empty"
fi

3 文件比较

-d file

检查file目录是否存在

-e file

检查file是否存在

-f file

检查file文件是否存在

-r file

检查file是否存在并可读

-s file

检查file是否存在并非空

-w file

检查file是否存在并可写

-x file

检查file是否存在并可执行

-O file

检查file是否存在并属于当前用户所有

-G file

检查file是否存在并且默认组与当前用户相同

file1 –nt file2

检查file1是否比file2新

file1 –ot file2

检查file1是否比file2旧

复合条件的编写

if-then语句可以使用布尔逻辑来组合测试,格式如下:

[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]

if-then高级特性

(( expression ))    用于对expression进行简单比较或计算,其中的比较操作符>等不需要转义
[[ expression ]] 主要用于字符串模式匹配
val1=

if (( $val1 **  >  ))
then
  (( val2=$val1 ** ))
  echo "The square of $val1 is $val2"
fi
if [[ $USER == benxin* ]]
then
  echo "Hello, $USER"
else
  echo "Sorry, I do not know you"
fi

当分支情况比较多时,可能需要编写多条if-then-elif-then-...语句,此时,可以使用case语句,格式如下:

case variable in
pattern1 | pattern2)
  command1;;
pattern3)
  commands;;
*)
  commands;;
esac

说明:在一行中使用|来分割多个模式。

val=benxin  

case $val in
benxin | tuzi)
  echo "You are alone";;
benxintuzi)
  echo "Congratulations";;
*)
  echo "go out now!";;
esac

循环结构

for命令

for var in list
do
commands
done # 或者 for var in list; do
commands
done
for var in benxin tuzi benxintuzi
do
  echo "The next user is $var"
done

说明:for命令使用空格来划分列表中的不同值,如果单个数据值中也有空格,此时需要用双引号将这个值括起来,如下:

for var in benxin tuzi "benxin     tuzi"
do
  echo "The next user is $var"
done

通常的情况下,我们将一系列的值都存储在一个变量中,如:

list="benxin tuzi benxintuzi"
for var in $list
do
echo "The next user is $var"
done

bash shell中使用IFS(Internal Field Separator)环境变量作为字段分隔符,默认情况下IFS使用如下字符:空格符,制表符,换行符。

如果要将冒号:设定为分隔符,可以如下设置:

IFS=:

IFS=:

list="benxin:tuzi:benxintuzi"
for var in $list
do
echo "The next user is $var"
done

如果需要指定多个IFS字符,只要将它们在赋值行中串起来就行了:

(IFS=$'\n:;')表示可以使用换行符、冒号、分号作为字段之间的分隔符。

IFS=$'\n:;'

list="benxin
tuzi:benxintuzi
tuzi;"
for var in $list
do
echo "The next user is $var"
done

注:我们修改默认IFS一般只是为了处理一段特殊的脚本,因此处理完成后必须恢复默认值,如下:

IFS.OLD=$IFS
IFS=$’\n’
# user processing...
IFS=$IFS.OLD

while命令

while test command
do
commands
done
val=

while [ $val -gt  ]
do
echo $val
val=$[ $val - ]
done

until命令

until test command
do
commands
done

说明:until命令的执行刚好与while相反,其测试条件不成立时才执行。

循环控制命令

 break命令
终止并退出循环;
break n
默认情况下,n为1,表示跳出当前循环;如果n为2,表示跳出上一级循环
for a in
do
echo "Outer loop: $a"
for (( b = ; b < ; b++ ))
do
if [ $b -gt ]
then
break
fi
echo "Inner loop: $b"
done
done

        
 continue命令
跳过本次循环中的其余命令,继续执行下一轮循环
同理也有continue n

循环重定向

for a in
do
  echo "Outer loop: $a"
  for (( b = ; b < ; b++ ))
  do
    if [ $b -gt ]
    then
      break
    fi
    echo "Inner loop: $b"
  done
done > output.txt

即直接在done后加上重定向符号即可,当然,也可以直接将循环输出重定向到一个命令的输入中:

for state in apple orange pear banana pineapple
do
echo "$state is my favourite fruit"
done | sort

shell 基本结构的更多相关文章

  1. Shell脚本、Shell脚本结构、date命令的用法、变量

    1.Shell脚本: shell是一种脚本语言 目的:可以实现自动化运维,能大大增加运维的效率.2.Shell脚本结构:   #!/bin/bash  以#!/bin/bash开头,即以/bin/ba ...

  2. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  3. shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

    7月11日任务 20.1 shell脚本介绍20.2 shell脚本结构和执行20.3 date命令用法20.4 shell脚本中的变量 20.1 shell脚本介绍 1.shell脚本语言是linu ...

  4. Linux centosVMware shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

    一. shell脚本介绍 shell是一种脚本语言 aming_linux blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 shell是系统命令的集合 shell脚 ...

  5. For,while,case,shell循环结构

                                                                For,while,case,shell循环结构 案例1:使用for循环结构 案 ...

  6. Linux编程 24 shell编程(结构化 if [ condition ] 数值比较,字符串比较)

    一.概述 接着上篇讲的结构化命令,最后讲到了test命令的另一种写法 if [ condition ],它的语法格式如下: --格式如下: if [ condition ] then commands ...

  7. Linux编程 23 shell编程(结构化条件判断 命令if -then , if-then ... elif-then ...else,if test)

    一.概述 在上一篇里讲到了shell脚本,shell按照命令在脚本中出现的顺序依次进行处理,对于顺序操作已经足够了,但许多程序要求对shell脚本中的命令加入一些逻辑流程控制,这样的命令通常叫做 结构 ...

  8. shell脚本结构

    echo $? 代表上一次命令的状态返回值,‘0’则代表为真<执行成功>,‘非零’则代表为假<执行失败>. shell脚本: <判断老男孩的年纪> [root@bo ...

  9. shell脚本结构化语句

    本文中记录一下shell中的两种循环语句:for和while for循环 for循环是linux shell中最常用的结构,for循环有三种结构:1.列表for循环.2.不带列表for循环.3.C风格 ...

随机推荐

  1. Apache Segmentaion Fault故障处理案例分析

    本文出自 "李晨光原创技术博客" 博客,转载请与作者联系!

  2. Anaconda日志

    https://fedoraproject.org/wiki/Anaconda/Logging Anaconda日志 centos7 anaconda安装日志目录 /var/log/anaconda ...

  3. Edit 方法

    1. 在FORM 的grid里面作为记录选择字段 AX 的edit 方法可以很方便地给用户提供记录选择功能,而不用在TABLE上添加新字段. 通常结合map使用,一般edit 方法格式: edit N ...

  4. 几款超实用的 CSS 开发工具

      当你开发一个网站或 web 应用程序的时候,有合适的工具,绝对可以帮助您节省大量的时间.在这篇文章中,我为大家收集了超有用的 CSS 开发工具. 对于 Web 开发人员来说,找到有用的 css 开 ...

  5. WPF拖动DataGrid中的数据到ListBox

    1.效果图: 2.XAML <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.m ...

  6. 十六、Struts2文件上传与下载

    文件上传与下载 1.文件上传前提:<form action="${pageContext.request.contextPath}/*" method="post& ...

  7. VHDL学习札记:library and Package

     参考:http://www.cnblogs.com/garylee/archive/2012/11/16/2773596.htmlhttp:// http://forums.xilinx.com ...

  8. PF_RING安装

    1.安装Build-essential.SVN.Flex.Libnuma-dev.bison ubuntu中:sudo apt-get install build-essential subversi ...

  9. webserver 发布问题

    1:web.config  <system.web>     <compilation debug="true" targetFramework="4. ...

  10. 2014年第五届蓝桥杯试题C/C++程序设计B组——李白打酒

    题目描述: 标题:李白打酒 话说大诗人李白,一生好饮.幸好他从不开车. 一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒. 逢店加一倍,遇花喝一斗. 这一路上,他一共 ...