2>&- #只是将错误内容不显示,正确内容显示结果
#&[n] 代表是已经存在的文件描述符,&1 代表输出 &2代表错误输出 &-代表关闭与它绑定的描述符
#/dev/null 这个设备,是linux 中黑洞设备,什么信息只要输出给这个设备,都会给吃掉
[root@localhost home]# ls a.txt aa bb cc 2>&-
a.txt
#关闭所有输出
[chengmo@centos5 shell]$ ls test.sh test1.sh 1>&- 2>&-
#关闭 1 ,2 文件描述符
[chengmo@centos5 shell]$ ls test.sh test1.sh 2>/dev/null 1>/dev/null
#将1,2 输出转发给/dev/null设备
#$(())里面做三元运算
#!/bin/sh
a=1;
b="ab";
echo $((a>1?8:9));
# [[]] 表达式
[root@localhost ~]# [ 1 -eq 1 ] && echo 'ok'
ok
[root@localhost ~]$ [[ 2 < 3 ]] && echo 'ok'
ok
[root@localhost ~]$ [[ 2 < 3 && 4 > 5 ]] && echo 'ok'
ok
注意:[[]] 运算符只是[]运算符的扩充。能够支持<,>符号运算不需要转义符,它还是以字符串比较大小。里面支持逻辑运算符:|| &&
##
seq [OPTION]... LAST
seq [OPTION]... FIRST LAST
seq [OPTION]... FIRST INCREMENT LAST
seq 1000 ‘起始默认是 1,间隔默认也是1
seq 2 1000 ‘间隔默认是1
seq 1 3 10 '从1开始,到10 间隔为3 结果是:1 4 7 10
说明:默认间隔是“空格” 如果想换成其它的可以带参数:-s
seq -s'#' 1 3 10
1#4#7#10
#输出数字1-100
[root@localhost ~]# echo {1..100}
#数组
用${#数组名[@或*]} 可以得到数组长度
用${数组名[下标]} 下标是从0开始 下标是:*或者@ 得到整个数组内容
直接通过 数组名[下标] 就可以对其进行引用赋值,如果下标不存在,自动添加新一个数组元素
直接通过:unset 数组[下标] 可以清除相应的元素,不带下标,清除整个数据。
直接通过 ${数组名[@或*]:起始位置:长度} 切片原先数组,返回是字符串,中间用“空格”分开,因此如果加上”()”,将得到切片数组,
上面例子:c 就是一个新数据
[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ unset a
[chengmo@centos5 ~]$ echo ${a[*]}
[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ unset a[1]
[chengmo@centos5 ~]$ echo ${a[*]}
1 3 4 5
[chengmo@centos5 ~]$ echo ${#a[*]}
4
##特殊使用
1. 分片:${数组名[@或*]:起始位置:长度}
[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ echo ${a[@]:0:3}
1 2 3
[chengmo@centos5 ~]$ echo ${a[@]:1:4}
2 3 4 5
[chengmo@centos5 ~]$ c=(${a[@]:1:4})
[chengmo@centos5 ~]$ echo ${#c[@]}
4
[chengmo@centos5 ~]$ echo ${c[*]}
2 3 4 5
2. 替换:调用方法是:${数组名[@或*] /查找字符/替换字符 } 该操作不会改变原先数组内容,如果需要修改,可以看上面例子,重新定义数据
[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ echo ${a[@]/3/100}
1 2 100 4 5
[chengmo@centos5 ~]$ echo ${a[@]}
1 2 3 4 5
[chengmo@centos5 ~]$ a=(${a[@]/3/100})
[chengmo@centos5 ~]$ echo ${a[@]}
1 2 100 4 5
### shell case语法
case $arg in
pattern | sample) # arg in pattern or sample
;;
pattern1) # arg in pattern1
;;
*) #default
;;
esac
说明:pattern1 是正则表达式,可以用下面字符:
* 任意字串
? 任意字元
[abc] a, b, 或c三字元其中之一
[a-n] 从a到n的任一字元
| 多重选择
###############################################
一、判断读取字符串值
表达式 含义
${var} 变量var的值, 与$var相同
${var-DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 *
${var:-DEFAULT} 如果var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 *
${var=DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 *
${var:=DEFAULT} 如果var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 *
${var+OTHER} 如果var声明了, 那么其值就是$OTHER, 否则就为null字符串
${var:+OTHER} 如果var被设置了, 那么其值就是$OTHER, 否则就为null字符串
${var?ERR_MSG} 如果var没被声明, 那么就打印$ERR_MSG *
${var:?ERR_MSG} 如果var没被设置, 那么就打印$ERR_MSG *
${!varprefix*} 匹配之前所有以varprefix开头进行声明的变量
${!varprefix@} 匹配之前所有以varprefix开头进行声明的变量
加入了“*” 不是意思是: 当然, 如果变量var已经被设置的话, 那么其值就是$var.
-------------------------------------------------------
[chengmo@localhost ~]$ echo ${abc-'ok'}
ok
[chengmo@localhost ~]$ echo $abc
[chengmo@localhost ~]$ echo ${abc='ok'}
ok
[chengmo@localhost ~]$ echo $abc
ok
如果abc 没有声明“=" 还会给abc赋值。
[chengmo@localhost ~]$ var1=11;var2=12;var3=
[chengmo@localhost ~]$ echo ${!v@}
var1 var2 var3
[chengmo@localhost ~]$ echo ${!v*}
var1 var2 var3
${!varprefix*}与${!varprefix@}相似,可以通过变量名前缀字符,搜索已经定义的变量,无论是否为空值。
二、字符串操作(长度,读取,替换)
表达式 含义
${#string} $string的长度
${string:position} 在$string中, 从位置$position开始提取子串
${string:position:length} 在$string中, 从位置$position开始提取长度为$length的子串
${string#substring} 从变量$string的开头, 删除最短匹配$substring的子串
${string##substring} 从变量$string的开头, 删除最长匹配$substring的子串
${string%substring} 从变量$string的结尾, 删除最短匹配$substring的子串
${string%%substring} 从变量$string的结尾, 删除最长匹配$substring的子串
${string/substring/replacement} 使用$replacement, 来代替第一个匹配的$substring
${string//substring/replacement} 使用$replacement, 代替所有匹配的$substring
${string/#substring/replacement} 如果$string的前缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
${string/%substring/replacement} 如果$string的后缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
说明:"* $substring”可以是一个正则表达式.
1.长度
[web97@salewell97 ~]$ test='I love china'
[web97@salewell97 ~]$ echo ${#test}
12
${#变量名}得到字符串长度
2.截取字串
[chengmo@localhost ~]$ test='I love china'
[chengmo@localhost ~]$ echo ${test:5}
e china
[chengmo@localhost ~]$ echo ${test:5:10}
e china
${变量名:起始:长度}得到子字符串
3.字符串删除
[chengmo@localhost ~]$ test='c:/windows/boot.ini'
[chengmo@localhost ~]$ echo ${test#/}
c:/windows/boot.ini
[chengmo@localhost ~]$ echo ${test#*/}
windows/boot.ini
[chengmo@localhost ~]$ echo ${test##*/}
boot.ini
[chengmo@localhost ~]$ echo ${test%/*}
c:/windows
[chengmo@localhost ~]$ echo ${test%%/*}
${变量名#substring正则表达式}从字符串开头开始配备substring,删除匹配上的表达式。
${变量名%substring正则表达式}从字符串结尾开始配备substring,删除匹配上的表达式。
注意:${test##*/},${test%/*} 分别是得到文件名,或者目录地址最简单方法。
4.字符串替换
[chengmo@localhost ~]$ test='c:/windows/boot.ini'
[chengmo@localhost ~]$ echo ${test/\//\\}
c:\windows/boot.ini
[chengmo@localhost ~]$ echo ${test//\//\\}
c:\windows\boot.ini
${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:”/”请加转义符”\/”表示。
三、性能比较
在shell中,通过awk,sed,expr 等都可以实现,字符串上述操作。下面我们进行性能比较。
[chengmo@localhost ~]$ test='c:/windows/boot.ini'
[chengmo@localhost ~]$ time for i in $(seq 10000);do a=${#test};done;
real 0m0.173s
user 0m0.139s
sys 0m0.004s
[chengmo@localhost ~]$ time for i in $(seq 10000);do a=$(expr length $test);done;
real 0m9.734s
user 0m1.628s
速度相差上百倍,调用外部命令处理,与内置操作符性能相差非常大。在shell编程中,尽量用内置操作符或者函数完成。使用awk,sed类似会出现这样结果。