溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

3 shell編程知識

發(fā)布時間:2020-06-28 14:04:57 來源:網(wǎng)絡 閱讀:140 作者:chenshengsheng 欄目:系統(tǒng)運維

[TOC]

一,DAY6

1.shell腳本介紹

  • shell是一種腳本語言 aming_linux blog.lishiming.net
  • 可以使用邏輯判斷、循環(huán)等語法
  • 可以自定義函數(shù)
  • shell是系統(tǒng)命令的集合
  • shell腳本可以實現(xiàn)自動化運維,能大大增加我們的運維效率

2.shell腳本結(jié)構(gòu)和執(zhí)行

  • 開頭需要加#!/bin/bash,這是shell的固有格式,指定接下來要運行的命令,是通過那一個解釋器來操作的
  • 以#開頭的行作為解釋說明,某些啟動腳本#號開頭的行有特定作用
    #! /bin/bash
    # chkconfig: 2345 10 90    定義啟動級別,
    # description: Activates/Deactivates all network interfaces configured to \    描述信息  這兩行一定要,沒有這兩行就不能添加到chkconfig列表里去
    #              start at boot time.  
    #
  • 腳本的名字以.sh結(jié)尾,用于區(qū)分這是一個shell腳本
  • 執(zhí)行方法有兩種:
    • chmod +x 1.sh; ./1.sh
    • bash 1.sh
    • 執(zhí)行腳本用bash或sh都可以
      [root@mydb1 test]# ls -l /bin/sh 
      lrwxrwxrwx 1 root root 4 Apr 24  2019 /bin/sh -> bash
      [root@mydb1 test]# ls -l /bin/bash 
      -rwxr-xr-x 1 root root 906568 Mar 23  2017 /bin/bash
  • 查看腳本執(zhí)行過程 bash -x 1.sh
  • 查看腳本是否語法錯誤 bash -n 1.sh 只能檢查語法上的錯誤

3.date命令用法

  • date +%Y-%m-%d, date +%y-%m-%d 年月日
  • date +%H:%M:%S = date +%T 時間
  • date +%s 時間戳
  • date -d @1504620492
  • date -d "+1day" 一天后
  • date -d "-1 day" 一天前
  • date -d "-1 month" 一月前
  • date -d "-1 min" 一分鐘前
  • date +%w, date +%W 星期

    • 演示:

      [root@mydb1 test]# date +%y  簡寫年
      19
      [root@mydb1 test]# date +%Y  
      2019
      [root@mydb1 test]# date +%m  月份
      11
      [root@mydb1 test]# date +%M  分
      40
      [root@mydb1 test]# date +%d  天
      20
      [root@mydb1 test]# date +%D   月/日/年
      11/20/19
      [root@mydb1 test]# date +%Y%m%d 年月日 
      20191120
      [root@mydb1 test]# date +%F    年-月-日
      2019-11-20
      [root@mydb1 test]# date +%H    時
      11
      [root@mydb1 test]# date +%s   時間戳,距離197001010000到現(xiàn)在過去多少秒
      1574221403
      [root@mydb1 test]# date +%S   秒
      30
      [root@mydb1 test]# date +%T   時:分:秒
      11:49:36
      [root@mydb1 test]# date +%h  英文月份
      Nov
      [root@mydb1 test]# date +%H:%M:%S   等同于T
      11:52:40
      [root@mydb1 test]# date +%w   星期幾
      3
      [root@mydb1 test]# date +%W    一年中第幾周
      46
      
          [root@mydb1 test]# cal   日歷形式
          November 2019   
          Su Mo Tu We Th Fr Sa
                                          1  2
           3  4  5  6  7  8  9
          10 11 12 13 14 15 16
          17 18 19 20 21 22 23
          24 25 26 27 28 29 30
      
          [root@mydb1 ~]# date +'%Y%m%d' -d '1 days ago'
          20191119
          [root@mydb1 ~]# date +'%Y%m%d' -d '-1 days'
          20191119
      
          [root@mydb1 ~]# date +%F -d '1 year' 1年以后
          2020-11-20
          [root@mydb1 ~]# date +%F -d '-1 year' 1年以前
          2018-11-20
      
          [root@mydb1 ~]# date +%T
          13:55:03
          [root@mydb1 ~]# date +%T -d '-1 hour'  1小時以前
          12:55:18
      
          [root@mydb1 ~]# date +%s
          1574229561
          [root@mydb1 ~]# date -d @1574229561   將時間戳轉(zhuǎn)換為具體日期
          Wed Nov 20 13:59:21 CST 2019
      
          [root@mydb1 ~]# date +%s -d "2019-11-20 14:01:01"  將具體時間轉(zhuǎn)換為時間戳
          1574229661

4.shell腳本中的變量

  • 當腳本中使用某個字符串較頻繁并且字符串長度很長時就應該使用變量代替
  • 使用條件語句時,常使用變量 if [ $a -gt 1 ]; then ... ; fi
  • 引用某個命令的結(jié)果時,用變量替代 n=`wc -l 1.txt`
  • 寫和用戶交互的腳本時,變量也是必不可少的read -p "Input a number: " n; echo $n 如果沒寫這個n,可以直接使用$REPLY
  • 內(nèi)置變量 $0, $1, $2… $0表示腳本本身,$1 第一個參數(shù),$2 第二個 .... $#表示參數(shù)個數(shù)
  • 數(shù)學運算a=1;b=2; c=$(($a+$b))或者$[$a+$b]

5.shell腳本中的邏輯判斷

  • 格式1:if 條件 ; then 語句; fi
    #!/bin/bash
    a=5
    if [ $a -gt 3 ]
    then
       echo ok
    fi
  • 格式2:if 條件; then 語句; else 語句; fi
    #!/bin/bash
    a=2
    if [ $a -gt 3 ]
    then
       echo ok
    else
       echo nook
    fi
  • 格式3:if …; then … ;elif …; then …; else …; fi
    #!/bin/bash
    a=3
    if [ $a -gt 4 ]
    then
       echo ">1"
    elif [ $a -lt 6 ]
    then
       echo "<6 && >1"
    else
       echo nook
    fi
  • 邏輯判斷表達式:
    • if [ $a -gt $b ]
    • if [ $a -lt 5 ]
    • if [ $b -eq 10 ]
    • -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到處都是空格
  • 可以使用 && || 結(jié)合多個條件
    • if [ $a -gt 5 ] && [ $a -lt 10 ]; then 并且
    • if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或者

二,DAY7

6.文件目錄屬性判斷

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

    #!/bin/bash
    f="/tmp/test.txt"
    if [ -f $f ]
    then
       echo $f exist
    else
       touch $f
    fi
    
    如果判斷不存在,加上感嘆號!
    #!/bin/bash
    f="/tmp/abc.txt"
    if [ ! -f $f ]
    then
       touch $f
    fi
  • [ -d file ] 判斷是否是目錄,且存在
  • [ -e file ] 判斷文件或目錄是否存在
  • [ -r file ] 判斷文件是否可讀
  • [ -w file ] 判斷文件是否可寫
  • [ -x file ] 判斷文件是否可執(zhí)行

7.if特殊用法

  • if [ -z "$a" ] 這個表示當變量a的值為空時會怎么樣
    #!/bin/bash
    if [ ! -f /tmp/abc.log ]
    then
       echo "/tmp/abc.log not exist"
       exit
    fi
    n=`wc -l /tmp/abc.log`
    if [ -z "$n" ]
    then
       echo error
       exit
    elif [ $n -gt 100 ]
    then
       echo $n
    fi
  • if [ -n "$a" ] 表示當變量a的值不為空,可以判斷一個文件的內(nèi)容不為空
    [root@localhost tmp]# if [ -n if.sh ];then echo ok;fi  判斷文件不用雙引號
    ok
    [root@localhost tmp]# if [ -n "$a" ];then echo $a;else echo "a is null";fi    如果是變量一定要加雙引號
    a is null 
  • if grep -q '123' 1.txt; then表示如果1.txt中含有'123'的行時會怎么樣
    [root@localhost tmp]# grep -w 'zabbix' /etc/passwd
    zabbix:x:498:498:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
    [root@localhost tmp]# 
    [root@localhost tmp]# if grep -w 'zabbix' /etc/passwd;then echo "zabbix exist";fi
    zabbix:x:498:498:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
    zabbix exist
    [root@localhost tmp]# if grep -wq 'zabbix' /etc/passwd;then echo "zabbix exist";fi    -q表示僅僅做過濾,不把過濾的內(nèi)容顯示出來
    zabbix exist
  • if [ ! -e file ]; then 表示文件不存在時會怎么樣
  • if (($a&lt;1)); then …等同于 if [ $a -lt 1 ]; then…
  • [ ] 中不能使用<,>,==,!=,>=,<=這樣的符號

8.cace判斷(上)

  • 格式:

    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

    9.cace判斷(下)

10.for循環(huán)

  • 語法:for 變量名 in 條件; do …; done
  • 案例1
    #!/bin/bash
    sum=0
    for i in `seq 1 100`
    do
      sum=$[$sum+$i]
      echo $i
    done
    echo $sum
  • 案例2
    
    文件列表循環(huán)
    #!/bin/bash
    cd /etc/
    for a in `ls /etc/`
    do
      if [ -d $a ]
      then
    ? ??  ls -d $a
      fi
    done
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI