SHELL 详解
http://blog.csdn.net/vah101/article/details/6173488
( a=2;b=4;c=9; ) 子shell 环境
{ a=2;b=4;c=9; } 当前shelll环境
((整型算术表达式 )) 返回0 /1
[[条件表达试]] 0/1
[] 0/1
$(命令)= `命令` 返回结果
[root@monitor ~]# b=`date`
[root@monitor ~]# echo $b
Mon May 23 23:59:44 CST 2016
[root@monitor ~]# a=$(date)
[root@monitor ~]# echo $a
Mon May 23 23:59:01 CST 2016
$(())
[root@monitor ~]# echo $((2,4,5))
5
[[ ]] [] :常量运算符恒为真 [root@monitor ~]# [[ > || ]]
[root@monitor ~]# echo $? [root@monitor ~]# [[ > || ]]
[root@monitor ~]# echo $? [root@monitor ~]# [[ > ]]
[root@monitor ~]# echo $?
root@monitor ~]# [ 1 -a 2 ]
[root@monitor ~]# echo $?
0
[root@monitor ~]# [ 1 -a 0 ]
[root@monitor ~]# echo $?
0
[root@monitor ~]# [ 1 -o 0 ]
[root@monitor ~]# echo $?
0
command1 & command2 & command3 三个命令同时执行
command1; command2; command3 不管前面命令执行成功没有,后面的命令继续执行
command1 && command2 只有前面命令执行成功,后面命令才继续执行
[root@monitor ~]# (( && ))
[root@monitor ~]# echo $?
[root@monitor ~]# type cat
cat is /bin/cat
[root@monitor ~]# type [
[ is a shell builtin
[root@monitor ~]# type [[
[[ is a shell keyword []:中的逻辑符号 -o -a
[[ ]] :中的逻辑符号 && ||
(( )) [root@monitor ~]# (( && ))
[root@monitor ~]# echo $? [root@monitor ~]# (( && > ))
[root@monitor ~]# echo $? [root@monitor ~]# (( && ))
[root@monitor ~]# echo $?
(())结构计算并测试算数表达式的结果,退出码与[]相反 [true返回0,false返回1] (()) #返回1 (()) #返回0 ((>)) #返回0 ((>)) #返回1 ((-)) #返回1 ((/)) #大于1,返回0 ((/)) #小于1,返回1 ((/)) #报错,返回1
[root@monitor ~]# ((0))
[root@monitor ~]# echo $?
1
[root@monitor ~]# [ 0 ]
[root@monitor ~]# echo $?
0
[root@monitor ~]# [[ 0 ]]
[root@monitor ~]# echo $?
0
文件测试操作:
返回true,如果:
-e 文件存在
-a 文件存在(已被弃用)
-f 被测文件是一个regular文件(正常文件,非目录或设备)
-s 文件长度不为0
-d 被测对象是目录
-b 被测对象是块设备
-c 被测对象是字符设备
-p 被测对象是管道
-h 被测文件是符号连接
-L 被测文件是符号连接
-S(大写) 被测文件是一个socket
-t 关联到一个终端设备的文件描述符。用来检测脚本的stdin[-t0]或[-t1]是一个终端
-r 文件具有读权限,针对运行脚本的用户
-w 文件具有写权限,针对运行脚本的用户
-x 文件具有执行权限,针对运行脚本的用户
-u set-user-id(suid)标志到文件,即普通用户可以使用的root权限文件,通过chmod +s file实现
-k 设置粘贴位
-O 运行脚本的用户是文件的所有者
-G 文件的group-id和运行脚本的用户相同
-N 从文件最后被阅读到现在,是否被修改
f1 -nt f2 文件f1是否比f2新
f1 -ot f2 文件f1是否比f2旧
f1 -ef f2 文件f1和f2是否硬连接到同一个文件
二元比较操作符,比较变量或比较数字
整数比较:
-eq 等于 if [ "$a" -eq "$b" ]
-ne 不等于 if [ "$a" -ne "$b" ]
-gt 大于 if [ "$a" -gt "$b" ]
-ge 大于等于 if [ "$a" -ge "$b" ]
-lt 小于 if [ "$a" -lt "$b" ]
-le 小于等于 if [ "$a" -le "$b" ]
< 小于(需要双括号) (( "$a" < "$b" ))
<= 小于等于(...) (( "$a" <= "$b" ))
> 大于(...) (( "$a" > "$b" ))
>= 大于等于(...) (( "$a" >= "$b" ))
字符串比较:
= 等于 if [ "$a" = "$b" ]
== 与=等价
!= 不等于 if [ "$a" != "$b" ]
< 小于,在ASCII字母中的顺序:
if [[ "$a" < "$b" ]]
if [ "$a" /< "$b" ] #需要对<进行转义
> 大于
-z 字符串为null,即长度为0
-n 字符串不为null,即长度不为0
注意:
使用-z或-n判断字符串变量时,必须要用""把变量引起来。
例如:
if [ -n $string1 ] #string1未被初始化
then
echo "String /"string1/" is not null."
else
echo "String /"string1/" is null"
fi
#结果显示string1为非空,错误
if [ -n "$string1" ] #string1仍然未被初始化
then
echo "String /"string1/" is not null"
else
echo "String /"string1/" is null"
fi
#结果显示string1为空,结果正确
if [ $string1 ] #string1裸体判断
then
echo "String /"string1/" is not null"
else
echo "String /"string1/" is null"
fi
#结果正确
#但这种用法存在漏洞,比如:
string1="1 > 2"
if [ $string1 ]
then
echo "String /"string1/" is not null"
else
echo "String /"string1/" is null"
fi
#实际上[]中的内容被扩展为[ "1 > 2" ],所以结果会出错。
而使用[[ $string1 ]],则可以避免错误
需要转义 [root@monitor ~]# [ "a" \> "b" ]
[root@monitor ~]# echo $? [root@monitor ~]# [ "b" \> "a" ]
[root@monitor ~]# echo $? [root@monitor ~]# [ -a ]
[root@monitor ~]# echo $? [root@monitor ~]# [ -a ]
[root@monitor ~]# echo $? [root@monitor ~]# [ -o ]
[root@monitor ~]# echo $? [root@monitor ~]# [ -o ]
[root@monitor ~]# echo $? [root@monitor ~]# [[ "a" > "b" ]]
[root@monitor ~]# echo $? [root@monitor ~]# [[ "a" < "b" ]]
SHELL 详解的更多相关文章
- Linux的Bash Shell详解
一.Bash Shell概述 1.什么是bash bash是Bourne Again Shell的简称,是从unix系统中的sh发展而来的,是用户和Linux内核交互的工具,用户通过b ...
- hadoop shell 详解
概述 所有的hadoop命令均由bin/hadoop脚本引发.不指定参数运行hadoop脚本会打印所有命令的描述. 用法: hadoop [--config confdir] [COMMAND] ...
- 每篇半小时1天入门MongoDB——3.MongoDB可视化及shell详解
本篇主要介绍MongoDB可视化操作以及shell使用及命令,备份恢复.数据导入导出. MongoVUE安装和简单使用 使用mongo.exe 管理数据库虽然可行,功能也挺强大,但每次都要敲命令,即繁 ...
- Impala shell详解
不多说,直接上干货! 查看帮助文档 impala-shell -h 刷新整个云数据 impala-shell -ruse impala;show tables; 去格式化,查询大数据量时可以提高性能 ...
- Linux之shell详解
Shell是什么 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言.Shell 是指一种应用程序,这个应用程序提供了一个界 ...
- Shell—详解$0、$1、$2、$#、$*、$@、$?、$$变量
预定义变量:常用来获取命令行的输入 变量 作用 $0 当前Shell脚本本身的文件名称 $1 脚本接收的第一个参数($1-$9:第1-9个命令行参数名) $2 脚本接收的第二个参数($1-$9:第1- ...
- Shell [[]]详解:检测某个条件是否成立
[[ ]]是 Shell 内置关键字,它和 test 命令类似,也用来检测某个条件是否成立. test 能做到的,[[ ]] 也能做到,而且 [[ ]] 做的更好:test 做不到的,[[ ]] 还能 ...
- Shell—详解$( )、$(( ))、``与${ }的区别
https://www.jianshu.com/p/2237f029c385 https://www.cnblogs.com/chenpython123/p/11052276.html https:/ ...
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
随机推荐
- Delphi word 颜色
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, ...
- 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(1)
我们要做的第一件事是安装ElasticSearch.对于多数应用程序,您开始安装和配置,通常忘记这些步骤的重要性,直到发生了糟糕的事情.这章我们将广泛关注ElasticSearch的这部分.请注意本章 ...
- [BILL WEI] A potentially dangerous Request.Path value was detected from the client 异常处理办法
我们在ASP.net中使用URL导向后, 我们在访问某个地址,或者打开某个系统页面的时候,就会报错误: A potentially dangerous Request.Path value was d ...
- [selenium webdriver Java]元素定位——findElement/findElements
策略 语法 语法 描述 By id driver.findElement(By.id()) driver.findElements(By.id()) 通过id属性定位元素 By name driver ...
- Cadence原理图与Allegro交互
1:激活orCAD与Allegro的交互程序 打开原理图,Options->Preference在Miscellaneous里勾选 2:打开用到的工程 原理图,还有Allegro PCB Des ...
- IOS-day03_OC中的get和set
OC中的get和set实质和C#/java中的一样 只是表现形式不同而已 如下: @interface Car : NSObject { int wheels; } -(void) run; -(vo ...
- jquery阻止冒泡事件:$('span').bind("click",function(event){event.stopPropagation();})(有用源)
冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件. <body> <div id="content"> 外层div元素 <span> ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- 给 TTreeView 添加复选框
//1.引用单元 uses Commctrl ; //2.定义私有过程 procedure tvToggleCheckbox(TreeView: TTreeView;Node: TTreeNode;i ...
- poj 2349 Arctic Network
http://poj.org/problem?id=2349 Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submis ...