shell脚本编程(基础)

  • 输出字符串hello world
[root@hadoop001 learn_shell]# vi test.sh
#!/bin/bash

echo "hello world!"

# 默认是没有可执行权限的
[root@hadoop001 learn_shell]# ll 
总用量 4
-rw-r--r-- 1 root root 34 8月  24 07:53 test.sh

# 修改文件权限 给文件拥有者可执行权限
[root@hadoop001 learn_shell]# chmod u+x test.sh
# 两种执行方式 1.相对路径 2.绝对路径
[root@hadoop001 learn_shell]# ./test.sh
hello world!
[root@hadoop001 learn_shell]# /root/learn_shell/test.sh
hello world!
  • shell debug模式
[root@hadoop001 learn_shell]# vi debug.sh
#!/bin/bash -x 

echo "hello"
echo "world"
echo "!"
[root@hadoop001 learn_shell]# chmod u+x debug.sh
[root@hadoop001 learn_shell]# ./debug.sh 
+ echo hello
hello
+ echo world
world
+ echo '!'
!
  • 变量定义与引用
[root@hadoop001 learn_shell]# vi variable.sh
#!/bin/bash

HW="hello world"
DATE1="date"
DATE2=`date`

echo $HW
echo ${DATE1}
echo ${DATE2}

[root@hadoop001 learn_shell]# chmod u+x variable.sh 
[root@hadoop001 learn_shell]# ./variable.sh 
hello world
date
2019年 08月 24日 星期六 09:07:33 CST

⚠️ 注意点

  1. = 前后不能有空格
  2. 变量名称一般为大写
  3. 调用变量最好使用{} 例如:${date1}
  4. 字符串可用单引号 也可以用双引号 区别是双引号中可以引用变量,如果字符串是连起来的,中间没有空格,也可以不加单引号、双引号
  • 参数传递
[root@hadoop001 learn_shell]# vi param.sh
#!/bin/bash

echo "第一个参数:"$1  # 获取第一个参数
echo "第二个参数:"$2  # 获取第二个参数
echo "$#"            # 参数的个数
echo "$*"            # 打印所有的参数
echo "PID: $$"       # 执行当前sh的pid
[root@hadoop001 learn_shell]# chmod u+x param.sh
[root@hadoop001 learn_shell]# ./param.sh hello world
第一个参数:hello
第二个参数:world
2
hello world
PID: 2469
[root@hadoop001 learn_shell]# ./param.sh "hello world"
第一个参数:hello world
第二个参数:
1
hello world
PID: 2470
  • 数组
[root@hadoop001 learn_shell]# vi array.sh
#!/bin/bash

ARR=(a b c d)
echo ${ARR[@]}   # 打印所有元素
echo ${ARR[3]}   # 打印指定元素 下角标从0开始
echo ${#ARR[@]}  # 数组长度
[root@hadoop001 learn_shell]# chmod u+x array.sh 
[root@hadoop001 learn_shell]# ./array.sh 
a b c d
d
4

⚠️ 注意点

1.数组内部元素 用空格隔开

  • if-else
# if-else
[root@hadoop001 learn_shell]# vi if.sh 
#!/bin/bash

A="abc"
B="edf"
if [ $A == $B ];then
    echo "=="
else
    echo "!="
fi
[root@hadoop001 learn_shell]# chmod u+x if.sh 
[root@hadoop001 learn_shell]# ./if.sh 
!=

# if-elif
[root@hadoop001 learn_shell]# vi if.sh 
#!/bin/bash

a="abc"
b="edf"
if [ $a == $b ];then
    echo "==$b"
elif [ $a == "abc" ];then
    echo "==abc"
else 
    echo "!="
fi

⚠️ 注意点

  1. if 后面的 [ ] 需要给前后元素留空格

  2. == 前后也需要空格

  3. 注意格式:if 开头 fi 结尾

  • for 循环
[root@hadoop001 learn_shell]# vi forwhile.sh 
#!/bin/bash

echo "---------------------------for 第一种写法"
for X in 1 2 3 4 5
do
    echo $X
done

echo "---------------------------for 第二种写法"

for ((I=1;I<6;I++))
do 
    echo $I
done

echo "---------------------------while 写法"

A=1
while (($A<6))
do
    echo $A
    let "A++"    # A+1
done
[root@hadoop001 learn_shell]# chmod u+x forwhile.sh 
[root@hadoop001 learn_shell]# ./forwhile.sh 
---------------------------for 第一种写法
1
2
3
4
5
---------------------------for 第二种写法
1
2
3
4
5
---------------------------while 写法
1
2
3
4
5
  • 分割
[root@hadoop001 learn_shell]# vi split.sh 
#!/bin/bash

S="a,b,c,d,e"
# 固定写法
OLD_IFS="$IFS"
IFS=","
ARR=($S)
IFS="$OLD_IFS"

for X in ${ARR[*]}
do 
    echo $X
done
[root@hadoop001 learn_shell]# chmod u+x split.sh 
[root@hadoop001 learn_shell]# ./split.sh 
a
b
c
d
e
  • awk
[root@hadoop001 learn_shell]# vi test.log 
a b c
1 2 3

# 打印第一列
[root@hadoop001 learn_shell]# cat test.log | awk '{print $1}'
a
1

# 打印第一、第二列
[root@hadoop001 learn_shell]# cat test.log | awk '{print $1,$2}'
a b
1 2

# 打印第一、第二拼接列
[root@hadoop001 learn_shell]# cat test.log | awk '{print $1$2}'
ab
12

# 打印第一行
[root@hadoop001 learn_shell]# cat test.log | awk 'NR==1{print}'
a b c

# 打印第一行的第一列
[root@hadoop001 learn_shell]# cat test.log | awk 'NR==1{print $1}'
a

# 打印除了第一行以外的
[root@hadoop001 learn_shell]# cat test.log | awk 'NR>1{print}'
1 2 3

# 根据指定分隔符 打印第一列
# 修改下test.log
[root@hadoop001 learn_shell]# vi test.log
a,b,c
1,2,3
# 打印
[root@hadoop001 learn_shell]# cat test.log | awk -F "," '{print $1}'
a
1
  • sed

一般用作替换

# 替换第一个
[root@hadoop001 learn_shell]# cat test.log
a,b,c
1,2,3
[root@hadoop001 learn_shell]# sed -i 's/a/aa/' test.log
[root@hadoop001 learn_shell]# cat test.log
aa,b,c
1,2,3

# 全局替换
[root@hadoop001 learn_shell]# vi test.log
aa,b,c,aa
1,2,3
[root@hadoop001 learn_shell]# sed -i 's/aa/ii/g' test.log
[root@hadoop001 learn_shell]# cat test.log 
ii,b,c,ii
1,2,3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值