操作系统:lubuntu 13.04
我正在运行一个简单的bash脚本来将各种系统信息保存到文件中。我主要只是将命令输出回显到文件中。我运行的命令之一是:
echo "$(nmcli dev list)" >> $CI
我注意到在某些系统上,上面的命令只会返回空白。
如果我直接从CLI运行“nmcli dev list”,我注意到nmcli会获得段错误,因此echo不会将任何内容放入该命令的$ CI文件中。
虽然这里的真正问题是nmcli有一个段错误,但是在“nmcli dev list”的输出中仍然存在有价值的信息,我想保存它直到它出现段错误。
鉴于我在本网站和Google搜索中找到和阅读的内容,由于该命令永远不会因为段错误而完成,因此似乎没有太简单的答案。但是,我希望有人可能有一个解决方法或其他技术的想法,以捕获输出直到segfault。
user1@comp1:/home/user1$ nmcli dev list
GENERAL.DEVICE: eth0
GENERAL.TYPE: 802-3-ethernet
GENERAL.VENDOR: Marvell Technology Group Ltd.
GENERAL.PRODUCT: 88E8040 PCI-E Fast Ethernet Controller
GENERAL.DRIVER: sky2
GENERAL.DRIVER-VERSION: 1.30
GENERAL.FIRMWARE-VERSION:
GENERAL.HWADDR: 00:24:81:5D:F3:F9
GENERAL.STATE: 20 (unavailable)
GENERAL.REASON: 2 (Device is now managed)
GENERAL.UDI: /sys/devices/pci0000:00/0000:00:1c.1/0000:02:00.0/net/eth0
GENERAL.IP-IFACE:
GENERAL.NM-MANAGED: yes
GENERAL.AUTOCONNECT: yes
GENERAL.FIRMWARE-MISSING: no
GENERAL.CONNECTION: not connected
CAPABILITIES.CARRIER-DETECT: yes
CAPABILITIES.SPEED: unknown
Segmentation fault
使用bash -x运行我的脚本的结果:
+ echo ' <NMCLI> '
++ nmcli dev list
+ echo ''
+ echo ' </NMCLI> '
整个脚本:
#!/bin/bash
# Define file to save configuration info to
CI=config-info.txt
# Get hostname
HOSTNAME=$(hostname)
echo " <Hostname>$HOSTNAME</Hostname>" >> $CI
# Get IP
IP=$(ifconfig | grep -m 1 -E '(inet.*Bcast.*Mask)' | awk '{print $2}' | sed s/addr:/""/)
echo " <IP>$IP</IP>" >> $CI
# Get SubnetMask
MASK=$(ifconfig | grep -m 1 -E '(inet.*Bcast.*Mask)' | awk '{print $4}' | sed s/Mask:/""/)
echo " <SubnetMask>$MASK</SubnetMask>" >> $CI
# Get Gateway IP
GATEWAY=$(ip route show | grep -m 1 'default via' | awk '{print $3}')
echo " <Gateway>$GATEWAY</Gateway>" >> $CI
# Get DNS Server IP
DNS=$(nmcli dev list | grep -m 1 DNS | awk {'print $2'})
echo " <DNS>$DNS</DNS>" >> $CI
# Get MAC Address of NIC
MAC=$(ifconfig | grep -m 1 HWaddr | awk '{print $5}')
echo " <MAC>$MAC</MAC>" >> $CI
# Get System Uptime
UPTIME=$(cat /proc/uptime | awk '{print $1}')
echo " <Uptime>$UPTIME</Uptime>" >> $CI
# Get NIC linkspeed
LINKSPEED=$(nmcli dev list | grep -m 1 CAPABILITIES.SPEED | awk '{print $2 " " $3}')
echo " <LinkSpeed>$LINKSPEED</LinkSpeed>" >> $CI
# Get NIC Vendor
# The first awk gets all columns in the line excluding the 1st column.
# The second awk removes the leading space in front of the output of the first awk
NICVENDOR=$(nmcli dev list | grep -m 1 GENERAL.VENDOR | awk '{$1=""; print $0}' | awk '{sub(/^[ \t]+/, "")};1'
)
echo " <NicVendor>$NICVENDOR</NicVendor>" >> $CI
# Get NIC Model
NICMODEL=$(nmcli dev list | grep -m 1 GENERAL.PRODUCT | awk '{$1=""; print $0}' | awk '{sub(/^[ \t]+/, "")};1')
echo " <NicModel>$NICMODEL</NicModel>" >> $CI
# Get NIC Driver Name
NICDRIVER=$(nmcli dev list | grep -m 1 GENERAL.DRIVER | awk '{$1=""; print $0}' | awk '{sub(/^[ \t]+/, "")};1')
echo " <NicDriver>$NICDRIVER</NicDriver>" >> $CI
# Get NIC Driver Version
NICDRIVERVERSION=$(nmcli dev list | grep -m 1 GENERAL.DRIVER-VERSION | awk '{$1=""; print $0}' | awk '{sub(/^[ \t]+/, "")};1')
echo " <NicDriverVersion>$NICDRIVERVERSION</NicDriverVersion>" >> $CI
# Get "ifconfig -a" information
echo " <Ifconfig>" >> $CI
echo "$(ifconfig -a)" >> $CI
echo " </Ifconfig>" >> $CI
# Get "nmcli dev list" information
echo " <NMCLI> " >> $CI
echo "$(nmcli dev list)" >> $CI
echo " </NMCLI> " >> $CI
# Get "nmcli -p dev wifi list" output - This lists all detected Wireless Networks
echo " <Wireless>" >> $CI
echo "$(nmcli -p dev wifi list)" >> $CI
echo " </Wireless>" >> $CI
# Get "nmcli -p con status" output - Shows the status for all known network connections
echo " <ConnectionsStatus>" >> $CI
echo "$(nmcli -p con status)" >> $CI
echo " </ConnectionsStatus>" >> $CI
# Get "nmcli -p con list" output - Lists all connections NetworkManager has
echo " <ConnectionsAll>" >> $CI
echo "$(nmcli -p con list)" >> $CI
echo " </ConnectionsAll>" >> $CI
# Get "route" information
echo " <Route>" >> $CI
echo "\"route\": " >> $CI
echo "$(route)" >> $CI
echo " " >> $CI
echo "\"route -n\": " >> $CI
echo "$(route -n)" >> $CI
echo " " >> $CI
echo "\"route -Cn\": " >> $CI
echo "$(route -Cn)" >> $CI
echo " </Route>" >> $CI
答案 0 :(得分:2)
您是否尝试将stdout和stderr重定向到您的文件。
根据http://www.tldp.org/LDP/abs/html/io-redirection.html上的文档,您可以使用&amp;&gt;将两者重定向到文件或2&gt;&amp; 1将stderr重定向到stdout。
nmcli dev list &>> $CI
你应该需要什么。如果您运行的是旧版本的bash,请尝试以下操作。
nmcli dev list >> $CI 2>&1
注意:如果由于某种原因仍然需要echo
,则需要将重定向放在子shell中,然后在命令或组之后将其重定向并重定向整个输出,如
echo "$(nmcli dev list >> $CI 2>&1)" >> $CI 2>&1
或
{ echo "$(nmcli dev list)"; } >> $CI 2>&1