shell编程(一)

NameVersion
Centos 73.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

 

shell是什么

  • shell是一种脚本语言 aming_linux blog.lishiming.net

  • 可以使用逻辑判断、循环等语法

  • 可以自定义函数

  • shell是系统命令的集合

  • shell脚本可以实现自动化运维,能大大增加我们的运维效率

shell基本运算符参考网址:http://www.runoob.com/linux/linux-shell-basic-operators.html

shell脚本结构和执行方法

  • 开头需要加#!/bin/bash

  • 以#开头的行作为解释说明

  • 脚本的名字以.sh结尾,用于区分这是一个shell脚本

  • 执行方法有两种

  • chmod +x 1.sh; ./1.sh

  • bash 1.sh

  • 查看脚本执行过程 bash -x 1.sh

  • 查看脚本是否语法错误 bash -n 1.sh

 

data命令的用法

年月日

  [root@localhost centos7]# date +%Y-%m-%d
  2018-07-29
  [root@localhost centos7]# date +%y-%m-%d
  18-07-29

时间

  [root@localhost centos7]# date +%H:%M:%S
  22:28:53
  [root@localhost centos7]# date +%T
  22:29:02

时间戳

  [root@localhost centos7]# date +%s
  1532892605
  ​
  [root@localhost centos7]# date -d @1532892605
  2018年 07月 29日 星期日 22:30:05 MSK

一天后

  [root@localhost centos7]# date -d "+1day"
  2018年 07月 30日 星期一 22:31:39 MSK

一天前

  [root@localhost centos7]# date -d "-1day"
  2018年 07月 28日 星期六 22:32:11 MSK

一月前

  [root@localhost centos7]# date -d "-1month"
  2018年 06月 29日 星期五 22:33:06 MSK

一分钟前

  [root@localhost centos7]# date -d "-1min"
  2018年 07月 29日 星期日 22:32:15 MSK

星期

  [root@localhost centos7]# date +%w
  0
  [root@localhost centos7]# date +%W
  30

 

shell脚本中的变量

  • 当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替

  • 使用条件语句时,常使用变量    if [ $a -gt 1 ]; then ... ; fi

  • 引用某个命令的结果时,用变量替代   n=`wc -l 1.txt`

  • 写和用户交互的脚本时,变量也是必不可少的  read -p "Input a number: " n; echo $n   如果没写这个n,可以直接使用$REPLY

  • 内置变量 $0, $1, $2…    $0表示脚本本身,$1 第一个参数,$2 第二个 ....       $#表示参数个数

  • 数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]

 

shell中的逻辑判断

  • 格式1:if 条件 ; then 语句; fi

  • 格式2:if 条件; then 语句; else 语句; fi

  • 格式3:if …; then … ;elif …; then …; else …; fi

  • 逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格

  • 可以使用 && || 结合多个条件

  • if [ $a -gt 5 ] && [ $a -lt 10 ]; then

  • if [ $b -gt 5 ] || [ $b -lt 3 ]; then

 

if判断文件、目录属性

  • [ -f file ]判断是否是普通文件,且存在

  • [ -d file ] 判断是否是目录,且存在

  • [ -e file ] 判断文件或目录是否存在

  • [ -r file ] 判断文件是否可读

  • [ -w file ] 判断文件是否可写

  • [ -x file ] 判断文件是否可执行

 

if 判断的一些特殊用法

  • if [ -z "$a" ]  这个表示当变量a的值为空时会怎么样

  • if [ -n "$a" ] 表示当变量a的值不为空

  • if grep -q '123' 1.txt; then  表示如果1.txt中含有'123'的行时会怎么样

  • if [ ! -e file ]; then 表示文件不存在时会怎么样

  • if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…

  • [ ] 中不能使用<,>,==,!=,>=,<=这样的符号

 

shell中的case判断

  • 格式case变量名in

  value1)
     command
     ;;
  value2)
     command
     ;;
  *)
    commond
    ;;
    esac
  • 在case程序中,可以在条件中使用|,表示或的意思,比如

  2|3)
    command
    ;;
  • 判断输入的分数所处的区间是不是定义的区间,是的话根据不同分数评级。

  #!/bin/bash
  read -p "Please input a number: " n
  if [ -z "$n" ]
  then
      echo "Please input a number."
      exit 1
  fi
  ​
  n1=`echo $n|sed 's/[0-9]//g'`
  if [ -n "$n1" ]
  then
   echo "Please input a number."
   exit 1
  fi
  ​
  if [ $n -lt 60 ] && [ $n -ge 0 ]
  then
      tag=1
  elif [ $n -ge 60 ] && [ $n -lt 80 ]
  then
      tag=2
  elif [ $n -ge 80 ]  && [ $n -lt 90 ]
  then
      tag=3
  elif [ $n -ge 90 ] && [ $n -le 100 ]
  then
      tag=4
  else 
      tag=0
  fi
  case $tag in
      1)
      echo "not ok"
          ;;
      2)
          echo "ok"
          ;;
      3)
          echo "ook"
          ;;
      4)
          echo "oook"
          ;;
      *)
          echo "The number range is 0-100."
          ;; 
  esac

 

for循环

语法:for 变量名 in 条件; do …; done

  • 数字循环

  #!/bin/bash
  sum=0
  for i in `seq 1 100`
  do
      sum=$[$sum+$i]
      echo $i
  done
  echo $sum
  • 文件列表循环

  #!/bin/bash
  cd /etc/
  for a in `ls /etc/`
  do
      if [ -d $a ]
      then
         ls -d $a
      fi
  done
  ​

 

while循环

语法 while 条件; do … ; done

while后面跟的“ :”是真的意思(也可以是“ 1 ” 、“ true ”,同样都是真),真就代表后面是死循环。

  • 系统负载大于10就发邮件告警,间隔30秒

  #!/bin/bash
  while :
  do
      load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
      if [ $load -gt 10 ]
      then
          top|mail -s "load is high: $load" asldkfls@11.com
      fi
      sleep 30
  done
  ​
  • 判断输入的是否是数字的交互死循环脚本,continue重新执行,break跳出死循环

  #!/bin/bash
  while :
  do
      read -p "Please input a number: " n
      if [ -z "$n" ]
      then
          echo "you need input sth."
          continue
      fi
      n1=`echo $n|sed 's/[0-9]//g'`
      if [ -n "$n1" ]
      then
          echo "you just only input numbers."
          continue
      fi
      break
  done
  echo $n

 

break跳出循环

  • 对循环1到5进行判断,如果i等于3直接退出死循环输出aaaaaaa,后面的4和5都不执行了

  #!/bin/bash
  for i in `seq 1 5`
  do
      echo $i
      if [ $i == 3 ]
      then
          break
      fi
      echo $i
  done
  echo aaaaaaa

 

continue结束本次循环

  • 忽略continue之下的代码,直接进行下一次循环,与break的不同是直接把4和5都执行完

  #!/bin/bash
  for i in `seq 1 5`
  do
      echo $i
      if [ $i == 3 ]
      then
          continue
      fi
      echo $i
  done
  echo $i

 

exit直接退出脚本

  #!/bin/bash
  for i in `seq 1 5`
  do
      echo $i
      if [ $i == 3 ]
      then
          exit
      fi
      echo $i
  done
  echo aaaaaaa

 

 

 

扩展:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值