写在前面:案例、常用、归类、解释说明。(By Jim)
使用if-then语句
如果命令的退出状态是0(成功执行命令),将执行then后面的所有命令。
如果命令的退出状态是0以外的其他值,那么then后面的命令将不会执行,bash shell会移动到脚本的下一条命令。

#!/bin/bash
# testing the if statement
if date
then
echo "it worked"
fi

(date返回0,执行then语句it worked)

#!/bin/bash
#testing multiple commands in the then section
testuser=jiqing9006
if grep $testuser /etc/passwd
then
echo The bash files for user $testuser are:
ls -a /home/$testuser/.b*
fi

(The bash files for user jiqing9006 are:
/home/jiqing9006/.bash_logout  /home/jiqing9006/.bash_profile  /home/jiqing9006/.bashrc
if语句行使用grep命令搜索/etc/passwd文件,查看系统是否正在使用一个特定的用户名。
如果一个用户拥有该登录名,脚本会显示一些文本,然后列出用户HOME目录下的bash文件

if-then-else语句

#!/bin/bash
#testing multiple commands in the then section
testuser=jim
if grep $testuser /etc/passwd
then
echo The bash files for user $testuser are:
ls -a /home/$testuser/.b*
else
echo "The user name $testuser does't exist on this system"
fi

(如果不存在,执行下面的语句)

嵌套if语句

#!/bin/bash
#testing multiple commands in the then section
user1=jim
user2=jiqing9006
if grep $user1 /etc/passwd
then
echo The bash files for user $user1 are:
ls -a /home/$user1/.b*
elif grep $user2 /etc/passwd
then
echo The bash files for user $user2 are:
ls -a /home/$user2/.b*
else
echo "The user name $user1 and $user2 does't exist on this system"
fi

(不是elseif 而是elif ,注意与其它语言的区别)

test命令
if [condition]
then
  commands
 fi
 test命令能够评估三类条件
 数值
 字符串
 文件
 a.数值比较
 n1 -eq n2(是否等于)
 n1 -ge n2(是否大于等于)
 n1 -gt n2(是否大于)
 n1 -le n2(是否小于等于)
 n1 -lt n2(是否小于)
 n1 -ne n2(不等于)

#!/bin/bash
#using numeric test comparisons
val1=
val2=
if [ $val1 -gt ]
then
echo "The test value $val1 is greater than 5"
fi if [ $val1 -eq $val2 ]
then
echo "The values are equal"
else
echo "The values are not equal"
fi

(注意if与[之间有空格,[与$val1之间有空格)
test命令无法处理浮点数,bash shell只能处理整数数字

b.字符串比较
str1 = str2 (str1与str2是否相同)
str1 != str2 (是否不同)
str1 < str2(是否小于)
str1 > str2 (是否大于)
-n str1(长度是否大于0)
-z str1(长度是否为0)

#!/bin/bash
#testing string equality
testuser=root
if [ $USER = $testuser ]
then
echo "Welcome $testuser"
fi

(比较相等)

#!/bin/bash
#testing string equality
testuser=baduser
if [ $USER != $testuser ]
then
echo "This isn't $testuser"
else
echo "Welcome $testuser"
fi

(不相等比较,所有标点符号和大小写都考虑在内)

使用大小于号,需要转义一下

#!/bin/bash
#mis-using string comparisons
val1=baseball
val2=hockey
val3=book
val4=Book
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
if [ $val1 \> $val3 ]
then
echo "$val1 is greater than $val3"
else
echo "$val1 is less than $val3"
fi
if [ $val1 \> $val2 ]
then
echo "$val3 is greater than $val4"
else
echo "$val3 is less than $val4"
fi

结果:
baseball is less than hockey
baseball is less than book
book is greater than Book
(test命令采用的是ascii码排序的,sort采用的是当前语言设置定义的排列顺序。由结果可知,比较第一个字母b小于h,如果第一个字符相同,比较第二个字符,a小于o。小写字符,大于大写字符a97,A65。)

ls -lt(按时间排序,最新的在最前面)
ls -l|sort -k1
ls -l|sort -k2
ls -l|sort -k3(按照第几列进行排序)
..

#!/bin/bash
#testing string length
str1="helloworld"
str2=""
if [ -n $str1 ]
then
echo "str1 is not null"
else
echo "str1 is null"
fi if [ -z $str2 ]
then
echo "str2 is null"
else
echo "str2 is not null"
fi

(我自己写的代码,发现str中不能有空格,否则无法判断)

c.文件比较
test命令能够测试Linux文件系统上的文件状态和路径。
-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新
file2 -ot file2 检查file1是否比file2旧
如果想将文件写到一个目录下,或试图改变到目录位置之前,最好检查一下-d

#!/bin/bash
#look before you leap
if [ -d $HOME ]
then
echo "Your HOME directory exists"
cd $HOME
ls -l
else
echo "There is a problem with your HOME directory"
fi

(检查一个目录是否存在,存在就做一些事情,不存在就提示错误。)
在脚本中使用文件或目录之前,-e比较能够检查它们是否存在

#!/bin/bash
# check if a directory exists
if [ -e $HOME ]
then
echo "Your home directory exists"
#check if file exists in the directory
if [ -e $HOME/testing ]
then
echo "$HOME/tesing exist in the directory"
date >>$HOME/testing
else
echo "Create a new file"
date >$HOME/testing
fi
else
echo "Your home directory doesn't exists"
fi

(这里稍微复杂了一点,用了嵌套,并且注释很清晰,提示很到位)
-f检测文件

#!/bin/bash
#testing if a file
if [ -e $HOME ]
then
echo "The obj exist,if it is a file?"
if [ -f $HOME ]
then
echo "Yes,it is a file!"
else
echo "No,it is not a file!"
if [ -f $HOME/testing ]
then
echo "But $HOME/testing is a file!"
else
echo "$HOME/testing is not a file too!"
fi
fi
else
echo "The obj doesn't exist"
fi

(这个好无聊,不过展示了一个流程,逐级向下进行查询)
是否能读
在尝试从文件中读取数据之前,通常首先检查一下是否能读取文件。-r

#!/bin/bash
#testing if can read a file
pwfile=/etc/shadow
#first,check if it is a file
if [ -f $pwfile ]
then
#now test if you can read it
if [ -r $pwfile ]
then
tail $pwfile
else
echo "sorry,the file can be read."
fi
else
echo "sorry,it is not a file."
fi

(注意变量使用是一定要加上$符,不然就会出错)

检查空文件
应该用-s比较来检查文件是否为空,尤其是想删除文件时。注意,-s比较成功时,它表明文件包含数据

#!/bin/bash
#testing a file is empty
file=testfile
touch $file if [ -s $file ]
then
echo "The $file exists and has data in it"
else
echo "The $file doesn't exist or is empty"
fi date >$file
if [ -s $file ]
then
echo "The $file exists and has data in it"
else
echo "The $file doesn't exist or is empty"
fi

(真表示有数据,假表示无数据)

检查是否能够写入数据-w

#!/bin/bash
#checking if a file if writeable
logfile=$HOME/logtest
touch $logfile
chmod u-w $logfile
now=`date +%Y%m%d-%H%M` if [ -w $logfile ]
then
echo "The program ran at:$now">>$logfile
echo "The first attempt succeeded"
else
echo "The first attempt failed"
fi chmod u+w $logfile
if [ -w $logfile ]
then
echo "The program ran at:$now">>$logfile
echo "The second attempt succeeded"
else
echo "The second attempt failed"
fi

(这里的判断有点问题,需要再研究)

..
检查文件日期

#!/bin/bash
#testing file dates
if [ ./test1 -nt ./test10 ]
then
echo "The test1 file is newer than test10"
else
echo "The test10 file is newer than test1"
fi if [ ./test1 -ot ./test10 ]
then
echo "The test1 file is older than test10"
else
echo "The test10 file is older than test1"
fi

(比较两个文件的新旧)

复合条件检查
&&
||

#!/bin/bash
#testing compound comparisons
if [ -d $HOME ] && [ -x $HOME/testing ]
then
echo "The file exists and you can execute it"
else
echo "You can't execute the file"
fi

(并列执行)

if-then的高级特征
(())双圆括号,可以进行复杂的算术操作
val++ 后增量
val-- 后减量
++val 前增量
--val 前减量
!     逻辑否定
~     取反
**    取幂
<<    左移
>>    右移
&
|
&&    逻辑与
||    逻辑或

#!/bin/bash
#using double parenthesis
val1=
if (($val1**>))
then
((val2 =$val1**))
echo "The square of $val1 is $val2"
fi

(双圆括号里面的内容更加智能,会识别很多东西,不用总是空格空格的编写代码了)

[[]]双方括号
可以进行字符串比较,更加智能
模式匹配,也就是正则表达式可以更好的使用

#!/bin/bash
# using pattern matching
if [[ $USER==r* ]]
then
echo "Hello $USER"
else
echo "Sorry,I don't know you"
fi

(规范编写)

case使用

#!/bin/bash
#using the case command
case $USER in
rich | barbara)
echo "Welcome,$USER"
ehco "Please enjoy your visit";;
testing)
echo "Special testing account";;
jessica)
echo "Don't forget to log off when you're done";;
root)
echo "Welcome,Manager";;
*)
echo "Sorry,you're not allowed here";;
esac

Linux&shell之结构化命令的更多相关文章

  1. Linux&shell之结构化命令进阶

    写在前面:案例.常用.归类.解释说明.(By Jim) for命令重复一系列的命令是一种常见的编程实践. #!/bin/bash # basic for command for test in A B ...

  2. shell的结构化命令

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

  3. shell初级-----结构化命令

    if-then语句 bash shell的if语句会执行if后面的那个命令,如果该命令的退出码状态为0会执行then部分的命令,如果是其他值不会执行. 格式如下: if command then co ...

  4. Shell编程—结构化命令(2)

    1for命令 for命令的基本格式: for var in list do commands done 在list参数中,你需要提供迭代中要用到的一系列值. 1.1读取列表中的值 例子: $ vim ...

  5. Shell编程—结构化命令

    1使用if-then语句 f-then语句有如下格式. if command then commands fi bash shell的if语句会运行if后面的那个命令.如果该命令的退出状态码是0(该命 ...

  6. linux shell脚本使用结构化命令

    内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...

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

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

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

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

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

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

随机推荐

  1. Project Euler problem 62

    题目的大意很简单 做法的话. 我们就枚举1~10000的数的立方, 然后把这个立方中的有序重新排列,生成一个字符串s, 然后对于那些符合题目要求的肯定是生成同一个字符串的. 然后就可以用map来搞了 ...

  2. [AWS] Install the AWS cli

    On Windows, just download the installer and install it. Configure: aws configure In your aws console ...

  3. PHP 函数的“引用返回”概念释疑(转)

    很多时候我们会看到这样的代码(出自 CI 框架源码): 1 $class =& load_class('a','b'); 我们都知道其中的'&'是指引用,但是它的作用是什么呢?它能够解 ...

  4. [转] Linux中gcc,g++常用编译选项

    http://blog.sina.com.cn/s/blog_5ff2a8a201011ro8.html gcc/g++ 在执行编译时,需要4步 1.预处理,生成.i的文件[使用-E参数] 2.将预处 ...

  5. iOS异步图片加载优化与常用开源库分析

    网络图片显示大体步骤: 1.下载图片: 2.图片处理(裁剪,边框等): 3.写入磁盘: 4.从磁盘读取数据到内核缓冲区: 5.从内核缓冲区复制到用户空间(内存级别拷贝): 6.解压缩为位图(耗cpu较 ...

  6. C#使用DataSet类、DataTable类、DataRow类、OleDbConnection类、OleDbDataAdapter类编写简单数据库应用

    //注意:请使用VS2010打开以下的源代码. //源代码地址:http://pan.baidu.com/s/1j9WVR using System; using System.Collections ...

  7. Volley的基本使用(转)

    Volley是Google在2003年的I/O大会上推出的通信框架,结合了AsyncHttpClient和Universal-Image-Loader的优点——简化了http的使用 + 异步加载图片的 ...

  8. 关于 yii 验证码显示, 但点击不能刷新的处理

    先说说 render 与 renderPartial, 各位看官, 先别走, 我没跑题, 这个问题如果需要解决, 关键就在 render 与 renderPartial 的区别. renderPart ...

  9. AsyncTask 解析

    [转载自 http://blog.csdn.net/yanbober ] 1 背景 Android异步处理机制一直都是Android的一个核心,也是应用工程师面试的一个知识点.前面我们分析了Handl ...

  10. Memento 备忘录 快照模式

    简介 定义: 在不破坏封装的前提下,捕获一个对象的[内部状态],并在该对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存的状态. 角色: 发起人Originator:要被备份的成员,它提供一创 ...