1.
1)整数计算:
(1)
$(()) :用于数学计算
echo $((3+3))
(2)
expr :运算符两边必须有空格,若被$[]包含,则不必考虑空格问题
expr 2 + 2
2)浮点数计算:
bc用于计算浮点数
bc -q
echo `"3.44*5"|bc`
2.定义数组
x=(1 2 4 100 5)
x[3] = 20
declare -A 数组名
(1)输出全部echo ${x[*]}
(2)输出数组个数:
${#x[*]}
${#x[@]}
(3)数组元素替换:
echo ${x[@]/100/3}
3.
((变量名=变量名+常量/变量))
((变量名**n)) : 变量的n次方
unset 变量名 :删除变量
unset x[0]
4.生成随机数 ,用$$作占位符
Random = $$
echo $Random
5.时间性能比较
time echo {1,100}
time echo seq 100
time seq 100
6.
管道两边不能有空格
若引号内多条语句,用分号分开
7. awk
[root@localhost ~]# awk '{print $0}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@localhost ~]# awk '{print ""}' /etc/passwd
[root@localhost ~]# awk '{print "hihi"}' /etc/passwd
hihi
hihi
hihi
hihi
[root@localhost ~]# awk -F":" '{print $1 ":" $2}' /etc/passwd
root:x
bin:x
daemon:x
adm:x
8.sed
1)ns :替换
压缩空白:sed 's/^$//g'
2)nd:删除
1>sed '1d' file 删除第一行
sed '2,$d' file 删除第二行到最后一行
2>2.txt
100 200 300
rr aa bb
ciejo2
[root@localhost ~]# sed '/[0-9]/d' 2.txt
rr aa bb
3) -n :显示
sed -n '1p' file 显示第一行
sed -n '2,$p' file 显示第二行到最后一行
sed -n '/100/p' 1.txt 显示包含100的行
[root@localhost ~]# sed -n '/100/p' 1.txt
100 9 19 20
100 89 70 80
[root@localhost ~]# sed -n 's/100//p' 1.txt
9 19 20
89 70 80
4)sed -y:对应着替换
sed -y '/[a-z]/[A-Z]/g'
5)sed -e:跟一个表达式
sed -e expression,-e expression = sed -e expression,expression
6)
'i':前插
‘a’:后插
sed 'na ajjj ' file :第n行增加ajjjj
[root@localhost ~]# sed '1a abc efg' 1.txt
100 9 19 20
abc efg
100 89 70 80
300 546 12 00
7)sed 'nc ssiejie' file :第n行内容 替换为ssiejie
[root@localhost ~]# sed '1c ssie' 1.txt
ssie
100 89 70 80
300 546 12 00
8)匹配子串
sed '/\([0-9]\{\+3\}\)/abc\1/'
[root@localhost ~]# echo seven EGIGHT |sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
EGIGHT seven
[root@localhost ~]# echo 8349aFde|sed 's/[0-9]+\([a-zA-Z]\+\)/\1/'
8349aFde
[root@localhost ~]# echo 8349aFde|sed 's/\([0-9]\+\)+\([a-zA-Z]\+\)/\1/'
8349aFde
[root@localhost ~]# echo 8349aFde|sed 's/\([0-9]\+\)\([a-zA-Z]\+\)/\1/'
8349
9.tee :读取输入的数据,并且输出到文件
1)tee -a file:追加到文件末尾
2)tee file:不加-a:覆盖文件
[root@localhost ~]# who|tee who.txt
root pts/3 2015-02-25 10:37 (113.31.32.123)
root pts/5 2015-02-27 10:19 (113.31.32.123)
[root@localhost ~]# cat who.txt
root pts/3 2015-02-25 10:37 (113.31.32.123)
root pts/5 2015-02-27 10:19 (113.31.32.123)
[root@localhost ~]# pwd |tee -a who.txt
/root
[root@localhost ~]# cat who.txt
root pts/3 2015-02-25 10:37 (113.31.32.123)
root pts/5 2015-02-27 10:19 (113.31.32.123)
/root
3)tee - :输出到屏幕两次
4)tee - - :输出到屏幕三次
输出到屏幕两次 并写入两个文件
[root@localhost ~]# pwd |tee who.txt who2.txt -
/root
/root
[root@localhost ~]# cat who2.txt
/root
[root@localhost ~]# pwd |tee who.txt who2.txt - -
/root
/root
/root
10.xargs
1)xargs -n [num] :一行显示几列
2)xargs -p '':每行输出都问用户“”
[root@localhost ~]# find / -type f| xargs -n 1 -p echo
echo /sbin/cryptsetup ?...y
echo /sbin/badblocks ?.../sbin/cryptsetup
echo /sbin/mcstransd ?...n
echo /sbin/telinit ?...n
echo /sbin/rpcbind ?...^C
3)xargs -I {} cp {} .
[root@localhost ~]# find /etc -name *.txt |xargs -n 1
/etc/sysconfig/rhn/sources.rpmforge.txt
/etc/pki/nssdb/pkcs11.txt
[root@localhost ~]# find /etc -name "*.txt"|xargs -I {} cp {} .
[root@localhost ~]# ls
install.log pkcs11.txt
install.log.syslog sources.rpmforge.txt
11.cut :提取列
1)-f :
2)-b:按字节
3)-c : 按字符
4)cut -b -3,3- file :-3 表示从第一个字节到第三个字节,
:3- 表示从第三个字节到行尾
[root@localhost ~]# cat /etc/passwd |cut -d : -f -2
root:x
bin:x
daemon:x
adm:x
[root@localhost ~]# cat /etc/passwd |cut -d : -b -2
cut: an input delimiter may be specified only when operating on fields
Try `cut --help' for more information.
[root@localhost ~]# cat '/etc/passwd' | cut -c 1
r
b
d
a