写在前面:案例、常用、归类、解释说明。(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. Appium官网Introduction

    英文官网:http://appium.io/introduction.html?lang=zh Appium 简介 Appium是一个开源的自动化测试工具,其支持iOS和安卓平台上的原生的,基于移动浏 ...

  2. android蓝牙4.0(BLE)开发之ibeacon初步

    一个april beacon里携带的信息如下 ? 1 <code class=" hljs ">0201061AFF4C0002159069BDB88C11416BAC ...

  3. 基于 Quartz 开发企业级任务调度应用--转

    Quartz 基本概念及原理 Quartz Scheduler 开源框架 Quartz 是 OpenSymphony 开源组织在任务调度领域的一个开源项目,完全基于 Java 实现.该项目于 2009 ...

  4. 数据存储简单了解(NSUserDefaults)

    数据存储-使用NSUserDefaults 两个类介绍: NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefault ...

  5. js获取当前url地址及参数

    介绍:设置或获取对象指定的文件名或路径. window.location.pathname //返回 设置或获取整个 URL 为字符串. window.location.href 设置或获取与 URL ...

  6. 那些年不错的Android开源项目(转)

    第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...

  7. UWP app HelloWorld 的创建

    步骤 1:在 Visual Studio 中创建新项目 启动 Visual Studio 2015 RC.将出现 Visual Studio 2015 RC 起始页. (从现在开始,我们将 Visua ...

  8. DataTable数据与Excel表格的相互转换

    using Excel = Microsoft.Office.Interop.Excel; private static Excel.Application m_xlApp = null; /// & ...

  9. 我的github今天大手笔分享,welcome——fork

    看项目名就知道是什么样子的demo,还包含了数据库文件,welcome fork!

  10. ASP.NET MVC 几种 Filter 的执行过程源码解析

    一.前言 之前也阅读过MVC的源码,并了解过各个模块的运行原理和执行过程,但都没有形成文章(所以也忘得特别快),总感觉分析源码是大神的工作,而且很多 人觉得平时根本不需要知道这些,会用就行了.其实阅读 ...