shell编程常用语法

格式与使用:

shell脚本是以.sh结尾的文件,执行文件的方法有两种:
一、./hello.sh
表示执行当前目录下的hello.sh脚本,执行条件:
1、需要在文件的第一行写

	#!/bin/bash
	#!是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell。

2、赋予执行权限

chmod +x hello.sh

二、作为解释器参数运行
sh hello.sh
/bin/sh hello.sh
这种方式运行脚本不需要写#!/bin/bash ,也不需要赋予执行权限

shell脚本debug

方法:
1、在解释器后面加 -x
2、sh -x hello.sh

[root@hadoop-01 shell]# cat hello.sh 
#!/bin/bash -x
echo "hello"
pwd
ls -l 
[root@hadoop-01 shell]# ./hello.sh 
+ echo hello  
hello
+ pwd
/root/shell
+ ls -l
total 16
-rwxr-xr-x 1 root root 38 Apr 13 16:22 hello.sh
-rwxr-xr-x 1 root root 17 Apr 13 15:45 wc1.sh
-rw-r--r-- 1 root root 13 Apr 13 16:01 wc2.sh
-rwxr-xr-x 1 root root 29 Apr 13 15:42 wc.sh
变量定义和引用

语法格式
变量=值

静态变量
K=V ‘V’ “V” 三种写法,单引,双引,不加引号

动态变量
K=V esc下面的点

注意:变量名和等号之间不能有空格,同时,变量名的命名须遵循如下规则:

  • 首个字符必须为字母( a-z, A-Z)
  • 中间不能有空格,可以使用下划线( _)
  • 不能使用标点符号
  • 不能使用 bash 里的关键字(可用 help 命令查看保留关键字)

变量使用:
$k
${k}
花括号是可选的,加不加都行,加花括号是为了帮助解释器识别变量的边界。

传递参数

在执行 Shell 脚本时, 可以向脚本传递参数。
脚本内获取参数的格式为: $n。 n 代表一个数字, 1 为执行脚本的第一个参
数, 2 为执行脚本的第二个参数,以此类推…… $0 表示当前脚本名称。

$# 	传递到脚本的参数个数
$* 	以一个单字符串显示所有向脚本传递的参数"$1 $2"。
$$ 	脚本运行的当前进程 ID 号
$! 	后台运行的最后一个进程的 ID 号
$@ 	不被" "包含时与$*相同,包含引号时"#@",以"$1" "$2"...形式组成一个参数列表。
$? 	显示最后命令的退出状态。 0 表示没有错误,其他任何值表明有错误。
数组

shell脚本里只支持一位数组
语法:

arr=(zhangsan lisi wangwu)
echo ${arr[@]}    ####显示数组的所有内容
echo ${arr[*]}	####显示数组的所有内容
echo ${arr[2]}  #### 输出下标为2的元素
echo ${#arr[@]} #### 输出数组的长度
if 判断

语法:

if condition1
then
command1
elif condition2
then
command2
else
commandN
fi

注意:
A="abc"
B="jepson"
if [ $a == $b ];then  ### 写条件时,调用变量时,变量前后加空格与[ ]隔开
       echo "=="
else
       echo "!="
fi
循环

for循环

#方式一
for N in 1 2 3
do
echo $N
done
或
for N in 1 2 3; do echo $N; done
或
for N in {1..3}; do echo $N; done
#方式二
for ((i = 0; i <= 5; i++))
do
echo "welcome $i times"
done
或
for ((i = 0; i <= 5; i++)); do echo "welcome $i times"; done

while循环
语法:

while expression
do
command
…
done
#!/bin/bash
i=1
while (( i <= 3))
do 
 let i++   ###let 命令是 BASH 中用于计算的工具,用于执行一个或多个表达式,变量计算中不需要加上 $ 来表示变量。 自加操作: let no++ 自减操作: let no—
 echo $i
done
分割
[root@hadoop001 shell]# vi spilt.sh
#!/bin/bash

S="ruoze,jepson,xingxing,dashu,xiaoshiqi,xiaohai"

OLD_IFS="$IFS" ### 指定格式
IFS="," ### 分隔符
arr=($S) ### 将所要分隔的字符串分割成数组
IFS="OLD_IFS"

for x in ${arr[*]}
do
        echo $x
done

[root@hadoop001 shell]# chmod +x ./spilt.sh 
[root@hadoop001 shell]# 
[root@hadoop001 shell]# 
[root@hadoop001 shell]# ./spilt.sh 
ruoze
jepson
xingxing
dashu
xiaoshiqi
xiaohai
[root@hadoop001 shell]# 
取数awk
[root@hadoop-01 shell]# cat awk.log 
a b c
1 2 3
4 5 6 

取列

[root@hadoop-01 shell]# cat awk.log | awk '{print $1}'
a
1
4
[root@hadoop-01 shell]# cat awk.log | awk '{print $1,$2}' ##参数间用逗号分隔表示先打印第一行,在打印第二行,包含分隔符
a b
1 2
4 5
[root@hadoop-01 shell]# cat awk.log | awk '{print $1 $2}'##参数间用空格隔开表示先打印第一列,再打第二列,不包含分隔符
ab
12
45
[root@hadoop-01 shell]# cat awk.log | awk '{print $0}'
a b c
1 2 3
4 5 6 

取行

[root@hadoop-01 shell]# cat awk.log | awk 'NR==1'
a b c
[root@hadoop-01 shell]# cat awk.log | awk 'NR==2'
1 2 3
[root@hadoop-01 shell]# cat awk.log | awk 'NR==3'
4 5 6 
[root@hadoop-01 shell]# cat awk.log | awk 'NR>1'
1 2 3
4 5 6 

-F 分隔符

[root@hadoop-01 shell]# cat awk2.log 
a,b,c
1,2,3
4,5,6
[root@hadoop-01 shell]# cat awk2.log | awk -F "," 'NR==1'
a,b,c
[root@hadoop001 shell]# cat awk.log |awk -F "," '{ print $3 }'
c
3
6
[root@hadoop001 shell]# cat awk.log |awk -F "," 'NR>1{ print $3 }'
3
6
[root@hadoop-01 shell]# awk -F "," '{print $1}' awk2.log 
a
1
4
[root@hadoop001 shell]#

输出文件:

 top -bn1 | awk 'NR==1{print > "kk.log"}' 
替换sed
[root@hadoop-01 shell]# cat sed.log 
a b c
1 2 3
[root@hadoop001 shell]# sed -i 's/a/aa/' sed.log 
[root@hadoop001 shell]# cat sed.log 
aa b c
1 2 3
[root@hadoop001 shell]# sed -i 's/aa/aa'/' sed.log 
> ^C
[root@hadoop001 shell]# sed -i "s/aa/aa'/" sed.log 
[root@hadoop001 shell]# 
[root@hadoop001 shell]# 
[root@hadoop001 shell]# cat sed.log 
aa' b c
1 2 3
[root@hadoop001 shell]# sed -i "s?aa'?bbb?" sed.log 
[root@hadoop001 shell]# cat sed.log 
bbb b c
1 2 3
[root@hadoop001 shell]# sed -i "s/b/w/" sed.log
[root@hadoop001 shell]# cat sed.log 
wbb b c
1 2 3
[root@hadoop001 shell]# vi sed.log
bbb b c
1 2 3

全局替换
[root@hadoop001 shell]# sed -i "s/b/w/g" sed.log ## 不加g表示替换每行的第一个b
[root@hadoop001 shell]# cat sed.log 
www w c
1 2 3


行首加
[root@hadoop001 shell]# sed -i "s/^/uuu&/g" sed.log ### ^ 表示行首
[root@hadoop001 shell]# cat sed.log 
uuuwww w c
uuu1 2 3

行尾加
[root@hadoop001 shell]# sed -i "s/$/&uuu/g" sed.log ### $表示行尾
[root@hadoop001 shell]# 
[root@hadoop001 shell]# cat sed.log 
uuuwww w cuuu
uuu1 2 3uuu
[root@hadoop001 shell]# 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值