首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在脚本中添加暂停或跳过暂停

如何在脚本中添加暂停或跳过暂停
EN

Stack Overflow用户
提问于 2015-01-17 01:27:49
回答 2查看 228关注 0票数 0

你好,我有一个脚本,我正在为工作创建,并被要求添加一个可伸缩性的功能。目前,我在每次请求后都会暂停,但我不知道如何添加跳过暂停功能。我留下了我的代码,以显示我正在做的事情的基本功能,以提供进一步的信息。在顶部,我主要有函数,在底部是函数、动作和暂停。

代码语言:javascript
复制
    #!/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
EN

回答 2

Stack Overflow用户

发布于 2015-01-17 13:11:30

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

代码语言:javascript
复制
function shouldSkip {
  read -p "$*" line
  test "$line" = 'n' -o "$line" = 's'
  return $?
}

要使用以下命令:

代码语言:javascript
复制
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "service ipmi start". ..'
then
  service ipmi start
fi
票数 1
EN

Stack Overflow用户

发布于 2015-01-20 06:25:00

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

代码语言:javascript
复制
#!/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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27989466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档