1.入门
- 后缀是.sh
- 以#!/bin/bash开头
- 只授予这个文件的所属者执行的权限chmod u+x wc.sh (u 代表用户. g 代表用户组. o 代表其他. a 代表所有)
- 加-x可以debug
[root@10-9-15-140 ~]# vi wc.sh
#!/bin/bash
echo "www.ruozedata.com"
-rw-r--r-- 1 root root 38 Aug 24 23:54 wc.sh
[root@10-9-15-140 ~]# chmod u+x wc.sh
-rwxr--r-- 1 root root 38 Aug 24 23:54 wc.sh
[root@10-9-15-140 ~]# sh wc.sh
www.ruozedata.com
[root@10-9-15-140 ~]# sh -x wc.sh
+ echo www.ruozedata.com
www.ruozedata.com
2.变量定义与引用
k=v
[root@10-9-15-140 ~]# vi variable.sh
#!/bin/bash
rz="ruozedata"
date1="date"
date2=`date`
P='ruozedata'
echo $rz
echo ${date1}
echo ${date2}
echo $PA
echo ${P}A
[root@10-9-15-140 ~]# sh variable.sh
ruozedata
date
Sun Aug 25 00:08:03 CST 2019
ruozedataA
坑:
= 前后不能有空格
变量名称 一般来说 都是大写
使用变量 习惯的使用{}
静态:
k="v" 'v' v
动态:
k=`v`
3.传递参数
[root@10-9-15-140 ~]# vi parameter.sh
#!/bin/bash
echo $1
echo $2
echo "$#" #输出多少个参数
echo "$*" #输出所有参数
echo "PID: $$" #输出当前的pid
[root@10-9-15-140 ~]# sh parameter.sh a b
a
b
2
a b
PID: 21730
[root@10-9-15-140 ~]# sh parameter.sh "a b"
a b
1
a b
PID: 21867
4.数组
[root@10-9-15-140 ~]# vi array.sh
#!/bin/bash -x
arr=(rz jepson xingxing huhu)
echo ${arr[@]} #输出所有数组内的字符串
echo ${arr[3]} #输出所有数组内第三个字符串
echo ${#arr[@]} #输出所有数组的个数
[root@10-9-15-140 ~]# ./array.sh
+ arr=(rz jepson xingxing huhu)
+ echo rz jepson xingxing huhu
rz jepson xingxing huhu
+ echo huhu
huhu
+ echo 4
4
5.if条件语句
a. [ ] 前后空格
b. == 前后空格
[root@10-9-15-140 ~]# vi cat if.sh
#!/bin/bash
a="abc"
b="jepson"
if [ $a == $b ];then
echo "=="
else
echo "!="
fi
[root@10-9-15-140 ~]# ./if.sh
!=
#!/bin/bash
[root@10-9-15-140 ~]# vi cat if1.sh
a="abc"
b="jepson"
if [ "${a}" == "${b}" ];then
echo "=="
elif [ "${a}" == "abc" ];then
echo "=="
else
echo "!="
fi
[root@10-9-15-140 ~]# ./if1.sh
==
6.forwhile循环语句
[root@10-9-15-140 ~]# vi forwhile.sh
#!/bin/bash
for x in 1 2 3 4 5
do
echo $x
done
echo "------------------"
for ((i=1;i<10;i++))
do
echo $i
done
echo "------------------"
j=1
while(($j<10))
do
echo $j
let "j++"
done
[root@10-9-15-140 ~]# ./forwhile.sh
1
2
3
4
5
------------------
1
2
3
4
5
6
7
8
9
------------------
1
2
3
4
5
6
7
8
9
7.分割
[root@10-9-15-140 ~]# vi spilt.sh
#!/bin/bash
s="rz,j,xx,huhu,yt,co"
OLD_IFS="$IFS"
IFS=","
arr=($s)
IFS="$OLD_IFS"
for x in ${arr[*]}
do
echo $x
done
[root@10-9-15-140 ~]# ./spilt.sh
rz
j
xx
huhu
yt
co
8.补充 awk
[root@10-9-15-140 ~]# cat test.log
a b c
1 2 3
[root@10-9-15-140 ~]# cat test.log | awk '{print $1}'
a
1
[root@10-9-15-140 ~]# cat test.log | awk '{print $1,$2}'
a b
1 2
[root@10-9-15-140 ~]# cat test.log | awk '{print $1$2}'
ab
12
[root@10-9-15-140 ~]# cat test.log | awk 'NR==1{print}'
a b c
[root@10-9-15-140 ~]# cat test.log | awk 'NR==1{print $1}'
a
[root@10-9-15-140 ~]# cat test.log | awk 'NR>1{print}'
1 2 3
[root@10-9-15-140 ~]# vi test.log
[root@10-9-15-140 ~]#
[root@10-9-15-140 ~]# cat test.log
a,b,c
1,2,3
[root@10-9-15-140 ~]# cat test.log |awk -F "," '{print $1}'
a
1
[root@10-9-15-140 ~]#
9.补充 sed
[root@10-9-15-140 ~]# cat test.log
a,b,c
1,2,3
[root@10-9-15-140 ~]# sed -i 's/a/aa/' test.log
[root@10-9-15-140 ~]# cat test.log
aa,b,c
1,2,3
[root@10-9-15-140 ~]# sed -i 's/aa/aa'/' test.log
> ^C
[root@10-9-15-140 ~]# sed -i "s/aa/aa'/" test.log
[root@10-9-15-140 ~]#
[root@10-9-15-140 ~]# cat test.log
aa',b,c
1,2,3
[root@10-9-15-140 ~]# sed -i "s/aa'/bbb/" test.log
[root@10-9-15-140 ~]# cat test.log
bbb,b,c
1,2,3
全局替换
[root@10-9-15-140 ~]# sed -i "s/b/j/g" test.log
[root@10-9-15-140 ~]# cat test.log
jjj,j,c
1,2,3
[root@10-9-15-140 ~]#