Linux Shell编程(1): 条件语句
1.if—then
#!/bin/bash
if date 如果命令运行成功(退出码为0),则then部分的命令被执行
then
echo "good"
fi
2.if—then—else
#!/bin/bash
if hunter
then
echo "good"
else
echo "bad" if语句中退出码非0,则执行else部分语句
fi
3.elif
#!/bin/bash
if hunter
then
echo "command not found"
elif date 多重If判断
then
echo "date"
fi
4.test命令 与其它编程语言思维一样的方式
#!/bin/bash
if test 1 -eq 2 test命令列出条件成立则退出并返回状态码0,命令等同if [ 1 -eq 2]
then
echo "right"
else
echo "wrong"
fi
5.条件比较
1)数据比较
n1 -eq n2 相等
n1 -ge n2 大于或等于
n1 -gt n2 大于
n1 -le n2 小于等于
n1 -lt n2 小于
n1 -ne n2 不等于
2)字符串比较
str1 = str2
str1 != str2
str1 < str2
str1 > str2
-n str1 检查str1的长度是否非0
-z str1 检查str1的长度是否为0
#!/bin/bash
str1=good
str2=wrong
if [ $str1 \> $str2 ] 大于号需要转义,否则脚本会当重定向符号
then echo "$str1 is greater than $str2"
else
echo "$str1 is less than $str2"
fi
#!/bin/bash
str=
if [ -z "$str" ] 判断字符串是否为空,空的或未初始化的变量对Shell脚本很致命
then
echo "empty"
else
echo "not empty"
fi
4)复合条件&&与、||或
#!/bin/bash
if [ -e str ] && [ -f zero ]
then
echo "file all exist"
fi
5)双圆括号
#!/bin/bash
str1=good
str2=wrong
if (( $str1 > $str2 )) 双圆括号中表达式里的大于号不用转义
then echo "$str1 is greater than $str2"
else
echo "$str1 is less than $str2"
fi
6)双方括号
#!/bin/bash
- if [[ $USER == r* ]] 支持通配符模式
- then
- echo "welcome root user"
- else
- echo "sorry"
- fi
7)case分支
#!/bin/bash
case $USER in
root|hunterno4)
echo "welcome root user";; 尾部两个分号
mysql)
echo "welcome to database";;
surfftp)
echo "nice to meet you";;
*) 都不匹配时的default语句
echo "i don't know you";;
esac
- #! /bin/bash
- str1=
- str2=
- if [ $str1 -gt $str2 ] #大于号需要转义,否则脚本会当重定向符号
- then echo "$str1 is greater than $str2"
- elif [ $str1 -eq $str2 ]
- then echo "$str1 is equal to $str2"
- else
- echo "$str1 is less than $str2"
- fi
- str="abc"
- if [ -z $str ]
- then
- echo "empty"
- else
- echo "not empty, the string is: "$str
- fi
- str3="aaa"
- if [[ $str3="aaa" ]]
- then echo "str3($str3) equals \"aaa\""
- else
- echo "not equals"
- fi
- if test =
- then echo "test "test" "
- else echo "not equals"
- fi
Linux Shell编程(1): 条件语句的更多相关文章
- 02 shell编程之条件语句
Shell编程之条件语句 学习目标: 掌握shell脚本条件测试 掌握if语句编程 目录结构: 条件测试 条件测试概述 l 对特定的条件进行判断,以决定如何执行操作 l 测试的方法 方法1:tes ...
- if语句 条件测试 shell编程之条件语句
shell 编程之条件语句一.条件测试 ① test命令 测试 ② 文件测试 ③ 整数值比较 ④ 字符串比较 ⑤ 逻辑测试二.if语句的结构 单分支结构 双分支结构 多分支结构三.ca ...
- Shell编程之条件语句:if、case语句
Shell编程之条件语句:if.case语句 一.条件测试 1)test命令测试 2)整数值比较 ...
- shell编程之条件语句
目录: 一.条件测试 1.test命令测试 2.文件测试 3.字符串比较 4.逻辑测试 二.if语句 1.if单分支语句 2.if双分支语句 3.if多分支语句 三.case语句 case多分支语句 ...
- Linux - 简明Shell编程05 - 条件语句(Case)
脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 #!/bin/bash var=$1 # 将脚本的第一 ...
- linux —— shell 编程(文本处理)
导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 基本文本处理 流编辑器sed aw ...
- linux —— shell 编程(编程语法)
导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 再识变量 函数 条件语句 循环语句 ...
- linux shell编程总结
linux shell编程总结 本周学习了unix/linux shell编程,参考的是<LINUX与UNIX Shell 编程指南>,David Tansley著:徐焱,张春萌等译,由机 ...
- linux shell 编程参考
#!/bin/bash my_fun() { echo "$#" } echo 'the number of parameter in "$@" is '$(m ...
随机推荐
- 【BZOJ】【1965】SHUFFLE 洗牌
扩展欧几里德+快速幂 每次转换位置:第x位的转移到2*x %(n+1)这个位置上 那么m次后就到了(2^m)*x %(n+1)这个位置上 那么找洗牌m次后在 l 位置上的牌就相当于解线性模方程: (2 ...
- WinForm点击按钮在对应的panel里画图
panel在form1里,button在form1上方,panel在下面. 主要是在button1的click时间获取panel的画笔. 下面的不行,在panel里获取画笔,然后传到button1,根 ...
- texCUBE() to CubemapSampler.Sample()
update dx9 to dx11 refers to CUBEMAP sampler texCUBE(CubeMpaSampler,normal) maybe change to Cubema ...
- Unity3D 与 objective-c 之间数据交互。iOS SDK接口封装Unity3D接口
原地址:http://www.cnblogs.com/qingjoin/p/3638915.html Unity 3D 简单工程的创建.与Xcode 导出到iOS 平台请看这 Unity3D 学习 创 ...
- tornado的cookie和secure cookie
tornado里面有关几个cookie的处理,在web.py文件里. get_cookie, set_cookie普通的设置cookie, clear_cookie, clear_all_cookie ...
- poj 2485 Highways(最小生成树,基础,最大边权)
题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...
- .net中的认证(authentication)与授权(authorization)
“认证”与“授权”是几乎所有系统中都会涉及的概念,通俗点讲: 1.认证(authentication) 就是 "判断用户有没有登录?",好比windows系统,没登录就无法使用(不 ...
- Linux解压 tar命令
tar [-cxtzjvfpPN] 文件与目录 ....参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个压缩文件的参数指令!-t :查看 tarfile 里面的文件! ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- 248. Strobogrammatic Number III
题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...