脚本的基础使用"/>
Shell脚本的基础使用
1.shell脚本的创建执行
创建wordcount.sh
#!/bin/bash
echo "www.google.com"
执行:/opt/shell/wordcount.sh
给该脚本的用户以及用户组加上执行权限
chmod 654 xxx.sh
shell 命令的debug
在#!/bin/bash 后面加上-x的参数可以在控制台中打印出执行的命令以及执行命令的结果
sh -x wordcount.sh 可以使用这个命令来进行调试
生产上面开发shell脚本
1.开头需要定义#!/bin/bash
2.调试脚本 #!/bin/bash -x
注意:使用sh执行脚本的时候 sh xxx.sh 不论在脚本的第一行是否加上#!/bin/bash 都可以执行该脚本
2.定义变量及引用
vim variable.sh
#!/bin/bashaa="www.google.com"
date=`date`echo $aa
echo $date
静态变量:值不会变化 k=“v” -->字符串 k=v -->数值
动态变量:值会发生改变 k=`v`
等号前后不能有空格
变量的引用:$a ${aa} :变量的引用需要加上美元符号
生产上面建议引用变量的时候加上大括号{}
3.传递参数
向shell脚本中传递参数
$1
$2
vim param.sh
#!/bin/bashecho $1
echo $2
echo "参数个数:$#"
echo "传递参数作为一个字符串显示:$*"
echo "PID:$$"
$1 :传递进来的第一个参数
$2 :传递进来的第二个参数
$# :传递进来的参数的个数
$* :传递进来的全部参数作为一个参数
$$ :显示PID
4.数组
一维数组
vim array.sh
#!/bin/basharr=(math chinese english)
echo ${arr} #只打印第一个
echo ${arr[*]} #打印数组中的所有的值
echo ${arr[0]} #打印第一个值
echo ${#arr[*]} #打印数组的个数echo ${arr[@]} #打印数组中的所有的值
echo ${#arr[@]} #打印数组的个数
${arr} :数组中的第一个元素
${arr[*]} :取数组中的所有元素
${arr[0]} :取数组中的第一个元素
${#arr[*]}:取数组中元素的个数
上面的* 也可用@ 来表示
5.if判断
vi if.sh
#!/bin/basha="abc"
b="jepson"if [ $a == $b ];then if里面的中括号前后要打上空格echo "=="else echo "!="
fi if [ $a == $b ];then 表示的是写到同一行中if [ "$a" == "$b"] 这种的写法也是可以的
多个if else的使用if[ "$a" == "$b" ];then echo "=="
elif[ "$a" == "ccc" ];thenecho "ccc"
elseecho "!="
fi
6.循环
for循环 while循环
vim for.sh
#!/bin/bash
echo "=========for循环=============="
for x in 1 2 3 4 5
doecho $x
doneecho "==========for循环============="
for ((int i=1;i<10;i++))
do echo $i
doneecho "===========while循环============"j=1
while(($j<10))
doecho ${j}let "j++"
done
7.分割字符串(背下来)
vi split.sh
#!bin/bashs="math,english,chinese,scala,spark,hdfs,hadoop"
OLD_IFS= "$IFS"
IFS=","
arr=($s)
IFS="$OLD_IFS"for x in ${arr[*]}
doecho $x
done
第二种:
vim splits.sh
#!/bin/bash
s="math,english,chinese,scala,spark,hdfs,hadoop"
arr2=(${s//,/ })
for x in ${arr2[*]}
doecho $x
done
8.案例:监控脚本
vi ganglia.sh
#!/bin/bash
for ip in $(cat /opt/monitor/sh/ping/ip_list|sed "/^#d")
doping -c 1 $ip &>/dev/null # 三个ping 有一个能通,说明服务器正常a=$?sleep 2ping -c 1 $ip &>/dev/nulla=$?sleep 2ping -c 1 $ip &>/dev/nulla=$?sleep 2DATE=$(date +%F" "%H:%M)if[ $a -ne 0 -a $b -ne 0 -a $c -ne 0 ];then#weixin alertpython /opt/monitor/sh/weixin_alert.py "Critical: ${ip} can't ping"elseecho "$ip ping is successful."fi
done
ssh
for ip in $(cat /opt/monitor/sh/ping/ip_list|sed "/^#d")
dossh -o ConnectTimeout=5 $ip date &>/dev/null # 能够正常返回时间说明是正常的
done
shell 判断数组是否为空
#!/bin/bash
para1=()
if [ ! -n "$para1" ];thenecho "param is null"
else echo "param is not null"
fi
shell 判断数组是否包含某一字符串或数字
方法一:推荐
#!/bin/basharray=(hadoop,hive,spark,kafka,hbase,flink)
var=hadoopsif [ ${array[@]/${var}/} != ${array[@]} ];thenecho "hadoop is in array"
elseecho "hadoop is not in array"
fi
shell 判断文件夹、文件、变量是否存在参数
-d:判断文件夹是否存在
-f:判断文件是否存在
-x:判断是否存在并且是否具有可执行权限
-n:判断一个变量是否有值
#!/bin/bashflod=/root/shell/fload
file=/root/shell/fload/file.txtecho "==========判断文件夹=========="if [ -d ${flod} ];thenecho "${flod} is in the path"
elseecho "not in the path"
fiecho "==========判断文件=========="if [ -x ${file} ];thenecho "file is exisit"
elseecho "file is not exisit"
fiecho "==========判断变量==========" if [ ! -n ${param} ];thenecho "param is not null"
elseecho "param is null"
fi
shell 判断文件是否为空
-e:文件存在
-s:文件长度不为0
-r:文件具有读权限
-w:文件具有写权限
-x:文件具有执行权限
#!/bin/bashfile=/root/shell/fload/sutdent.txt
flod=/root/shell/floadecho "==========判断文件是否存在=========="
if [ -e ${file} ];thenecho "file is exisit"
elseecho "file is not exisit"
fiecho "==========判断文件是否为空=========="
if [ ! -s ${file} ];thenecho "file is null"
elseecho "file is xxx kb"
fiecho "==========判断文件是否具有执行权限=========="
if [ -x ${file} ];thenecho "file have the execute permission"
else echo "file not have the execute permission"
fi
更多推荐
Shell脚本的基础使用
发布评论