21、exec相关用法:exec命令能够在不创建新的子进程的前提下,转去执行指定的命令,当指定的命令执行完毕后,该进程
就终止了。
root@anLA7856:/home/anla7856/shell# exec date
2017年 08月 24日 星期四 21:39:06 CST
anla7856@anLA7856:/var/log$ #变为用户模式了。
当使用exec打开文件后,read命令每次都会将文件指针移动到下一行进行读取,直到文件末尾,从而可以处理文件内容。
root@anLA7856:/home/anla7856/shell# cat exec.sh
# !/bin/sh
exec <./tmp/tmp1.log
while read line
do
echo $line
done
echo ok
root@anLA7856:/home/anla7856/shell# sh exec.sh
1
2
3
4
5
ok
root@anLA7856:/home/anla7856/shell#
22、shift语句会将所有参数一次左移动一个位置,即$2变为$1,$3变为$2依次类推,原来的$1消失。并且$#减1
root@anLA7856:/home/anla7856/shell# cat n.sh
# !/bin/sh
echo $1 $2
if [ $# -eq 2 ]
then
shift
echo $1
fi
root@anLA7856:/home/anla7856/shell# sh n.sh 1 2
1 2
2
root@anLA7856:/home/anla7856/shell#
23、计算字符串的长度:
root@anLA7856:/home/anla7856/shell# ANLA7856="I am anla7856"
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856}
I am anla7856
root@anLA7856:/home/anla7856/shell# echo ${#ANLA7856} #最快
13
root@anLA7856:/home/anla7856/shell# echo $ANLA7856|wc -L #第二快
13
root@anLA7856:/home/anla7856/shell# expr length "ANLA7856"
8
root@anLA7856:/home/anla7856/shell# expr length $ANLA7856
expr: 语法错误
root@anLA7856:/home/anla7856/shell# expr length "$ANLA7856" #第三快
13
root@anLA7856:/home/anla7856/shell# echo "$ANLA7856"|awk '{print length($0)}' #最慢
13
可以用下面方式来计算时间,也就是定义一个字符串并且执行10000词所需要的时间。
root@anLA7856:/home/anla7856/shell# time for n in {1..10000};do char=`seq -s "anla7856" 100`;echo ${#char} &>/dev/null;done
real 0m8.464s
user 0m0.988s
sys 0m1.156s
root@anLA7856:/home/anla7856/shell#
24、截取字符串长度:
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856}
I am anla7856
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856:2} #从第二个字符开始截取到最后
am anla7856
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856:2:2} #从第二个字符开始截取2个字符
am
25、删除匹配字符串相关操作
root@anLA7856:/home/anla7856/shell# ANLA7856=abcABC123ABCabc
root@anLA7856:/home/anla7856/shell# echo $ANLA7856
abcABC123ABCabc
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856#a*C} #从开头删除最短匹配a*C的过程
123ABCabc
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856#a*c}
ABC123ABCabc
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856##a*c} #从开头开始删除最长匹配a*c的过程
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856##a*C}
abc
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856%a*C} #从结尾删除最短匹配a*C的过程
abcABC123ABCabc
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856%a*c}
abcABC123ABC
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856%%a*C} #从结尾删除最长匹配a*C的过程
abcABC123ABCabc
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856%%a*c}
root@anLA7856:/home/anla7856/shell#
#表示从开头删除匹配最短
##表示从开头删除匹配最长
%表示从结尾删除匹配最短
%%表示从结尾删除匹配最长
26、替换字符串操作
root@anLA7856:/home/anla7856/shell# ANLA7856="I am a handsome boy,yes handsome"
root@anLA7856:/home/anla7856/shell# echo $ANLA7856
I am a handsome boy,yes handsome
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856/handsome/beautiful} #替换第一个字符串
I am a beautiful boy,yes handsome
root@anLA7856:/home/anla7856/shell# echo ${ANLA7856//handsome/beautiful} #替换所有的字符串
I am a beautiful boy,yes beautiful
root@anLA7856:/home/anla7856/shell#
/表示替换匹配的第一个字符串
//表示替换匹配的所有字符串
27、批量替换某一些文件名:
root@anLA7856:/home/anla7856/shell/tmp# ls
pic1user.jpg pic2user.jpg picuser.jpg tmp1.log
root@anLA7856:/home/anla7856/shell/tmp# for f in `ls *.jpg`;do mv $f `echo ${f//user/anla7856}`;done
root@anLA7856:/home/anla7856/shell/tmp# ls
pic1anla7856.jpg pic2anla7856.jpg picanla7856.jpg tmp1.log
root@anLA7856:/home/anla7856/shell/tmp#
28、${parameter:-word}使用示例:
root@anLA7856:/home/anla7856/shell# unset ANLA7856
root@anLA7856:/home/anla7856/shell# echo $ANLA7856
root@anLA7856:/home/anla7856/shell# result=${ANLA7856:-aaaaaa} #ANLA7856无值,则表达式值为后者(aaaaaa)
root@anLA7856:/home/anla7856/shell# echo $result
aaaaaa
root@anLA7856:/home/anla7856/shell# ANLA7856=bbbb
root@anLA7856:/home/anla7856/shell# result=${ANLA7856:-aaaaaa} #ANLA7856有值,则表达式值为ANLA7856的值(bbbb)
root@anLA7856:/home/anla7856/shell# echo $result
bbbb
这个用法可以检测某个变量是否非空。即是否定义
${parameter:=word}:无论parameter是否有值,都将parameter设为word值,并把word值作为整个表达式值返回。
29、${parameter:?word}使用示例:
root@anLA7856:/home/anla7856/shell# unset ANLA7856
root@anLA7856:/home/anla7856/shell# result=${ANLA7856:?aaaaa} #ANLA7856没值,所以一定会输出信息,并且aaa的提示
bash: ANLA7856: aaaaa
root@anLA7856:/home/anla7856/shell# echo $result
bbbb
root@anLA7856:/home/anla7856/shell# ANLA7856=66666
root@anLA7856:/home/anla7856/shell# result=${ANLA7856:?aaaaa} #ANLA7856有值,就会把值付给result,并不会输出
root@anLA7856:/home/anla7856/shell# echo $ANLA7856
66666
root@anLA7856:/home/anla7856/shell# echo $result
66666
root@anLA7856:/home/anla7856/shell#
${parameter:+word}:如果parameter变量为空或未复制,则什么也不做,否则word字符串将代替变量的值。
30、删除7天前的过期数据备份:
root@anLA7856:/home/anla7856/shell# find ${path-/tmp} -name "*.tar.gz" -type f -mtime +7 |xargs rm -f
这样执行时,就算忘记设置path的值,也会用/tmp来替代,也就是删除/tmp下面7天的东西。
31、涉及的数字必须是整形不能是小数
(())双小括号运算符基本用法:
root@anLA7856:/home/anla7856# echo $((1+1))
2
root@anLA7856:/home/anla7856# echo ((1+1))
bash: 未预期的符号 `(' 附近有语法错误
root@anLA7856:/home/anla7856# ((i=5))
root@anLA7856:/home/anla7856# ((i=i*2))
root@anLA7856:/home/anla7856# echo $i
10
root@anLA7856:/home/anla7856# ((a=1+2**3-4%3)) #**是取幂运算
root@anLA7856:/home/anla7856# echo $a
8
(())进行比较及判断
root@anLA7856:/home/anla7856# echo ((3<8))
bash: 未预期的符号 `(' 附近有语法错误
root@anLA7856:/home/anla7856# echo $((3<8))
1
root@anLA7856:/home/anla7856# if ((8>7&&5==5))
> then
> echo yes
> fi
yes
(())注意a++和a+1
root@anLA7856:/home/anla7856# echo $a
8
root@anLA7856:/home/anla7856# echo $((a+1))
9
root@anLA7856:/home/anla7856# echo $((a++))
8
(())表达式在命令行执行时不需要加$符号,直接使用((6%2))形式即可,但是如果要输出,就必须加上$符号,例如echo $((6%2))
32、let相关用法,let i=i+8等同于((i=i+8)),但后者效率更高
root@anLA7856:/home/anla7856/shell# i=2
root@anLA7856:/home/anla7856/shell# let i=i+8
root@anLA7856:/home/anla7856/shell# echo $i
10
33、expr表达式用法:
root@anLA7856:/home/anla7856/shell# expr 2+2
2+2
root@anLA7856:/home/anla7856/shell# expr 2 + 2
4
root@anLA7856:/home/anla7856/shell# expr 2 - 2
0
root@anLA7856:/home/anla7856/shell# expr 2 * 2
expr: 语法错误
root@anLA7856:/home/anla7856/shell# expr 2 \* 2
4
root@anLA7856:/home/anla7856/shell# expr 2 / 2
1
使用expr时,
运算符及计算数字左右两边都至少有一个空格
使用乘号时,需要烦写刚转意,因为shell可能误解为星号。
使用expr判断一个变量是否为整数:
root@anLA7856:/home/anla7856/shell# expr $i + 6 &>/dev/null
root@anLA7856:/home/anla7856/shell# echo $?
0
root@anLA7856:/home/anla7856/shell# expr $i + 6 & >/dev/null #注意&后面不能有空格,将会判断失败,&>/dev/null,表示不保留任何输出
[1] 12994
root@anLA7856:/home/anla7856/shell# 11
^C
[1]+ 已完成 expr $i + 6
root@anLA7856:/home/anla7856/shell# echo $?
130
使用expr判断一个文件名是否符合要求:
if expr "$1" : ".*\.pub" &>/dev/null
then
echo "you are using $1"
else
echo "pls use *.pub file"
fi
通过expr计算字符串长度:
root@anLA7856:/home/anla7856/shell# char="Hello shell"
root@anLA7856:/home/anla7856/shell# expr length $char
expr: 语法错误
root@anLA7856:/home/anla7856/shell# expr length "$char"
11
34、bc的相关用法:
root@anLA7856:/home/anla7856/shell# echo 3+5|bc
8
root@anLA7856:/home/anla7856/shell# echo 3.3+5.3|bc
8.6
root@anLA7856:/home/anla7856/shell# echo 3.3-5.3|bc
-2.0
root@anLA7856:/home/anla7856/shell# echo "scale=2;3.3-5.3"|bc
-2.0
root@anLA7856:/home/anla7856/shell# echo "scale=2;355/113"|bc #scale表示保留小数点后几位
3.14
root@anLA7856:/home/anla7856/shell# echo "scale=5;355/113"|bc
3.14159
root@anLA7856:/home/anla7856/shell# unset i
root@anLA7856:/home/anla7856/shell# i=5
root@anLA7856:/home/anla7856/shell# i=`echo $i+6|bc`
root@anLA7856:/home/anla7856/shell# echo $i
11
35、计算累加1~10的几种方法:
root@anLA7856:/home/anla7856/shell# seq -s "+" 10
1+2+3+4+5+6+7+8+9+10
root@anLA7856:/home/anla7856/shell# echo {1..10}|tr " " "+"
1+2+3+4+5+6+7+8+9+10
root@anLA7856:/home/anla7856/shell# echo `seq -s '+' 10`=`seq -s "+" 10|bc`
1+2+3+4+5+6+7+8+9+10=55
root@anLA7856:/home/anla7856/shell# echo "`seq -s '+' 10`="$((`seq -s "+" 10`))
1+2+3+4+5+6+7+8+9+10=55
root@anLA7856:/home/anla7856/shell# echo `seq -s '+' 10`=`seq -s " + " 10|xargs expr`
1+2+3+4+5+6+7+8+9+10=55
root@anLA7856:/home/anla7856/shell# echo `seq -s "+" 10`=$(echo $[`seq -s "+" 10`])
1+2+3+4+5+6+7+8+9+10=55
36、awk实现计算功能:
root@anLA7856:/home/anla7856/shell# echo "7.7 3.8" | awk '{print ($1-$2)}'
3.9
root@anLA7856:/home/anla7856/shell# echo "358 113" | awk '{print ($1-$2)/$2}'
2.16814
root@anLA7856:/home/anla7856/shell# echo "358 113" | awk '{print ($1-$2)*$2}'
27685
37、$[]运算示例:
root@anLA7856:/home/anla7856/shell# i=5
root@anLA7856:/home/anla7856/shell# i=$[i+6]
root@anLA7856:/home/anla7856/shell# echo $i
11
root@anLA7856:/home/anla7856/shell# echo $[2*3]
6
root@anLA7856:/home/anla7856/shell# echo $[2**3]
8
root@anLA7856:/home/anla7856/shell# echo $[3/5]
0
38、read命令基础,常用两个参数
-p prompt:设置提示信息
-t timeout:设置输入等待时间,单位默认为秒
root@anLA7856:/home/anla7856/shell# read -p "Please input two number:" a1 a2
Please input two number:1 2
root@anLA7856:/home/anla7856/shell# echo $a1
1
root@anLA7856:/home/anla7856/shell# echo $a2
2
39、条件测试test:
test的基本用法:
root@anLA7856:/home/anla7856# test -f file && echo true || echo false #可用于判断某一个文件是否存在
false
root@anLA7856:/home/anla7856# test -f /tmp/anla7856.txt && echo 1
root@anLA7856:/home/anla7856# test -f /tmp/anla7856.txt || echo 1
1
root@anLA7856:/home/anla7856# test -z "anla7856" && echo 1 ||echo 0
0
test中使用[]来替代test:
在这里面一般能用-a、-o、-gt、>、<等
root@anLA7856:/home/anla7856# [ -f /tmp/anla7856.txt ]&& echo 1 || echo 0
0
root@anLA7856:/home/anla7856# [ -f /tmp/anla7856.txt ] || echo 0
0
test中使用[[]]来替代test:
在这里面可以用&&、||、>、<等
root@anLA7856:/home/anla7856# [[ 1>3 ]] && echo 1 || echo 0
bash: 条件命令中有未预期的符号 284
bash: `1>' 附近有语法错误
root@anLA7856:/home/anla7856# [[ 1 > 3 ]] && echo 1 || echo 0
0
root@anLA7856:/home/anla7856# [ 1 > 3 ] && echo 1 || echo 0
1
40、常用的文件测试操作符(一般和上一点的test联合使用):
-d文件(directory) :文件存在且为目录则为真,即测试表达式成立
-f文件(file) :文件存在且为普通文件则为真
-e文件(exist) :文件存在则为真,注意与-f区别
-r文件(read) :文件存在且可读为真
-s文件(size) :文件存在且大小不为0为真
-w文件(write) :文件存在且可写为真
-x文件(executable) :文件存在且可执行为真
-L文件(link) :文件存在且为链接文件则为真
f1 -nt f2(newer than):文件f1比文件f2新则为真,根据修改时间来
f1 -ot f2(older than):文件f1比文件f2旧则为真,根据修改时间来
root@anLA7856:/home/anla7856/shell# [ -f "/etc/services" ] && echo 1 || echo 1
1
root@anLA7856:/home/anla7856/shell# [ -f /etc/profile ] && echo 1 || echo 0
1
root@anLA7856:/home/anla7856/shell# [ -f /etc/profile ] && `source /etc/profile`