一、shell基础
1、编写.sh文件
vi wordcount.sh
2、执行文件
./wordcount.sh
报错:
3、赋予用户组权限
chmod 654 wordcount.sh
=source wordcount.sh
4、调试
vi wordcount.sh
#!/bin/bash -x
echo "www.hbinz.com"
总结:
1.开头定义 #!/bin/bash
2.sh -x xxx.sh
5、shell命令
(1)定义变量
#!/bin/bash
rz="www.hbinz.com"
date=`date`
echo $rz
echo $date
(2)引用
$k
${k}----------建议都这么写
注意:
1、=号前后不能有空格
2、字符串j建议大家双引号
3、引用变量加上{}
(3)传递参数
shell脚本外往shell脚本传递参数
#!/bin/bash
echo $1
echo $2
sh parameter.sh a b
#!/bin/bash
echo $1
echo $2
echo $#
echo "传递参数作为一个字符串显示:$*"
echo "PID:$$"
(4)数组
shell只有一维数组
(5)If判断
写法1:
#!/bin/bash
a="abc"
b="hbinz"
if [ "$a" == "$b" ];then
echo "=="
else
echo "!="
fi
#!/bin/bash
a="abc"
b="hbiz"
if [ "$a" == "$b" ]
then
echo "=="
else
echo "!="
fi
(6)if嵌套
#!/bin/bash
a="ccc"
b="hbinz"
if [ "$a" == "$b" ]
then
echo "=="
elif [ "$a" == "ccc" ]
then echo "ccc"
else
echo "!="
fi
6、循环
(1)for
第一种方式:
第二种方式:
#!/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
(2)while
#!/bin/bash
j=1
while(($j<10))
do
echo $j
let "j++"
done
7、Split分割
第一种方式:
#!/bin/bash
s="rz,jepson,xingxing,huhu,qianxi,juren"
OLD_IFS="$IFS"
IFS=","
arr=($s)
IFS="$QLD_IFS"
for x in ${arr[*]}
do
echo $x
done
第二种方式:
arr2=(${s//,/ })
for x in ${arr2[*]}
do
echo $x
done
8.案例: 监控脚本
场景: 公司机房,局域网多台机器,大数据测试环境( VM虚拟机)
(1)网络波动 VM物理机器挂了(断电,夏天来了.....)
(2)VM虚拟机挂了
方案:
ping 机器的脚本
ssh 机器的脚本(ssh脚本 需要提前配置 多台机器的互相信任关系)
zabbix耗费机器内存
CDH(比较慢)
写ping脚本:
#!/bin/bash
for ip in $(cat /opt/shell/ip_list|sed "/^#/d") #ip_list是当前目录下IP表
do
ping -c 1 -w 1 $ip &>/dev/null #三个ping有一个能通,说明服务器正常
a=$?
sleep 2
ping -c 1 -w 1 $ip &>/dev/null
b=$?
sleep 2
ping -c 1 -w 1 $ip &>/dev/null
c=$?
sleep 2
DATE=$(date +%F" "%H:%M)
if [ $a -ne 0 -a $b -ne 0 -a $c -ne 0 ];then
#weixin alert
#python /opt/monitor/sh/weixin_alert.py "Critical: ${ip} can't ping"
echo "Critical: ${ip} can't ping"
else
echo "$ip ping is successful."
fi
done
$? 上一条命令语句的值,0表示没用错误,其他值表示有错误
> 重定向到哪里
& 等同于
-c 1 ping一次
-w 1 ping的超时时间为1s
-ne 不等于
-a 如果存在就为真
写ssh脚本:
#!/bin/bash
for ip in $(cat /opt/shell/ip_list|sed "/^#/d") #ip_list是当前目录下IP表
do
ssh -o ConnectTimeout=5 $ip date &>/dev/null #ssh 能够返回date 说明服务器正常
a=$?
if [ $a -ne 0 ];then
#weixin alert
#python /opt/monitor/sh/weixin_alert.py "Critical: ssh ${ip} checking is failed."
echo "ssh ${ip} checking is failed."
else
echo "ssh ${ip} is successful."
fi
done
9、维护脚本
场景: 大数据多台机器,Phoenix jar
改造Phoenix,jar升级
中心机器1台:---->多台机器
新版本jar 旧版本替换为新的jar
scp+多台机器的互相信任关系
脚本:
#!/bin/bash
HOSTNAME_LIST="hadoop001,hadoop002,hadoop003,hadoop004"
JAR=phoenix-4.10.0-cdh5.12.0-server.jar
SOURCEPATH=/opt/sync/jars
TARGETPATH=/opt/cloudera/parcels/CDH/lib/hbase/lib
OLD_IFS="$IFS"
IFS=","
arr=($HOSTNAME_LIST)
IFS="$OLD_IFS"
for hostname in ${arr[@]}
do
echo "$hostname:"
ssh "$hostname" "rm -f $TARGETPATH/$JAR"
scp $SOURCEPATH/$JAR "$hostname":$TARGETPATH/$JAR
ssh "$hostname" "ls -l $TARGETPATH/$JAR"
done
SOURCEPTAH 中心机器的jar包
TARGETPATH 目标地址
判断文件是否为存在:
#!/bin/bash
if [ -f "/opt/shell/array.sh" ];then
echo "文件存在"
else
echo "文件不存在"
fi
判断文件是否为空:
#!/bin/bash
if [ ! -s /opt/shell/fileis.sh ]
then
echo "file is empty"
else
echo "file is not empty"
fi