【问题标题】:how to add pause or skip pause in script如何在脚本中添加暂停或跳过暂停
【发布时间】:2015-03-15 09:11:24
【问题描述】:

您好,我正在为工作创建一个脚本,并被要求添加一项可扩展性功能。我目前在每次请求后都会暂停,但我不知道如何添加跳过暂停功能。我留下了我的代码来展示我正在做什么以提供更多信息的基本功能。顶部我主要有功能,底部是功能动作和暂停。

    #!/bin/bash
    # Basic Linux host info script
    # Matthew Morcaldi 2015
    # TODO: Getops & HP Support

    # Config ------------------------------------------------
outfile="linux_info.txt"
#--------------------------------------------------------

#### uncomment (set -x) for debug 
# set -x 


checkroot() { 
    if [ $UID -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
}

header() { 
    echo '' | tee -a $outfile
    echo '----------------------' | tee -a $outfile
    echo "[*] $title" | tee -a $outfile
    echo '----------------------' | tee -a $outfile
}

function pause(){
   read -p "$*"
}


display_os() { 
    title="System"
    header $title
    dmidecode -t 1 | grep -i 'serial' | sed 's/^ *//' | tee  $outfile
    cat /etc/*-release | tee -a $outfile
    dmidecode -t system -q | egrep -i 'Manufacturer: |Product|UUID' | \
    sed 's/^ *//' | tee -a $outfile
    ipmitool bmc info | egrep -i 'Firmware revision' | tee  -a $outfile
    dmidecode -t bios -q | egrep -i 'version|vendor' | \
    sed 's/^ *//' | tee -a $outfile
}

display_network_oob() { 
    title="Drac-Info"
    header $title
    ipmitool lan print | egrep -i 'IP Address|MAC Address|Default Gateway IP|Subnet Mask' | tee -a $outfile
}

display_networking() {
    title="Networking"
    header $title
    ifconfig -a | tee -a $outfile
}

display_bonding() { 
    if [ -f /proc/net/bonding/bond0 ] ; then
        title="Bonding"
        header $title
        cat /proc/net/bonding/bond0 | sed 's/^ *//' | tee -a $outfile
    fi
}

display_switch_info() { 
    if [ -f /usr/sbin/lldpctl ] ; then
        title="Switch-Port-Information"
        header $title
        lldpctl | egrep '(Interface|VLAN|PortDescr|SysName)' | \
        sed 's/^ *//' | tee -a $outfile
        else
            echo "Switch file LLDPCTL does not exist skipping"
    fi
}

show_disks_dell() {
    if [ -f /opt/MegaRAID/MegaCli/MegaCli64 ] ; then 
        title="Dell-Raid"
        header $title
        /opt/MegaRAID/MegaCli/MegaCli64 -PDList -aAll |  \
        egrep -i 'count|^Device Id: |firmware state' |  \
        grep -v 'Count: 0' | \
        perl -p -e 's/Firmware state: (.*)$/Firmware state: $1\n/' | tee -a $outfile
        else
            echo "skipping Dell disk check"
     fi
}

show_battery_dell() { 
    if [ -f /opt/MegaRAID/MegaCli/MegaCli64 ]; then
        title="Dell-Battery"
        header $title
        /opt/MegaRAID/MegaCli/MegaCli64 -AdpBbuCmd -GetBbuStatus -a0 | \
        egrep -i 'isSOHGood|Charger Status|Capacity|Relative|Charging' | \
        sed 's/^ *//' | tee -a $outfile
        else
            echo "skipping Dell battery check"
    fi
}

show_disks_hp() { 
    if [ -f /usr/sbin/hpacucli ]; then
        title="HP-Raid"
        header $title
        hpacucli ctrl all show config detail | \
        sed 's/^ *//' | tee -a $outfile 
    else
        echo "skipping HP disk check"
    fi
}

show_battery_hp() { 
    if [ -f /usr/sbin/hpacucli ]; then
        title="HP-Battery"
        header $title
        hpacucli ctrl all show status | \
        sed 's/^ *//' | tee -a $outfile
    else
        echo "skipping HP battery check"
    fi
}

display_dimms() {
    title="Memory"
    header $title
    cat /proc/meminfo|grep MemTotal >> $outfile
    free -g | tee -a $outfile
}

sel_list() {
    title="SEL"
    header $title
    ipmitool sel elist | tee -a $outfile
}
# Where the magic happens -------------------------------------
# TODO: Add Optarg Support
# http://wiki.bash-hackers.org/howto/getopts_tutorial
# comment out below to skip 




pause 'Press [Enter] key to continue. ..'
service ipmi start 
pause 'Press [Enter] key to continue. ..'
display_os
pause 'Press [Enter] key to continue. ..'
display_network_oob
pause 'Press [Enter] key to continue. ..'
display_networking
pause 'Press [Enter] key to continue. ..'
display_bonding
pause 'Press [Enter] key to continue. ..'
display_dimms
pause 'Press [Enter] key to continue. ..'
display_switch_info
pause 'Press [Enter] key to continue. ..'
sel_list
pause 'Press [Enter] key to continue. ..'
show_disks_hp
pause 'Press [Enter] key to continue. ..'
show_battery_hp
pause 'Press [Enter] key to continue. ..'
show_disks_dell
pause 'Press [Enter] key to continue. ..'
show_battery_dell
pause 'Press [Enter] key to continue. ..'
service ipmi stop


cat << "EOF"
          _ _,---._
       ,-','       `-.___
      /-;'               `._
     /\/          ._   _,'o \
    ( /\       _,--'\,','"`. )
     |\      ,'o     \'    //\
     |      \        /   ,--'""`-.
     :       \_    _/ ,-'         `-._
      \        `--'  /                )
       `.  \`._    ,'     ________,','
         .--`     ,'  ,--` __\___,;'
          \`.,-- ,' ,`_)--'  /`.,'
           \( ;  | | )      (`-/
             `--'| |)       |-/         
               | | |        | |
               | | |,.,-.   | |_
               | `./ /   )---`  )
              _|  /    ,',   ,-'
             ,'|_(    /-<._,' |--,
             |    `--'---.     \/ \
             |          / \    /\  \
           ,-^---._     |  \  /  \  \
        ,-'        \----'   \/    \--`.
       /            \              \   \
EOF

【问题讨论】:

  • pause 函数中添加 if 语句以选择性地跳过读取?

标签: linux bash shell networking server


【解决方案1】:

更改您的 pause 函数以返回 0 (true) 以跳过,或 1 (false) 以继续,并将其重命名为更具描述性的名称:

function shouldSkip {
  read -p "$*" line
  test "$line" = 'n' -o "$line" = 's'
  return $?
}

使用方法:

if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "service ipmi start". ..'
then
  service ipmi start
fi

【讨论】:

    【解决方案2】:

    特别感谢丹尼·达格拉。发布最终脚本以供全球使用。

    #!/bin/bash
    # Linux host info script [DONUTS]
    # Matthew Morcaldi 2015
    # Version 0.1.2 last update (1/19/2015)
    # w00t be in r00t
    
    # Config ------------------------------------------------
    outfile="linux_info.txt"
    #--------------------------------------------------------
    
    #### uncomment (set -x) for debug 
    # set -x 
    
    
    checkroot() { 
        if [ $UID -ne 0 ] ; then
            echo "User has insufficient privilege."
            exit 4
        fi
    }
    
    
    function shouldSkip {
      read -p "$*" line
      test "$line" = 'n' -o "$line" = 's'
      return $?
    }
    
    
    header() { 
        echo '' | tee -a $outfile
        echo '----------------------' | tee -a $outfile
        echo "[*] $title" | tee -a $outfile
        echo '----------------------' | tee -a $outfile
    }
    
    function pause(){
       read -p "$*"
    }
    
    
    display_os() { 
        title="System"
        header $title
        dmidecode -t 1 | grep -i 'serial' | sed 's/^ *//' | tee  $outfile
        cat /etc/*-release | tee -a $outfile
        dmidecode -t system -q | egrep -i 'Manufacturer: |Product|UUID' | \
        sed 's/^ *//' | tee -a $outfile
        /usr/bin/ipmitool bmc info | egrep -i 'Firmware revision' | tee  -a $outfile
        dmidecode -t bios -q | egrep -i 'version|vendor' | \
        sed 's/^ *//' | tee -a $outfile
    }
    
    display_network_oob() { 
        title="Drac-Info"
        header $title
        ipmitool lan print | egrep -i 'IP Address|MAC Address|Default Gateway IP|Subnet Mask' | tee -a $outfile
    }
    
    display_networking() {
        title="Networking"
        header $title
        ifconfig -a | tee -a $outfile
    }
    
    display_bonding() { 
        if [ -f /proc/net/bonding/bond0 ] ; then
            title="Bonding"
            header $title
            cat /proc/net/bonding/bond0 | sed 's/^ *//' | tee -a $outfile
        fi
    }
    
    display_switch_info() { 
        if [ -f /usr/sbin/lldpctl ] ; then
            title="Switch-Port-Information"
            header $title
            lldpctl | egrep '(Interface|VLAN|PortDescr|SysName)' | \
            sed 's/^ *//' | tee -a $outfile
            else
                echo "Switch file LLDPCTL does not exist skipping"
        fi
    }
    
    show_disks_dell() {
        if [ -f /opt/MegaRAID/MegaCli/MegaCli64 ] ; then 
            title="Dell-Raid"
            header $title
            /opt/MegaRAID/MegaCli/MegaCli64 -PDList -aAll |  \
            egrep -i 'count|^Device Id: |firmware state' |  \
            grep -v 'Count: 0' | \
            perl -p -e 's/Firmware state: (.*)$/Firmware state: $1\n/' | tee -a $outfile
            /opt/MegaRAID/MegaCli/MegaCli64 -LDInfo -Lall -aALL | \
            egrep -i 'virtual|cache|state|raid level|name|size|number' | tee -a $outfile
            else
                echo "skipping Dell disk check"
         fi
    }
    
    show_battery_dell() { 
        if [ -f /opt/MegaRAID/MegaCli/MegaCli64 ]; then
            title="Dell-Battery"
            header $title
            /opt/MegaRAID/MegaCli/MegaCli64 -AdpBbuCmd -GetBbuStatus -a0 | \
            egrep -i 'isSOHGood|Charger Status|Capacity|Relative|Charging' | \
            sed 's/^ *//' | tee -a $outfile
            else
                echo "skipping Dell battery check"
        fi
    }
    
    show_disks_hp() { 
        if [ -f /usr/sbin/hpacucli ]; then
            title="HP-Raid"
            header $title
            hpacucli ctrl all show config detail | \
            sed 's/^ *//' | tee -a $outfile 
        else
            echo "skipping HP disk check"
        fi
    }
    
    show_battery_hp() { 
        if [ -f /usr/sbin/hpacucli ]; then
            title="HP-Battery"
            header $title
            hpacucli ctrl all show status | \
            sed 's/^ *//' | tee -a $outfile
        else
            echo "skipping HP battery check"
        fi
    }
    
    display_dimms() {
            title="Memory"
            header $title
            cat /proc/meminfo|grep MemTotal | tee -a $outfile
            free -g | tee -a $outfile
            dmidecode -t Memory Device -q |egrep -i 'Size:|Type: D|Locator: D' | tee -a $outfile
    
    
    }
    
    sel_list() {
        title="SEL"
        header $title
        ipmitool sel elist | tee -a $outfile
    }
    
    
    
    
    # Where the magic happens -------------------------------------
    # TODO: Add Optarg Support
    # http://wiki.bash-hackers.org/howto/getopts_tutorial
    
    
    
    
    checkroot
    
    pause 'Press [Enter] key to continue into DONUTS Version: 0.1.1. ..'
    service ipmi start
    
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display OS".   '
    then
      display_os
    fi
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Network OOB".   '
    then
      display_network_oob
    fi
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Network".   '
    then
      display_networking
    fi
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Bonding".   '
    then
      display_bonding
    fi
    
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Switch Port Information".   '
    then
      display_switch_info
    fi
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Memory".   '
    then
      display_dimms
    fi
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "System Event List".   '
    then
      sel_list
    fi
    
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "HP RAID".   '
    then
      show_disks_hp
    fi
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "HP RAID Battery".   '
    then
      show_battery_hp
    fi
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Dell RAID".   '
    then
      show_disks_dell
    fi
    
    if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Dell RAID Battery".   '
    then
      show_battery_dell
    fi
    
    pause 'Press [Enter] key to continue. ..'
    service ipmi stop
    
    
    cat << "EOF"
              _ _,---._
           ,-','       `-.___
          /-;'               `._
         /\/          ._   _,'o \
        ( /\       _,--'\,','"`. )
         |\      ,'o     \'    //\
         |      \        /   ,--'""`-.
         :       \_    _/ ,-'         `-._
          \        `--'  /                )
           `.  \`._    ,'     ________,','
             .--`     ,'  ,--` __\___,;'
              \`.,-- ,' ,`_)--'  /`.,'
               \( ;  | | )      (`-/
                 `--'| |)       |-/         DONUTS Version: 0.1.1
                   | | |        | |
                   | | |,.,-.   | |_
                   | `./ /   )---`  )
                  _|  /    ,',   ,-'
                 ,'|_(    /-<._,' |--,
                 |    `--'---.     \/ \
                 |          / \    /\  \
               ,-^---._     |  \  /  \  \
            ,-'        \----'   \/    \--`.
           /            \              \   \
    EOF
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多