Shell编程实战

Shell 编程在日常的大数据开发中还是使用非常实用的

1,第一个shell实例及介绍

#!/bin/bash
echo "hello world!"

写完脚本后要么 sh xxx.sh执行,
要么 chmod u+x xxx.sh 然后 ./xxx.sh执行
不用多说,自然很简单,打印hello,world!

shell debug 模式运行 sh -x xxx.sh
或者在脚本中第一行 #!/bin/bash -x
即可看到运行脚本是每一详细信息

2,shell变量定义和引用

注意:
①变量名大写
②= 前后不能有空格
③使用变量 习惯的使用{} ,加花括号是为了帮助解释器识别变量的边界
④将命令的结果赋值给变量

variable=`command`
variable=$(command)

实例

#!/bin/bash
VARB="hello"
TODAY_DATE=`date`
TODAY_DATE2=$(date)
TODAY_DATE3='date'
echo "$VARB"
echo "$TODAY_DATE"
echo "$TODAY_DATE2"
echo "$TODAY_DATE3"
[hadoop@tool shell]$ sh variable.sh 
hello
Thu Aug 22 15:30:54 CST 2019
Thu Aug 22 15:30:54 CST 2019
date

脚本参数实践

[hadoop@tool shell]$ cat param.sh 
#!/bin/bash
echo $1 #获取第一个参数
echo $0 #获取当前脚本名称
CURRENT_DIR=$(cd $(dirname $0);pwd) #当前脚本所在路径
echo $CURRENT_DIR
[hadoop@tool shell]$ sh param.sh 111
111
param.sh
/home/hadoop/kzw/shell

3,if/else

语法:

if  condition;  then
    statement(s)
fi
或者
if  condition
then
    statement(s)
fi

请注意 condition 后边的分号;,当 if 和 then 位于同一行的时候,这个分号是必须的,否则会有语法错误

实例

[hadoop@tool shell]$ cat second.sh 
#!/bin/bash

echo $$ "当前进程号"  # $$ 两个美元符号代表取当前进程pid
echo "请输入y/n"
read FLAG  #read从控制台读取输入的值
echo "flag:${FLAG}"
if [ ${FLAG} == "y" ]; then
        echo "stdin ${FLAG}"
        echo "you are win"
else 
        echo "stdin ${FLAG}"
        echo "you are also win"
fi

注意点:
a. [ ] 前后空格
b. == 前后空格

[hadoop@tool shell]$ cat if2.sh 
#!/bin/bash
echo "input int num:"
read num
if (($num == 1));then
        echo "monday"
elif (($num == 2));then
        echo "tuesday"
else 
        echo "error num"
fi
[hadoop@tool shell]$ sh if2.sh 
input int num:
1
monda

4,数组

注意:
①,用括号( )来表示数组,数组元素之间用空格来分隔。
array_name=(ele1 ele2 ele3 … elen)
② 取值
${array_name[index]} //index从0开始

实例

#!/bin/bash
arr=(1 2 3 4 5 6 7)
echo "${arr[1]}" #数组下标也是从0开始
echo "${arr[*]}" #获取数组所有元素
echo ${#arr[*]} #获取数组长度

VALUE=${arr[2]} #赋值
echo "the value:$VALUE"
[hadoop@tool shell]$ sh arr.sh 
2
1 2 3 4 5 6 7
7
the value:3

5 shell 循环(for循环,while循环)

注意:
① Shell (()):对整数进行数学运算的命令,(( )) 只能进行整数运算,不能对小数(浮点数)或者字符串进行运算。 bc 命令可以用于小数运算。

value_list 表示取值列表,in 是 Shell 中的关键字。
每次循环都会从 value_list 中取出一个值赋给变量 variable,然后进入循环体(do 和 done 之间的部分),执行循环体中的 statements。直到取完 value_list 中的所有值,循环就结束了
for variable in value_list
do
    statements
done
# 求1加到一百的和
[hadoop@tool shell]$ cat for.sh 
#!/bin/bash
sum=0
i=0
for((i=0;i<=100;i++))
do
        ((sum += i))
done
echo "the sum=$sum"



sum2=0
for n in {1..100}
do
        ((sum2 += n))
done
        echo "the sum2 = $sum2"


##while
sum3=0
j=1
while((j<=100))
do
        ((sum3 += j))
        ((j++))#等同于  let "j++"
done
        echo "the sum3 = $sum3"
[hadoop@tool shell]$ sh for.sh 
the sum = 5050
the sum2 = 5050
the sum3 = 5050

6 分隔

这个分隔写法差不多固定,需要的时候查看下就行

[hadoop@tool shell]$ cat split.sh 
#!/bin/bash
STR="aaa,bbb,ccc,ddd,eee,fff,hhh"
OLD_IFS="$IFS"
IFS=","
arr=($STR)
IFS="$OLD_IFS"
for x in ${arr[*]}
do
        echo "$x"
done
[hadoop@tool shell]$ sh split.sh 
aaa
bbb
ccc
ddd
eee
fff
hhh

7 awk 命令

awk 是逐行处理的,一行一行进行处理,

准备好一个测试log

[hadoop@tool shell]$ cat test.log 
a b c
1 2 3
d e f
4 5 6

实践

# 打印第一列 awk '{print $1}'
[hadoop@tool shell]$ awk '{print $1}' test.log 
a
1
d
4
# 打印所有
[hadoop@tool shell]$ awk '{print $0}' test.log  
a b c
1 2 3
d e f
4 5 6

#awk '{print $1,$2}' 打印第一和第二列

# 打印第一行
[hadoop@tool shell]$ awk 'NR==1{print}' test.log 
a b c
# 打印第一行第一列的元素
[hadoop@tool shell]$ awk 'NR==1{print $1}' test.log 
a
# 打印大于第一行的元素
[hadoop@tool shell]$ cat test.log | awk 'NR>1{print}'
1 2 3
d e f
4 5 6

#加一个后缀
[hadoop@tool shell]$ awk '{print $1,$2,666}' test.log 
a b 666
1 2 666
d e 666
4 5 666

#随意添加列
[hadoop@tool shell]$ awk '{print "添加前缀",$1,"666",$2,"添加后缀"}' test.log                   
添加前缀 a 666 b 添加后缀
添加前缀 1 666 2 添加后缀
添加前缀 d 666 e 添加后缀
添加前缀 4 666 5 添加后缀


8sed 命令

准备好一个测试log

[hadoop@tool shell]$ cat test.log 
a b c
1 2 3
d e f
4 5 6

替换语法:

sed -i 's/old/new/' test.txt
匹配每一行的第一个old替换为new
-i 改变源文件

sed 's/old/new/gi' test.txt
匹配所有old替换为new,g 代表一行多个,i 代表匹配忽略大小写

sed '3,9s/old/new/gi' test.txt
匹配第 3~9 行所有old替换为new
# -i参数是改变源文件
[hadoop@tool shell]$ sed -i 's/a/tihuanA/' test.log 
[hadoop@tool shell]$ cat test.log 
tihuanA b c
1 2 3
d e f
4 5 6

# sed -n '2p' test.log 只打印第 2 行(p参数打印)
[hadoop@tool shell]$ sed -n '2p' test.log 
1 2 3

# 只打印第 1~3 行(p参数打印)
[hadoop@tool shell]$ sed -n '1,3p' test.log  
tihuanA b c
1 2 3
d e f

# 在第 2 行后面新增一行内容 (a参数是新增)
[hadoop@tool shell]$ sed '2a testContent' test.log 
tihuanA b c
1 2 3
testContent
d e f
4 5 6
# 修改第2行内容 (c参数是替换)
[hadoop@tool shell]$ sed '2c updateContent' test.log 
tihuanA b c
updateContent
d e f
4 5 6

# 在第 2 行后面删除一行内容 (d参数是删除)
[hadoop@tool shell]$ sed '2d' test.log 
tihuanA b c
d e f
4 5 6


9 实战(shell读取一个文件)

目标文件:

#数据库IP
mysql_ip=192.168.11.140
#数据库端口
mysql_port=3306
#数据库名称
mysql_db=school
#数据库username
username=admin
#数据库password
passwd=admin

shell 脚本:

#!/bin/bash

while read line;do
eval "$line"
done < /home/hadoop/mysql_140.cfg


mysql_ip="${mysql_ip}"
mysql_port="${mysql_port}"
echo "${mysql_ip}"
echo "$mysql_port"
echo "${passwd}"

控制台正常输出结果

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冬瓜螺旋雪碧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值