【发布时间】: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