Linux&shell之结构化命令
写在前面:案例、常用、归类、解释说明。(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之结构化命令的更多相关文章
- Linux&shell之结构化命令进阶
		
写在前面:案例.常用.归类.解释说明.(By Jim) for命令重复一系列的命令是一种常见的编程实践. #!/bin/bash # basic for command for test in A B ...
 - shell的结构化命令
		
shell在逻辑流程控制这里会根据设置的变量值的条件或其他命令的结果跳过一些命令或者循环执行的这些命令.这些命令通常称为结构化命令 1.if-then语句介绍 基本格式 if command then ...
 - shell初级-----结构化命令
		
if-then语句 bash shell的if语句会执行if后面的那个命令,如果该命令的退出码状态为0会执行then部分的命令,如果是其他值不会执行. 格式如下: if command then co ...
 - Shell编程—结构化命令(2)
		
1for命令 for命令的基本格式: for var in list do commands done 在list参数中,你需要提供迭代中要用到的一系列值. 1.1读取列表中的值 例子: $ vim ...
 - Shell编程—结构化命令
		
1使用if-then语句 f-then语句有如下格式. if command then commands fi bash shell的if语句会运行if后面的那个命令.如果该命令的退出状态码是0(该命 ...
 - linux shell脚本使用结构化命令
		
内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...
 - 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令
		
许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then ...
 - Linux编程 23 shell编程(结构化条件判断 命令if -then , if-then ... elif-then ...else,if test)
		
一.概述 在上一篇里讲到了shell脚本,shell按照命令在脚本中出现的顺序依次进行处理,对于顺序操作已经足够了,但许多程序要求对shell脚本中的命令加入一些逻辑流程控制,这样的命令通常叫做 结构 ...
 - Shell 语法之结构化命令(流程控制)
		
许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...
 
随机推荐
- Java读书笔记二(封装类)
			
1.介绍 都知道java中基本数据类型有非常多,比方string,int--,可是基本数据类型与对象之间是不同的.但非常多情况下,我们希望将基本数据类型当作对象使用,这时候就须要用到封装类. 2.封装 ...
 - Android抓包工具Fiddler抓取数据
			
1.手机端设置 2.Fiddler设置监听 Tools/Fiddler options 是否允许监听到https(Fiddler默认只抓取http格式的),首次点击会弹出是否信任fiddler证书和安 ...
 - ios 打电话结束返回到应用中
			
在我们做打电话这个功能时,我们常常是调用这个方法: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel ...
 - 【开源java游戏框架libgdx专题】-10-核心库-Viewport
			
Viewport类,又称为视口类,主要负责管理游戏相机,处理游戏世界坐标与布景层坐标的换算关系.在移动端开发,不像PC端容易.因为要适配不同分辨率的设备.libgdx处理不同的设备屏幕时,用视口处理舞 ...
 - 关于AuthorizeAttribute使用
			
在开发中,假如你只对一个角色进行权限处理,你可以这么写 class ActionAuthAttribute : AuthorizeAttribute { private RoleType _roleT ...
 - 排名最重要的三个优化阶段分析  --------------------->>转至(卧牛SEO/武汉SEO http://blog.sina.com.cn/zhengkangseo )
			
网站排名,不是一两天能够决定的.要想取得好的排名,得分时间分阶段地做排名,网站优化分前期,中期,后期,怎么来区别不同的阶段该用怎样的优化手段.今晚SEO研究中心创始人Moon老师分享:排名最重要的三个 ...
 - jQuery的live()方法对hover事件的处理示例
			
hover([over,]out) 一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法 当鼠标移动到一个匹配的元素上面时,会触发指定的第一个函数. 当鼠标移出这个元素时,会触发指定的第二个 ...
 - java中的数据库事务处理
			
/*java使用事务处理,首先要求数据库支持事务,如使用MYSQL的事务功能,就要求mysql的表类型为Innodb,*/ /*InnoDB,是MySQL的数据库引擎之一 与传统的ISAM与MyISA ...
 - asp.net中调用COM组件发布IIS时常见错误 80070005解决方案
			
很多人在.net开发Web站点中引用了COM组件,调试时一切正常,但一发布到IIS下就提示如下错误: 检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 80070005 ...
 - android 中Log - 简单使用
			
例如,我们可以使用'Log.d'进行Debug,在java代码中输入Log.d(String tag, String message),tag为自己命名的tag,message为待输出的信息.然后打开 ...