shell 流水账

在shell脚本运行时,会先查找系统环境变量ENV,该变量指定了环境文件(加载顺序通常是/etc/profile ~/.bash_profile ~/.bashrc /etc/bashrc)


设置全局变量:
declare -x 变量名
export 变量名

常见全局变量配置文件: /etc/profile、/etc/bashrc、/etc/profile.d/目录下

/etc/motd 登录提示


当位置参数大于9时,需要使用{}括起来 ,${10}

dirname 获取路径
basename 获取文件名


for i ;do echo $i;done #相当于for i in "$@"


$!获取上一次执行脚本的PID


echo ${#parameter} 打印变量值的长度

${var:2:2} 从第二个字符开始截取,截取两个字符。

${var/oldboy/oldgirl} 替换第一个匹配的字符串

${var//oldboy/oldgirl} 替换匹配的所有字符串

${parameter:=word}
如果parameter变量值为空或未赋值,就设置这个变量值为word,并返回其值

${parameter:-word}
如果parameter变量值为空或未赋值,则会返回word字符串替代变量的值


${parameter:-word}
如果parameter变量值为空或未赋值,那么word字符串将被作为标准错误输出,否则输出变量的值

${parameter:+word}
如果parameter变量值为空或未赋值,则什么都不做,否则word字符串将替代变量的值

RETVAL=$?

find ${path-/tmp} -name "*.tar.gz" -type f -mtime +7 --delete


计算字符串长度的方法:

[root@localhost scripts]# char="I am Template"
[root@localhost scripts]# expr length "$char"
13
[root@localhost scripts]# echo ${#char}
13
[root@localhost scripts]# echo ${char} | wc -L
13
[root@localhost scripts]# echo $char |awk '{print length($0) }'
13


通过命令输出1+2+3+4+5+6+7+8+9+10

[root@localhost scripts]# seq -s "+" 10 #-s 指定分割符默认为空格
1+2+3+4+5+6+7+8+9+10

[root@localhost scripts]# echo {1..10} |tr " " "+"
1+2+3+4+5+6+7+8+9+10

[root@localhost scripts]# echo `seq -s '+' 10` =`seq -s " + " 10|xargs expr`
1+2+3+4+5+6+7+8+9+10 =55

[root@localhost scripts]# echo "10 9" | awk '{print ($1-$2)}'
1

[root@localhost scripts]# declare -i A=10 =18 #声明整数为整型之后,可以相加
[root@localhost ~]# A=A+B
[root@localhost ~]# echo $A
28


我们可以利用expr做计算时变量或字符串必须是整数的规则,来判断一个变量是否为整数
#!/bin/bash
expr $1 + 1 &>/dev/null
[ $? -eq 0 ] && echo int || echo chars


#变量的子串

#!/bin/bash
a=$1
b=$2
[ ${#a} -le 0 ] && {
echo "The first number is null"
exit 1
}
[ ${#b} -le 0 ] && {

echo "The second num is null"
exit 1

}

echo `expr $a + $b`


##0+0 expr计算之后返回退出状态码1

[root@localhost ~]# expr 0 + 0
0
[root@localhost ~]# echo $?
1

#条件测试与比较
[[ ]] 在双中括号中可以使用通配符等进行模式匹配

&& || > <等操作符可以应用于[[]]中,但不能应用于[]中,在[]中一般用-a -o -gt(用于整数) -lt(用于整数)

对于整数的运算关系,也可以使用shell的算术运算符(( ))

#测试时变量的特殊写法及问题,用[]测试变量时,如果被测试的变量不加双引号,测试结果可能会是不正确的
[root@localhost ~]# echo $test 这是一个不存在的变量

[root@localhost ~]# [ -f $test ] && echo 1 || echo 2 不加双引号测试变量,逻辑不对了
1

[root@localhost ~]# [ -f "$test" ] && echo 1 || echo 2 加了双引号逻辑就丢了
2

添加一个IP的方法
ip addr add 192.168.8.29/24 dev eth0 label eth0:0


#通过nc命令检测服务是否正常
[root@localhost scripts]# nc -w 2 127.0.0.1 80
[root@localhost scripts]# echo $?
0

#使用wget或curl命令测试网站或数据库是否正常
[root@localhost scripts]# wget --spider --timeout=10 --tries=2 www.baidu.com &> /dev/null
#--spider的意思是模拟爬取 --tries 表示如果不成功则重试两次
[root@localhost scripts]# echo $?
0

[root@localhost scripts]# wget -T 10 -q --spider www.baidu.com
[root@localhost scripts]# echo $?
0
#-q 表示安静的

#使用curl
[root@localhost scripts]# curl -I -s -w "%{http_code}\n" -o /dev/null www.baidu.com
200

#使用nmap远程监控
[root@localhost scripts]# nmap 127.0.0.1 -p 80 | grep open | wc -l
1

#测试网站是否正常脚本

脚本1
------------------------------------------------------
#!/bin/bash
if [[ `curl -I -s -o /dev/null -w "%{http_code}" 127.0.0.1` =~ "200"|"301"|"302" ]];then
echo "Nginx is running"
else
echo "Nginx is Stopped"
/etc/init.d/nginx
fi
------------------------------------------------------

脚本2

============================================================
#!/bin/bash
if [ `curl -I 127.0.0.1 2> /dev/null | head -1 | egrep "200|301|302" | wc -l` -eq 1 ];then
echo "Nginx is running."
else
echo "Nginx is down"
/etc/init.d/nginx
fi
============================================================

#比较两个整数的(传参方式)
------------------------------------------------------------
#!/bin/bash

a=$1
b=$2

Usage(){

echo "Usage:$0 NUM1 NUM2"
exit 2
}

[ $# -ne 2 ] && { ##判断传参个数
Usage
}

expr $a + 1 &> /dev/null #使用expr只能计算两个整数的原理,判断传入的参数是否为整数
RETVAL_A=$?
expr $b + 1 &> /dev/null
RETVAL_B=$?

[ $RETVAL_A -ne 0 -o $RETVAL_B -ne 0 ] && {

echo "you must input integer "
exit 1
}

if [ "$a" -lt "$b" ];then
echo "$a < $b"
elif [ "$a" -gt "$b" ];then
echo "$a > $b"
else
echo "$a = $b"
fi
exit 0
------------------------------------------------------------


##判断字符串是否为数字

a=$1
[[ "$a" =~ ^[0-9]+$ ]] && echo int || echo char


##判断字符串长度几种方法
[root@localhost scripts]# expr length Template
8
[root@localhost scripts]# var=Template
[root@localhost scripts]# echo ${#var}
8
[root@localhost scripts]# echo $var | wc -L
8
[root@localhost scripts]# echo $var | awk '{print length}'
8


-------------------------
#!/bin/bash
a=$1
b="^[0-9]{5}-[0-9]{3}$"
if [[ "$a" =~ $b ]];then
echo num
else
echo nonu
fi
-----------------------------

##shell函数的语法
函数的语法
function 函数名 () {
}

function 函数名 {
}

函数名(){
}

在shell函数里面,return命令的功能和exit类似,return的作用是退出函数,而exit是退出脚本文件

return会返回一个退出值给调用函数的当前程序,而exit会返回一个退出值给执行程序的当前shell


##最小化服务
chkconfig | egrep "sshd|crond|network|rsyslog|sysstat" | awk '{print "chkconfig",$1,"on"}' | bash

猜你喜欢

转载自www.cnblogs.com/Template/p/9241520.html