0

I am trying to make a script that will output system information using several functions that call each other. Can someone tell me what's wrong with how the piped commands' I/O is handled?

#!/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

The output from the "#OS" "field" call works. But the "#DISK" call doesn't like the pipes to "column -t". Under "#Example" the color function calls a literal piped "column -t" fine. Here is what the output looks like:

[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
1

1 Answer 1

1

Change "$($COM1)" to "$(eval "$COM1")", and similarly for $COM2. Variable expansions are only scanned for word splitting and wildcard expansion, not for command metacharacters like pipelines. You need to use eval to process it recursively as a command line.

Sign up to request clarification or add additional context in comments.

1 Comment

You are the man. I found something about eval but wasn't sure where to try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.