内容:

一.if-then命令

二.if-then-else命令

三.test命令

四.case命令

1.if-then结构化命令中最基本的类型,其格式如下:

 if command
then
commands
fi

这里需要注意的是在其他语言中if 语句之后的对象是一个等式来测试是TRUE还是FALSE值,而在bash shell中if 语句会运行if 行定义那个命令。如果该命令退出码是数字0,则表示该命令运行成功,位于then 后面的命令就会运行。如果退出码是其他值,那么then后面的命令就不会执行。

 oracle@suse:~> cat test
#!/bin/bash
#test if-then
if ls
then
echo "ls worked"
fi

将第4行ls命令换成bash shell不识别的命令,比如sdjflsdjfl,执行脚本会报错.

 oracle@suse:~> cat test
#!/bin/bash
#test if-then
if sdjflsdjfl
then
echo "ls worked"
fi
oracle@suse:~> ./test
./test: line : sdjflsdjfl: command not found

2.if-then-else,格式如下:

 if command
then
commands
else
commands
fi

当if语句中的命令退出码是0时,then部分命令会执行,否则就会执行else部分命令。

2.1嵌套if,当脚本代码中有多个判断条件时,可以用elif 代替else部分,格式如下:

 if command1
then
commands
elif command2
then
commands
fi
 oracle@suse:~> cat test1
#!/bin/bash
if jjj
then
echo "1 worked"
elif lllls
then
echo "2 worked"
elif date
then
echo "3 worked"
fi
oracle@suse:~> ./test1
./test1: line : jjj: command not found
./test1: line : lllls: command not found
Thu Apr :: CST
worked

3.如果test命令中列出的条件成立,test命令就会退出并返回退出码0,如果条件不成立test命令就会退出并返回退出码1。test 命令,格式:

 test condition

与if-then配合使用

 if test condition
then
commands
fi

bash shell另一种声明test方法,用方括号[],需要注意[]与condition之间有空格,就是必须在左括号右侧和右括号左侧各加一个空格,否则会报错。

if [ condition ]
then
commands
fi

3.1使用test进行数值比较

n1 -eq n2              检查n1是否等于n2

n1 -gt n2              检查n1是否大于n2

n1 -ge n2              检查n1是否大于等于n2

n1 -lt n2     检查n1是否小于n2

n1 -le   n2             检查n1是否小于等于n2

n1 -ne  n2             检查n1是否不等于n2

 oracle@suse:~> cat test2.sh
#!/bin/bash
#using numeric test comparisons
var1=
var2= if [ $var1 -gt ]
then
echo "the value $var1 is greater than 88"
fi if [ $var1 -gt $var2 ]
then
echo $var1 ">" $var2
else
echo $var1"<="$var2
fi oracle@suse:~> ./test2.sh
the value is greater than
>

3.2 使用test进行字符串比较

str1 = str2 检查str1是否和str2相同

str1 > str2 检查str1是否比str2大

str1 < str2 检查str1是否比str2小

str1 != str2 检查str1是否和str2不同

-n str1    检查str1长度是否非零

-z str1        检查str1长度是否为零

 oracle@suse:~> cat test3.sh
#!/bin/bash
#testing string str1=hello
str2=word if [ -z $str ]
then
str=$str2
echo $str
fi if [ $str1 = $str2 ]
then
echo "str1 = str2"
else
echo "str1 != str2"
fi
oracle@suse:~> ./test3.sh
word
str1 != str2

需要注意使用大于号和小于号时候要在其前面使用转义字符 \ .否则可能会报错,或者将大小于号当成重定向。

 oracle@suse:~> cat test4.sh
#!/bin/bash
#testing string str1=abcd
str2=abcdef if [ $str1 \< $str2 ]
then
echo "$str1 less than $str2"
fi
oracle@suse:~> ./test4.sh
abcd less than abcdef

3.3使用test进行文件比较

 -d  file      检查file是否存在并是一个目录
oracle@suse:~/testshell> cat testfile.sh
#/bin/bash
#test "-d file"
if [ -d $HOME/testshell ]
then
echo "$HOME/testshell is exists"
cd $HOME/testshell
pwd
else
echo "$HOME/testshell is not exists"
fi if [ -d $HOME/nba ]
then
echo "$HOEM/nba is exists"
cd $HOME/nba
pwd
else
echo "$HOME/nba is not exists"
fi oracle@suse:~/testshell> ./testfile.sh
/home/oracle/testshell is exists
/home/oracle/testshell
/home/oracle/nba is not exists

3.4复合条件测试,if-then允许使用布尔逻辑组合测试

[ command1 ] && [ command2 ]

[ command1 ] || [ command2 ]

 oracle@suse:~/testshell> cat showhello.sh
#/bin/bash
echo "hello world"
oracle@suse:~/testshell> cat testfile2.sh
#/bin/bash
#test [ command1 ] && [ command2 ] if [ -f $HOME/testshell/showhello.sh ] && [ -x $HOME/testshell/showhello.sh ]
then
cd $HOME/testshell
./showhello.sh
else
echo "it is not exists"
fi oracle@suse:~/testshell> ./testfile2.sh
hello world

3.5 if-then高级特性双尖括号与双中括号,(( expression ))双尖括号命令允许将高级数学表达式放入比较中,[[ expression ]]双方括号命令提供了针对字符串比较的高级特性。

 oracle@suse:~/testshell> cat testfile3.sh
#/bin/bash
#using double parenthesis var1=
var2= if (( ++var1* > var2 ))
then
echo "$var1*10 is greater $var2"
else
echo "$var1*10"
fi if [[ $USER == ora* ]]
then
echo "hello $USER"
else
echo "there is no this user"
fi
oracle@suse:~/testshell> ./testfile3.sh
* is greater
hello oracle

4.case命令

case variable in
pattern1 | pattern2 ) commands1 ;;
pattern3) commands2;;
*) default commands;;
esac
 oracle@suse:~/testshell> cat testcase.sh
#/bin/bash
#using the case command case $USER in
rich)
echo "hello rich";;
jim)
echo "hello jim";;
tom)
echo "hello tom";;
*)
echo "you are no here"
echo "user is $USER";;
esac
oracle@suse:~/testshell> ./testcase.sh
you are no here
user is oracle

linux shell脚本使用结构化命令的更多相关文章

  1. linux shell脚本使用结构化命令(2)

    一.for命令 二.while命令 三.until命令 1.for命令基本格式 for var in list do commands done oracle@suse:~/testshell> ...

  2. shell脚本之结构化命令if...then...fi

    if的用法日常主要用于数值或者字符串的比较来实现结构化的,模拟人脑,就是如果遇到什么事情,我们应该做什么 语法格式分为 1. if command;then command;fi    (如果if满足 ...

  3. Shell 语法之结构化命令(流程控制)

    许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...

  4. shell初级-----更多结构化命令

    for命令 bash shell提供了for命令,允许你创建一个遍历一系列的循环. for var in list do commands done 1.读取列表中的值 for命令最基本的用法就是遍历 ...

  5. 【学习】Linux Shell脚本编程

    1.脚本的组成和执行 Linux shell脚本的结构并不复杂,其主要由变量.内部命令以及shell的语法结构和一些函数.其他命令行的程序等组成,以下是一个简单的shell脚本. #!/bin/bas ...

  6. 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令

    许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then     ...

  7. bash&nbsp;shell笔记2&nbsp;结构化命令

    二.使用结构化命令 知识内容: # 改变命令流 # 使用if-then逻辑 # 嵌套if-then # 测试条件 # 高级if-then功能 许多程序在脚本命令之间需要某些逻辑控制流,有些命令允许脚本 ...

  8. shell的结构化命令

    shell在逻辑流程控制这里会根据设置的变量值的条件或其他命令的结果跳过一些命令或者循环执行的这些命令.这些命令通常称为结构化命令 1.if-then语句介绍 基本格式 if command then ...

  9. linux shell 脚本攻略学习20--awk命令入门详解

    awk生于1977年,创始人有三个,分别为 Alfred Aho,Peter Weinberger, 和 Brian Kernighan,名称源于三个创始人的姓的首字母. 作用:处理文本文件. awk ...

随机推荐

  1. 【手把手教你Maven】构建过程

    Maven是一款进行 依赖管理.项目构建.信息管理 为一体的工具. 它不像Make具有复杂的命令.也不像Ant需要手动编写大量的重复代码就能进行项目的构建: 还能提供强大的依赖库管理,避免jar包混乱 ...

  2. Hibernate主键生成策略(转)

    1.自动增长identity 适用于MySQL.DB2.MS SQL  Server,采用数据库生成的主键,用于为long.short.int类型生成唯一标识 使用SQL Server 和 MySQL ...

  3. Thinking in Java——笔记(13)

    Strings Immutable Strings Objects of the String class are immutable. Every method in the class that ...

  4. Socket练习

    第一类方法 package socketLianXi; import java.io.IOException; import java.io.InputStreamReader; import jav ...

  5. sqlserver 索引的一些总结【转】

    1.1.1 摘要 如果说要对数据库进行优化,我们主要可以通过以下五种方法,对数据库系统进行优化. 1. 计算机硬件调优 2. 应用程序调优 3. 数据库索引优化 4. SQL语句优化 5. 事务处理调 ...

  6. 升级到macOS 10.12 mysqlb报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

    系统升级到macOS 10.12后启动mysql后,在终端输入mysql 报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' ...

  7. CSS之伪类

    1. :link                     向未被访问的链接添加样式 :visited                向已被访问的链接添加样式 :hover               ...

  8. OpenStack学习参考

    预备知识 Python 调试手段.日志:pdb 开源框架 Django 面向对象:类.继承.多态 编码规范 搭建环境 安装docker,下载openstack镜像,关于docker参考 使用fuel来 ...

  9. 使用git版本控制器C#工程,git托管到GitHub和visual studio on line

    类比TFS, 托管到了VS online,为私有.GitHub上托管的代码为开源. 新建工程选择版本控制器"Git" VS online: 本地: GitHub,下载github ...

  10. post- build event

    Ref:http://blog.csdn.net/teng_ontheway/article/details/8307410 Ref: http://blog.csdn.net/sodickbird/ ...