在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

##shell调用脚本的模式

fork 模式调用 : fork模式是最普通的脚本调用方式,即直接在脚本里面用/bin/sh /directory/scrpit.sh 来调用 使用此方式来调用脚本的时候,会开启一个子sehll执行调用脚本,子shell调用的

        时候父shell还在,子shell执行完毕后返回到父shell,此模式下子shell的环境变量不能带回父shell,但是可以从父shell继承环境变量

exec 模式调用:exec模式调用脚本,被调用的脚本与父脚本在同一个shell内执行,但是使用exec调用一个新脚本以后,父脚本中exec执行之后的脚本内容就不会再执行了,这就是exec和source的区别

source 模式调用:source调用脚本与fork模式的区别是不会新开一个子shell来执行被调用脚本,而是在同一个Shell中执行,所以在被调用的脚本中声明的变量和环境变量都可以在主脚本中获取和使用

最新文章

  1. Spark 官方文档(4)——Configuration配置
  2. 忘记Windows7登陆密码解决办法
  3. ios UIWebView 在开发中加载文件
  4. 清北学堂模拟赛day7 石子合并加强版
  5. 模板 BFS
  6. iOS后台定位实现
  7. Vagrant工具
  8. 【HDOJ】5096 ACM Rank
  9. CUSPARSE 第三章 CUSPARAE索引和数据格式
  10. NOIP2011-普及组复赛模拟试题-第二题-买票
  11. jquery的ajax提交后,会跳转页面
  12. 多线程编程-- part 2 线程的生命周期和优先级
  13. Varnish 实战项目
  14. UIKit中ImageView动画堆叠显示的微调整
  15. CentOS7安装MySQL8.0图文教程
  16. spring boot 2.0.4 Redis缓存配置
  17. tomcat启动报错:Annotation-specified bean name &#39;patrolTrailServiceImpl&#39; for bean class [cn.oppo.inventoryService.patrolTrailServiceImpl] con
  18. MyEclipse运行Java出错:could not find the main class:test.program will exit(导入项目)
  19. api中locale或language字段,传送客户端地域信息,一般为下划线
  20. JS中Float类型加减乘除

热门文章

  1. 练习十七:python辨别数据类型
  2. 027 Remove Element 移除元素
  3. Python 魔术方法及调用方式
  4. js 数据类型及检测
  5. CentOS7.5搭建Hadoop分布式集群
  6. Java Web项目在Mac系统上启动时提示nodename nor servname provided的解决办法
  7. 服务器部署nginx报错 nginx: [warn] conflicting server name &quot;localhost&quot; on xxx.xxx.xxx.xxx:80, ignored
  8. web.config文件executionTimeout的单位
  9. 根据FileUpload的值,控制textBox的可用与否
  10. Windows下使用nvm管理多个Node.js 版本