0

我正在尝试制作一个脚本,该脚本将使用多个相互调用的函数来输出系统信息。有人可以告诉我如何处理管道命令的 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”“字段”调用的输出有效。但是“#DISK”调用不喜欢“column -t”的管道。在“#Example”下,颜色函数调用文字管道“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
4

1 回答 1

1

更改"$($COM1)""$(eval "$COM1")",同样为$COM2。变量扩展只被扫描用于分词和通配符扩展,而不是像管道这样的命令元字符。您需要使用eval它作为命令行递归地处理它。

于 2013-05-10T21:41:35.867 回答