`
WaltersRen
  • 浏览: 3317 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

linux shell if_then

阅读更多
比如比较字符串、判断文件是否存在及是否可读等,通常用"[]"来表示条件测试。

注意:这里的空格很重要。要确保方括号的空格。

if ....; then
....
elif ....; then
....
else
....
fi
[ -f "somefile" ] :判断是否是一个文件
[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限
[ -n "$var" ] :判断$var变量是否有值
[ "$a" = "$b" ] :判断$a和$b是否相等
-r file     用户可读为真
-w file     用户可写为真
-x file     用户可执行为真
-f file     文件为正规文件为真
-d file     文件为目录为真
-c file     文件为字符特殊文件为真
-b file     文件为块特殊文件为真
-s file     文件大小非0时为真
-t file     当文件描述符(默认为1)指定的设备为终端时为真
含条件选择的shell脚本 对于不含变量的任务简单shell脚本一般能胜任。但在执行一些决策任务时,就需要包含if/then的条件判断了。shell脚本编程支持此类运算,包括比较运算、判断文件是否存在等。
基本的if条件命令选项有: - eq —比较两个参数是否相等(例如,if [ 2 –eq 5 ])
-ne —比较两个参数是否不相等
-lt —参数1是否小于参数2
-le —参数1是否小于等于参数2
-gt —参数1是否大于参数2
-ge —参数1是否大于等于参数2
-f — 检查某文件是否存在(例如,if [ -f "filename" ])
-d — 检查目录是否存在
几乎所有的判断都可以用这些比较运算符实现。脚本中常用-f命令选项在执行某一文件之前检查它是否存在。

下面举二个例子吧,以方便大家的理解。

1、判断文件是否存在


#!/bin/sh
# 判断文件是否存在
# link:www.jb51.net
# date:2013/2/27
YACCESS=`date -d yesterday +%Y%m%d`
FILE="access_$YACCESS.log.tgz"
cd /data/nginx/logs
if [ -f "$FILE" ];then
echo "OK"
else
echo "error $FILE" > error.log
mail -s "$FILE backup fail" test@126.net <error.log
fi



#!/bin/sh
# 清除相关文件,并按时间段记录日志
# link:www.jb51.net
# date:2013/2/27
#
DIR=/data/img_cache
DAY=`date +"%Y-%m-%d %H:%M"`
NUM=`ls $DIR |wc -l`
DIRNAME=`ls $DIR| grep leveldb | head -n 1 | awk '{print $NF}'`
if [[ $NUM -gt 3 ]];then
  rm -rf $DIR/$DIRNAME
  echo "---------$DAY----($DIR)-----------------------" >> /tmp/img_cache.log
  echo "$DIRNAME Deleted successful" >> /tmp/img_cache.log
fi
分享到:
评论

相关推荐

    Learning Linux Shell Scripting 2nd Edition pdf

    You'll then take a step ahead and learn new and advanced topics in Shell scripting, such as decision making, starting up a system, and customizing a Linux environment. You will also learn about grep,...

    Linux Shell Scripting Cookbook

    follow recipes on new features on Linux, particularly, Debian-based, to help you accomplish even the most complex tasks with ease, Who This Book Is For, If you are a beginner or an intermediate Linux ...

    Linux Shell Scripting Cookbook - Third Edition

    If you are a beginner or an intermediate Linux user who wants to master the skill of quickly writing scripts and automate tasks without reading the entire man pages, then this book is for you....

    Mastering Linux Shell Scripting 2nd Edition

    shell. Then, you'll learn how to write a simple bash script and how to edit your bash script using Linux editors. Following this, you will learn how to define a variable and the visibility of a ...

    LINUX与UNIX_Shell编程指南

    18.3 if then else语句 161 18.3.1 简单的if语句 162 18.3.2 变量值测试 162 18.3.3 grep输出检查 163 18.3.4 用变量测试grep输出 163 18.3.5 文件拷贝输出检查 164 18.3.6 当前目录测试 164 18.3.7 文件权限...

    用Linux Command Shell模拟DOS命令

    Linux的shell scripting 是一种自动完成...而对于那些需要一定程度决策的工作来说,if/then条件假设就成为必须的了。有了这些基本的脚本知识,你就能够很容易地把绝大部分常用的Windows命令行转化成Linux可用的脚本。

    Linux and Unix Shell Programming

    If you are new to UNIX and Linux or if you are a power user in waiting then this book is for you. Most shell programming books merely annotate manual pages and syntax , but in this book users of all ...

    Linux_unix_shell第一部分编程(共两部分)

    18.3 if then else语句 161 18.3.1 简单的if语句 162 18.3.2 变量值测试 162 18.3.3 grep输出检查 163 18.3.4 用变量测试grep输出 163 18.3.5 文件拷贝输出检查 164 18.3.6 当前目录测试 164 18.3.7 文件权限测试 ...

    LINUX与UNIX_Shell编程指南(下)

    18.3 if then else语句 161 18.3.1 简单的if语句 162 18.3.2 变量值测试 162 18.3.3 grep输出检查 163 18.3.4 用变量测试grep输出 163 18.3.5 文件拷贝输出检查 164 18.3.6 当前目录测试 164 18.3.7 文件权限测试 ...

    LINUX与UNIX_SHELL编程指南2

    控制流结构 160 &lt;br&gt;18.1 退出状态 160 &lt;br&gt;18.2 控制结构 160 &lt;br&gt;18.2.1 流控制 161 &lt;br&gt;18.2.2 循环 161 &lt;br&gt;18.3 if then else语句 161 &lt;br&gt;18.3.1 简单的if语句 162 &lt;br&gt;18.3.2 变量值...

    linux shell流程控制语句实例讲解(if、for、while、case语句实例)

    fi如果对于:条件测试语句不是很清楚,可以参考:linux shell 逻辑运算符、逻辑表达式详解shell命令,可以按照分号分割,也可以按照换行符分割。如果想一行写入多个命令,可以通过“’;”分割。如: 代码如下:...

    linux shell中 if else以及大于、小于、等于逻辑表达式介绍

    if ….; then….elif ….; then….else….fi[ -f “somefile” ] :判断是否是一个文件[ -x “/bin/ls” ] :判断/bin/ls是否存在并有可执行权限[ -n “$var” ] :判断$var变量是否有值[ “$a” = “$b” ] :判断$...

    go tool Linux dir

    应用于Ubuntu等Linux系统环境,方便进入指定目录,方便查询包含关键字的文件 使用方法 拷贝go.py到 /usr/local/lib/... if [ -f $GO_SHELL_SCRIPT ] ; then source $GO_SHELL_SCRIPT fi unset GO_SHELL_SCRIPT }

    LINUX与UNIX SHELL编程指南(很全)

    18.3 if then else语句 161 18.3.1 简单的if语句 162 18.3.2 变量值测试 162 18.3.3 grep输出检查 163 18.3.4 用变量测试grep输出 163 18.3.5 文件拷贝输出检查 164 18.3.6 当前目录测试 164 18.3.7 文件权限测试 ...

    Linux shell编程指南

    18.3 if then else语句 161 18.3.1 简单的if语句 162 18.3.2 变量值测试 162 18.3.3 grep输出检查 163 18.3.4 用变量测试grep输出 163 18.3.5 文件拷贝输出检查 164 18.3.6 当前目录测试 164 18.3.7 文件权限测试 ...

    linux shell 编程教程

    linux shell编程 教程大全 目 录 译者序 前言 第一部分 shell 第1章 文件安全与权限 1 1.1 文件 1 1.2 文件类型 2 1.3 权限 2 1.4 改变权限位 4 1.4.1 符号模式 4 1.4.2 chmod命令举例 5 1.4.3 绝对模式 5 1.4.4 ...

    Linux Shell 流程控制

    linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case)。下面我将通过例子介绍下,各个语句使用方法。  一、shell条件语句(if用法)  if语句结构[if/then/elif/...

    Linux之shell中if语句–test命令

    复合条件测试3.if-then的高级特性 1.test命令的用法 Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符串和文件三个方面的测试。 test命令可以判断三类条件: 数值比较 字符串比较 文件比较 1.1...

    Linux与unix shell编程指南

    18.3 if then else语句 161 18.3.1 简单的if语句 162 18.3.2 变量值测试 162 18.3.3 grep输出检查 163 18.3.4 用变量测试grep输出 163 18.3.5 文件拷贝输出检查 164 18.3.6 当前目录测试 164 18.3.7 文件权限测试 ...

Global site tag (gtag.js) - Google Analytics