2013-05-10 94 views
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」「field」調用的輸出有效。但是「#DISK」呼叫不喜歡管道到「列-t」。在「#Example」下,顏色函數調用一個文字管道「column -t」。下面是輸出的樣子:

[[email protected] ~]# 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 
+1

請參閱[BashFAQ/050](http://mywiki.wooledge.org/BashFAQ/050)。 – 2013-05-10 22:15:40

回答

1

變化"$($COM1)""$(eval "$COM1")",同樣爲$COM2。變量擴展僅針對單詞拆分和通配符擴展進行掃描,而不是對管道等命令元字符進行掃描。您需要使用eval作爲命令行遞歸處理它。

+0

你是男人。我發現了一些關於評估,但不知道在哪裏嘗試。 – 2013-05-10 22:16:03