if结构

#!/bin/env bash

if [ $ -gt  ]
then
echo "$1 is positive"
elif [ $ -lt ]
then
echo "$1 is negative"
else
echo "$1 is zero"
fi

while读取文件

while read aa
do
echo "$aa"
done < aaa.s

数字比较

#等于
$num1 -eq $num2
#不等于
$num1 -ne $num2
#小于
$num1 -lt $num2
#小于或等于
$num1 -le $num2
#大于
$num1 -gt $num2
#大于等于
$num1 -ge $num2

字符串比较

#字符串长度是否为0
-z $str
#字符串长度是否不为0
-n $str
#字符串是否相等
$str1 == $str2
#字符串是否不等
$str1 != $str2

使用举例:

if [ -z $str ]
then
echo "string is empty"
elif [ -n $str ]
then
echo "string not empty"
fi [ -z $str ] && echo "yes" || echo "no" //获取命令的结果方法一
ret=$([ -z $str ] && echo "yes" || echo "no") //获取命令的结果方法二
ret1=`[ -z $str ] && echo "yes" || echo "no"` echo "ret=$ret, ret1=$ret1"

文件比较

#文件名是否存在
-e $file
#是否是文件
-f $file
#是否是目录
-d $file
#是否是符号链接
-L $file
#文件是否可读
-r $file
#文件是否可写
-w $file
#文件是否可执行
-x $file

多个条件判断,即与、或

#与
if [ -e $file -a -f $file ]
then
echo "$file is exists, and is file"
fi #或
if [ $num -eq - -o $num -gt ]
then
echo "$num is ok"
fi

Linux shell 脚本小记的更多相关文章

  1. Linux shell 脚本小记2

    .从文件读取 while read line do echo "line=$line" done < file.txt .将字符串转换为数组,并进行遍历 str=" ...

  2. Linux shell脚本编程(三)

    Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...

  3. Linux shell脚本编程(二)

    Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...

  4. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  5. Linux Shell 脚本入门

    linux shell 脚本格式 #!/bin/sh#..... (注释)命令...命令... 使用vi 创建完成之后需设置权限 chmod +x filename.sh 执行命令: ./filena ...

  6. Linux Shell脚本入门--cut命令

    Linux Shell脚本入门--cut命令 cut cut 命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields &l ...

  7. Linux Shell脚本攻略 读书笔记

    Linux Shell脚本攻略 读书笔记 这是一本小书,总共253页,但内容却很丰富,书中的示例小巧而实用,对我这样总是在shell门前徘徊的人来说真是如获至宝:最有价值的当属文本处理,对这块我单独整 ...

  8. 阿里Linux Shell脚本面试25个经典问答

    转载: 阿里Linux Shell脚本面试25个经典问答 Q:1 Shell脚本是什么.它是必需的吗? 答:一个Shell脚本是一个文本文件,包含一个或多个命令.作为系统管理员,我们经常需要使用多个命 ...

  9. Linux Shell脚本教程

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

随机推荐

  1. 在 linux x86-32 模式下分析内存映射流程

    前言 虚拟内存机制已经成为了现代操作系统所不可缺少的一部分, 不仅可以为每个程序提供独立的地址空间保证安全性,更可以通过和磁盘的内存交换来提高内存的使用效率.虚拟内存管理作为linux 上的重要组成部 ...

  2. JQuery window.opener

    $('#Save').click(function () {    var parent = $(parent.document.body);    $(parent).find('input#add ...

  3. jeecms子栏目或者文章页导航父栏目选中解决方法

    jeecms在子栏目或者文章页导航父栏目选中,看例子 <div class="nav"> <ul> [@cms_channel_list ] <li ...

  4. Android Error:You must supply a layout_width attribute……

    出现这种情况的可能原因目前本人碰到的有: 1:在xml文件中某个属性名或者属性值写错,请务必仔细检查你有没有写错某个拼写. 2:当你在,比如TextView中,没有声明layout_width,经测试 ...

  5. Mvc中把list从View传入Controller

    public class User { public string Name { get; set; } public bool IsChecked { get;set;} public int Ag ...

  6. Python中docstring文档的写法

    该写法根据Python的PEP 257文档总结. 类的函数称为方法(method),模块里的函数称为函数(function) 每一个包,模块,类,函数,方法都应该包含文档,包括类的__init__方法 ...

  7. mac常用设置

    1.修改mac主机名 系统偏好设置->共享->电脑名称 ,编辑就可以了. sudo scutil --set HostName hostname 这个是修改主机名 sudo scutil ...

  8. SetTimeOut jquery的作用

    1. SetTimeOut() 1.1 SetTimeOut()语法例子 1.2 用SetTimeOut()执行Function 1.3 SetTimeOut()语法例子 1.4 设定条件使SetTi ...

  9. DC-EPC小结

    今晚上修完了最后2个学时的EPC(课程主页),这意味着本学期的DC和共20个学时的EPC到此结束,这有可能是我人生中最后一次上英语课. Tom是我DC课的老师,EPC起于Tom和Micheal的Deb ...

  10. 64bit Ubuntu, Android AAPT, R.java

    Ubuntu 13.10 aapt: error while loading shared libraries: libstdc++.so.6: cannot open shared object f ...