在 shell 脚本运行时显示进度

Showing progress as shell script runs

我想显示一些内容,表明脚本仍在从设备收集信息。无论是散列标记还是句点。正如您在下面看到的,当它开始遍历设备时,它会回显其开始时间。在这里我想展示一些东西表明它仍然 运行.

#!/bin/sh
#Create nbar directory in the /opt
#Change diretory to /opt/nbar
#Create file to be read from it needs to be in the following format.
#The only white space in the file that is allowed is between the columns
#column 1 the IP address
#column 2 the SNMP community string
#column 3 the state
#column 4 the city
#Eample: 10.0.0.1 public Cisco RTP
#
#Get file to read
echo "What file should be used?"
read input_variable
echo "Ok using $input_variable"
if [ -e $input_variable ]
then
while read field1 field2 field3 field4
do
curdate=`date "+%Y%m%d%H%M"`
fname=$field3-$field4-$field1-$curdate.nbar
#Tell me what device you started
echo Now querying $field3-$field4 started at `date "+%T"`
#Create file
echo $field3-$field4-$curdate $'\n' > $fname
#Confirm System Name
snmpwalk -v2c -c $field2 $field1 1.3.6.1.2.1.1.5  >> $fname
#Insert blank line
echo >> $fname
#Get IOS version
snmpget -v2c -c $field2 $field1 1.3.6.1.2.1.1.1.0  >> $fname 
#Insert blank line
echo >> $fname
#Get system uptime
snmpwalk -v2c -c $field2 $field1 1.3.6.1.2.1.1.3  >> $fname
#Insert blank line
echo >> $fname
#Get nbar protocol table  
snmptable -v2c -c $field2 $field1 -m ALL 1.3.6.1.4.1.9.9.244.1.2.1 >> $fname
#close target file
done </opt/nbar/$input_variable
#Create zip file
for file in *.nbar
do
zip "nbar$curdate.zip" "$file"
done
#Cleanup individual files
rm -rf /opt/nbar/*.nbar
else
echo No such file.
fi

除了我的问题,这是一个用于轮询 Cisco 设备的功能脚本。如果您了解 SNMP MIB 和 OID,则可以根据需要更改其中的值。享受! :)

这将每秒在屏幕上打印一个点。在第一个 snmpwalk 命令之前添加这些行。

while true; do echo -n . ; sleep 1 ; done &
pid=$!

#do time consuming tasks here

然后要停止打印点,请在最后一个 snmpwalk 命令之后添加此行。

kill $pid