gNU.be
gNU.be

Reputation: 1937

Format output of multiple bash commands into columns

What I want to do is simple enough, but I cannot get it done without several commands. This is what it looks like so far. I'd love to be able to do this in one sloppy looking line that just passes all the commands at once.

# cat /sys/class/scsi_host/host*/device/fc_host:host*/port_name >port
# cat /sys/class/scsi_host/host*/device/fc_host:host*/speed >speed
# cat /sys/class/scsi_host/host*/device/fc_host:host*/port_state >state
# paste -d ' ' port speed state
0x218000e01a0002d2 8 Gbit Online
0x218100e01a2002d2 8 Gbit Online

I've tried to do this with sub shells, variables, etc. The format ends up misaligned, or worse.

# echo "$port_name" "$speed" "$state"
0x218000e01a0002d2
0x218100e01a2002d2 8 Gbit
8 Gbit Online
Online

# paste -d ' ' "$(cat /sys/class/scsi_host/host*/device/fc_host:host*/port_name)" "$(cat /sys/class/scsi_host/host*/device/fc_host:host*/speed)"
paste: 0x218000e01a0002d2
0x218100e01a2002d2: No such file or directory

Upvotes: 0

Views: 415

Answers (2)

demon_86_rm
demon_86_rm

Reputation: 1

What about this?

for i in /sys/class/fc_host/host*; do (cd $i; echo -e "$(cat port_name)\t$(cat port_state)\t$(cat speed)"); done

Upvotes: -1

chepner
chepner

Reputation: 530960

Instead of command substitution, try process substitution:

paste -d ' ' <(cat /sys/class/scsi_host/host*/device/fc_host:host*/port_name) \
             <(cat /sys/class/scsi_host/host*/device/fc_host:host*/speed) \
             <(cat /sys/class/scsi_host/host*/device/fc_host:host*/port_state)

Upvotes: 2

Related Questions