shell中的判断语句
1、字符串判断
str1 = str2 当两个串有相同内容、长度时为真
str1 != str2 当串str1和str2不等时为真
-n str1 当串的长度大于0时为真(串非空,变量)
-z str1 当串的长度为0时为真(空串)
str1 当串str1为非空时为真
2、数字的判断
int1 -eq int2 两数相等为真
int1 -ne int2 两数不等为真
int1 -gt int2 int1大于int2为真
int1 -ge int2 int1大于等于int2为真
int1 -lt int2 int1小于int2为真
int1 -le int2 int1小于等于int2为真
3、文件的判断
-r file 用户可读为真 read
-w file 用户可写为真 write
-x file 用户可执行为真 executable
-f file 文件为正规文件为真
-d file 文件为目录为真
-c file 文件为字符特殊文件为真
-b file 文件为块特殊文件为真
-s file 文件大小非0时为真 size
-t file 当文件描述符(默认为1)指定的设备为终端时为真
4、复杂逻辑判断
-a 与
-o 或
! 非
使用实例:
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var/log/httpd/access.log"
#这里的 -x 参数判断 $myPath 是否存在并且是否具有可执行权限
if [ ! -x "$myPath"]; then
mkdir "$myPath"
fi
#这里的-d 参数判断$myPath是否存在
if [ ! -d "$myPath"]; then
mkdir "$myPath"
fi
#这里的-f参数判断$myFile是否存在
if [ ! -f "$myFile" ]; then
touch "$myFile"
fi
#其他参数还有-n,-n是判断一个变量是否是否有值
if [ ! -n "$myVar" ]; then
echo "$myVar is empty"
exit 0
fi
#两个变量判断是否相等
if [ "$var1" == "$var2" ]; then
echo '$var1 eq $var2'
else
echo '$var1 not eq $var2'
fi
shell中的判断语句的更多相关文章
- shell中条件判断if中的-z到-d的意思
shell中条件判断if中的-z到-d的意思 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 ...
- shell 中如何判断前一个命令是否执行成功
shell 中如何判断前一个命令是否执行成功 通过判断返回值来解决: if [ $? -eq 0 ];then 命令正确的分支 else 命令失败的分支 fi
- 在shell中如何判断字符串是否为有效的IP地址【转】
转自 在shell中如何判断字符串是否为有效的IP地址_echoisecho_新浪博客http://blog.sina.com.cn/s/blog_53a844e50100xxus.html 近来需要 ...
- shell中条件判断if中的-z到-d
shell中条件判断if中的-z到-d的意思 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真.[ -c FILE ] 如果 ...
- Linux - 简明Shell编程04 - 判断语句(If)
脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 #!/bin/bash var=$1 # 将脚本的第一 ...
- Shell - 简明Shell入门04 - 判断语句(If)
示例脚本及注释 #!/bin/bash var=$1 # 将脚本的第一个参数赋值给变量var if test $var # test - check file types and compare va ...
- shell脚本if判断语句报错[: too many arguments的两种原因
shell脚本,if判断语句报错[: too many arguments 我遇到过两种情况: 1.第一中情况就是网上大家说的,字符串变量中可能存在空格,shell解析时将其认为是多个参数,再进行判断 ...
- shell 中的判断
一.if的基本语法: if [ command ];then 符合该条件执行的语句 elif [ command ];then 符合该条件执行的语句 else 符合该条件执行的语句 ...
- shell 脚本之判断语句 if 详解
使用 Linux 系统这么长时间,对 shell 脚本也算是比较熟悉.其实不管是搞开发,还是搞运维,shell 脚本都是必备的基本技能.这次抽时间好好总结一下 shell 方面的知识,综合的再学习一下 ...
随机推荐
- Codeforces Round #279 (Div. 2) C. Hacking Cypher (大数取余)
题目链接 C. Hacking Cyphertime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...
- 2018ICPC徐州区域赛网络赛G(VECTOR+SET,模拟)
#include<bits/stdc++.h>using namespace std;int x,y;vector<int>v1,v2;long long solve(vect ...
- 自动化运维工具之Cobbler
一.anaconda anaconda负责安装系统向导,默认为GUI界面,如果我们使用ks自动应答安装的话建议使用TUI界面来安装 在安装启动界面我们可以按esc键来自己手动指定安装启动选项 anac ...
- ldap第二天-yum安装LDAP + phpLDAPadmin
1.安装LDAP服务器和客户端,migrationtools工具包 yum install -y openldap-servers openldap-clients migrationtools 2. ...
- yum 缓存包到本地
yum install –downloadonly –downloaddir=/root/mypackages/ vim 说明: --downloadonly 只下载 --downloaddir 下载 ...
- Ubuntu下rsyslog审计用户bash操作命令、收集、写入MySQL
服务端 2台服务端:10.25.109.64.10.45.18.133 1.rsyslog最新版本安装 sudo add-apt-repository ppa:adiscon/v8-stable su ...
- Luogu P3265 [JLOI2015]装备购买
好吧刚开始不知道自己在写什么,,,后来写了线性方程组,又过了一天一上午终于明白了... 当然题意很显然:求代价最小的极大线性无关组. 那就高斯消元(好吧刚开始我不会用它来解这道题qwq) 第一个循环是 ...
- BZOJ 1924 && Luogu P2403 [SDOI2010]所驼门王的宝藏 恶心建图+缩点DP
记住:map一定要这么用: if(mp[x[i]+dx[j]].find(y[i]+dy[j])!=mp[x[i]+dx[j]].end()) add(i,mp[x[i]+dx[j]][y[i]+dy ...
- Unity 碰撞的条件
- 交换函数swap的三种实现方法
http://blog.csdn.net/GarfieldEr007/article/details/48314295 本文采用三种方式实现两个数之间的交换,分别是①借助辅助变量temp的swap函数 ...