我正在尝试创建一个脚本,该脚本将使用多个相互调用的函数输出系统信息。有人能告诉我管道命令的I / O处理方式有什么问题吗?
#!/bin/bash
function lyellow() {
lyellow="$1"
echo -e -n "\033[0;33m$lyellow"
echo -e -n '\033[0m \n'
}
function red() {
RED="$1"
echo -e -n "\033[0;31m$RED"
echo -e -n '\033[0m \n'
}
function lcyan() {
LCYAN="$1"
echo -e -n "\033[0;36m$LCYAN"
echo -e -n '\033[0m \n'
}
function lgreen() {
LGREEN="$1"
echo -e -n "\033[1;32m$LGREEN"
echo -e -n '\033[0m \n'
}
field ()
{
HEADER="$1"
SUB1="$2"
COM1="$3"
SUB2="$4"
COM2="$5"
echo -e "$(red "$(echo -e "### $HEADER ###")")"
echo -e "$(lyellow "$(echo -e "$SUB1")")\n$(lcyan "$(echo -e "$($COM1)")")"
echo -e "$(lyellow "$(echo -e "$SUB2")")\n$(lcyan "$(echo -e "$($COM2)")")"
}
#set -x pipefail
SEP=$(seq -s= 40|tr -d '[:digit:]')
echo $SEP
echo -e "$(lgreen "$(hostname -f) :: $(hostname -i)")"
echo $SEP
#OS
field "Operating System" \
"Kernel:" "/bin/uname -srp" \
"Release:" "cat /etc/redhat-release"
echo $SEP
#DISK
field "Storage Devices" \
"Mounted Devices:" "mount|column -t" \
"Disk Free:" "df -kh|column -t"
echo $SEP
#Example
lcyan "$(echo -e "$(df -kh | column -t)")"
exit 0
“#OS”“field”调用的输出有效。但是“#DISK”调用不喜欢管道“column -t”。在“#Example”下,color函数调用文字管道“column -t”。这是输出的样子:
[root@CLFT1Q ~]# sh sysinfo.sh
=======================================
CLFT1Q.local :: 10.9.19.70
=======================================
### Operating System ###
Kernel:
Linux 2.6.18-348.3.1.el5 i686
Release:
Red Hat Enterprise Linux Server release 5.9 (Tikanga)
=======================================
### Storage Devices ###
sysinfo.sh: line 36: /bin/mount|column: No such file or directory
Mounted Devices:
df: invalid option -- |
Try `df --help' for more information.
Disk Free:
=======================================
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vgsystem-lv_root
3.9G 3.3G 421M 89% /
/dev/mapper/vgsystem-lv_var
4.9G 2.3G 2.4G 49% /var
/dev/mapper/vgsystem-ora
3.0G 1008M 1.9G 36% /ora
/dev/sda1 99M 25M 69M 27% /boot
tmpfs 1014M 0 1014M 0% /dev/shm
clnsa05:/vol/ftpnfsqa1/ftp
29G 25G 4.2G 86% /ftp
答案 0 :(得分:1)
将"$($COM1)"
更改为"$(eval "$COM1")"
,将$COM2
更改为eval
。变量扩展仅针对字拆分和通配符扩展进行扫描,而不是针对管道等命令元字符进行扫描。您需要使用{{1}}作为命令行递归处理它。