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. 创建本地Ubuntu镜像

    参考文档 http://www.howtoforge.com/local_debian_ubuntu_mirror 安装服务 : sudo apt-get install apt-mirror apa ...

  2. mysql数据库表格导出为excel表格

    在本地数据库中操作如下: 由于excel表格的编码是GBK,所以导出时要加一个设置字符编码: select * from 某个表 into outfile 'd:/文件名.xls' CHARACTER ...

  3. TextView中gravity属性值测定

    Attributes Explain top 不改变控件大小,对齐到容器顶部 bottom 不改变控件大小,对齐到容器底部 left 不改变控件大小,对齐到容器左侧 right 不改变控件大小,对齐到 ...

  4. C# 获得手机归属地功能

    今天通过查资料了解到web的页面抓取功能,应用HttpWebRequest和HttpWebResponse功能,从http://www.showji.com网站中抓取归属地信息 应该说这个方法是从别的 ...

  5. 第六周 N题

    Description As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (h ...

  6. Python标准库 urllib2 的使用

    1.Proxy 的设置 urllib2 默认会使用环境变量 http_proxy 来设置 HTTP Proxy. 如果想在程序中明确控制 Proxy,而不受环境变量的影响,可以使用下面的方式 impo ...

  7. ThinkPHP/Common/extend.php

    <?php // +---------------------------------------------------------------------- // | ThinkPHP [ ...

  8. yii2.0 控制器加载不同的user组件

     Yii::$app->user->id  Yii::$app->user2->id Yii::$app->admin->id          identityC ...

  9. LintCode-Search 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...

  10. HttpWebRequest,HttpWebResponse的用法和用途

    1.用途:HettpWebRequest,HettpWebResponse用途和webServers的作用差不多,都是得到一个页面传过来的值.HttpWebRequest 2.用法:--------- ...