1.shell介绍
通常,shell脚本以.sh结尾作为标识。脚本的开头都会写#!/bin/bash这一串表示使用bash解释器来解释这个脚本内容。一般执行脚本可以通过./xxx.sh或者sh xxx.sh的方式来执行shell脚本,前者需要给shell脚本添加执行权限,后者不需要。可以通过添加参数x来进行调试脚本,会显示脚本执行的每一条命令。以下面为例:
[hadoop@hadoop001 shell]$ sh first.sh
ruozedata
[hadoop@hadoop001 shell]$ sh -x first.sh
+ echo ruozedata
ruozedata
[hadoop@hadoop001 shell]$ chmod u+x first.sh
[hadoop@hadoop001 shell]$ ./first.sh
ruozedata
[hadoop@hadoop001 shell]$ cat first.sh
#!/bin/bash
echo "ruozedata"
[hadoop@hadoop001 shell]$
2.shell脚本使用
2.1 变量的创建与引用
变量使用“变量名=值”的方式来创建,而且=前后不允许有空格。变量名最好使用大写形式,变量的值可以用" "或者’ '来使用,用``引用的值,可以用来做命令替换。变量的使用,最好使用{}放进去。
[hadoop@hadoop001 shell]$ sh variable.sh
ruozedata
date
Thu Aug 22 20:46:31 CST 2019
[hadoop@hadoop001 shell]$ cat variable.sh
#!/bin/bash
RZ="ruozedata"
DATE01='date'
DATE02=`date`
echo $RZ
echo ${DATE01}
echo $DATE02
[hadoop@hadoop001 shell]$
2.2 传递参数
通常脚本可以通过参数的传递来实现,$0表示脚本的名称;$1表示脚本的第一个参数;$2, $ 3 依 次 类 推 , 3依次类推, 3依次类推,$#表示传递参数的个数; $ ∗ 表 示 显 示 所 有 参 数 ; *表示显示所有参数; ∗表示显示所有参数; $$表示当前脚本运行的pid号。
[hadoop@hadoop001 shell]$ sh parameter.sh ruoze jepson
ruoze
jepson
2
ruoze jepson
PID:6809
[hadoop@hadoop001 shell]$ cat parameter.sh
#!/bin/bash
echo $1
echo $2
echo $#
echo $*
echo "PID:$$"
[hadoop@hadoop001 shell]$
2.3 数组
shell 数组用括号来表示,元素用"空格"符号分割开,数组可以使用[1]来显示数组中元素下标位1的元素,使用[@]来显示所有元素,使用#“数组变量”[@]来显示数组中元素个数
[hadoop@hadoop001 shell]$ sh array.sh
a b c d
d
4
[hadoop@hadoop001 shell]$ cat array.sh
#!/bin/bash
arr=(a b c d)
echo ${arr[@]}
echo ${arr[3]}
echo ${#arr[@]}
2.4 if判断
if判断注意点:1 [ ] 前后空格 2 == 前后空格
[hadoop@hadoop001 shell]$ sh if.sh
ruoze='ruoze'
[hadoop@hadoop001 shell]$ cat if.sh
#!/bin/bash
A="ruoze"
B="jepson"
if [ "${A}" == "${B}" ];then
echo "${A}==${B}"
elif [ "${A}" == "ruoze" ];then
echo "${A}='ruoze'"
else
echo "!="
fi
[hadoop@hadoop001 shell]$
2.5 for循环跟while循环
[hadoop@hadoop001 shell]$ sh forwhile.sh
1
2
3
4
####################
1
2
3
4
5
6
####################
3
4
5
6
[hadoop@hadoop001 shell]$ cat forwhile.sh
#!/bin/bash
for j in 1 2 3 4
do
echo $j
done
echo "####################"
for ((j=1;j<=6;j++))
do
echo $j
done
echo "####################"
j=3
while ((j<7))
do
echo $j
let j++
done
2.6 分隔符
[hadoop@hadoop001 shell]$ sh split.sh
a
b
c
d
e
f
[hadoop@hadoop001 shell]$ cat split.sh
#!/bin/bash
s="a,b,c,d,e,f"
OLD_IFS="$IFS"
IFS=","
arr=($s)
IFS="$OLD_IFS"
for i in ${arr[@]}
do
echo $i
done
[hadoop@hadoop001 shell]$
2.7 awk
awk [-F 分隔符] ‘awk程序段’ 文件
[hadoop@hadoop001 shell]$ cat test.log
a b c
1 2 3
4 5 6
[hadoop@hadoop001 shell]$ awk '{print $1}' test.log 打印文件第一列内容
a
1
4
[hadoop@hadoop001 shell]$ awk '{print $1,$2}' test.log 打印文件第一列跟第二列内容
a b
1 2
4 5
[hadoop@hadoop001 shell]$ awk '{print $1$2}' test.log 打印文件第一列跟第二列内容
ab
12
45
[hadoop@hadoop001 shell]$ awk 'NR==1{print}' test.log 打印文件第一行
a b c
[hadoop@hadoop001 shell]$ awk 'NR==1{print $1}' test.log 打印文件第一行第一列
a
[hadoop@hadoop001 shell]$ awk 'NR>1{print}' test.log 打印文件大于第一行的内容
1 2 3
4 5 6
[hadoop@hadoop001 shell]$ cat test.log
a,b,c
1,2,3
4,5,6
[hadoop@hadoop001 shell]$ awk -F "," '{print $1}' test.log 以F指定分隔符,打印文件第一列
a
1
4
2.8 sed
sed 选项 ‘编辑指令’ 文件路径
-e:它告诉sed将下一个参数解释为一个sed指令,只有当命令行上给出多个sed指令时才需要使用-e选项
i:插入 向匹配行前插入内容
d:删除 删除匹配的内容
s:替换 替换掉匹配的内容
p:打印 打印出匹配的内容,通常与-n选项和用
n:读取下一行,遇到n时会自动跳入下一行
[hadoop@hadoop001 shell]$ sed -n "s/a/aa/p" test.log sed使用-n跟-p参数的时候,不会改变文件内容
aa,b,c
[hadoop@hadoop001 shell]$ cat test.log
a,b,c
1,2,3
4,5,6
[hadoop@hadoop001 shell]$ sed -i "s/a/b/" test.log sed使用-i的时候,会改变文件内容,不输出信息
[hadoop@hadoop001 shell]$ cat test.log
b,b,c
1,2,3
4,5,6
[hadoop@hadoop001 shell]$ sed -i "s/b/b1/" test.log
[hadoop@hadoop001 shell]$ cat test.log
b1,b,c
1,2,3
4,5,6
[hadoop@hadoop001 shell]$ sed -i "s/b/b1/" test.log
[hadoop@hadoop001 shell]$ cat test.log
b11,b,c
1,2,3
4,5,6
[hadoop@hadoop001 shell]$ sed -i "s/b/b1/g" test.log sed添加g参数时,才会全局替换,不然只替换文件中每行第一个参数
[hadoop@hadoop001 shell]$ cat test.log
b111,b1,c
1,2,3
4,5,6
[hadoop@hadoop001 shell]$