bash帮助转义多个子shell和eval上的引号

时间:2015-07-29 13:25:58

标签: linux bash shell paste

我有一个脚本需要并排打印多个主机的文件内容。我认为最好的方法是使用paste和bash process substitution

它适用于单个bash衬垫:

paste <(/usr/bin/ssh module-1 'printf %-30s\\n "module-1" && find /proc/icp_dh895xcc_dev*/qat -print -exec awk -F'"'[][ ]' '"'/AE/{if ($5%2==0) printf "%-9s AE %-2d: %-7d\n",$3,$5,$(NF-1)}'"' {} \;") \
<(/usr/bin/ssh module-2 'printf %-30s\\n "module-2" && find /proc/icp_dh895xcc_dev*/qat -print -exec awk -F'"'[][ ]' '"'/AE/{if ($5%2==0) printf "%-9s AE %-2d: %-7d\n",$3,$5,$(NF-1)}'"' {} \;") \
<(/usr/bin/ssh module-3 'printf %-30s\\n "module-3" && find /proc/icp_dh895xcc_dev*/qat -print -exec awk -F'"'[][ ]' '"'/AE/{if ($5%2==0) printf "%-9s AE %-2d: %-7d\n",$3,$5,$(NF-1)}'"' {} \;") \
<(/usr/bin/ssh module-4 'printf %-30s\\n "module-4" && find /proc/icp_dh895xcc_dev*/qat -print -exec awk -F'"'[][ ]' '"'/AE/{if ($5%2==0) printf "%-9s AE %-2d: %-7d\n",$3,$5,$(NF-1)}'"' {} \;")

当我尝试通过为每个主机构建一个命令行并通过eval运行它来从中创建脚本时,引号和反斜杠的转义会中断。

这是我使用的简单脚本逻辑:

#!/bin/bash

CMD="paste "
MODULES=( $(awk '/module/ {print $NF}' /etc/hosts) )
for ii in ${!MODULES[@]}
do
        CMD=''"${CMD}"' <(/usr/bin/ssh '"${MODULES[$ii]}"' ''printf "%-30s\\\\n" "'"${XIV_MODULES[${ii}]}"'" && find /proc/icp_dh895xcc_dev*/qat -print -exec awk -F'"'[][ ]' '"'/AE/{if ($5%2==0) printf "%-9s AE %-2d: %-7d\n",$3,$5,$(NF-1)}'"' {} \; '")"

done

echo '~~~~~~~~~~~~~~~~~~~~~~~~~~ echo "${CMD}" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo "${CMD}"
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~ eval "${CMD}" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
eval "${CMD}"
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

我已经尝试了几乎任何组合,让它在过去的几天里没有运气。请帮助:)

我尝试解析的文件如下所示:

# cat /proc/icp_dh895xcc_dev0/qat
+--------------------------------------------------+
| Statistics for Qat Instance 0                    |
+--------------------------------------------------+
| Firmware Requests[AE 0]:                  637107 |
| Firmware Responses[AE 0]:                 637107 |
+--------------------------------------------------+
| Firmware Requests[AE 1]:                       0 |
| Firmware Responses[AE 1]:                      0 |
+--------------------------------------------------+
| Firmware Requests[AE 2]:                       0 |
| Firmware Responses[AE 2]:                      0 |
+--------------------------------------------------+
| Firmware Requests[AE 3]:                       0 |
| Firmware Responses[AE 3]:                      0 |
+--------------------------------------------------+
| Firmware Requests[AE 4]:                       0 |
| Firmware Responses[AE 4]:                      0 |
+--------------------------------------------------+
| Firmware Requests[AE 5]:                       0 |
| Firmware Responses[AE 5]:                      0 |
+--------------------------------------------------+
| Firmware Requests[AE 6]:                  675167 |
| Firmware Responses[AE 6]:                 675167 |
+--------------------------------------------------+
| Firmware Requests[AE 7]:                       0 |
| Firmware Responses[AE 7]:                      0 |
+--------------------------------------------------+
| Firmware Requests[AE 8]:                       0 |
| Firmware Responses[AE 8]:                      0 |
+--------------------------------------------------+
| Firmware Requests[AE 9]:                       0 |
| Firmware Responses[AE 9]:                      0 |
+--------------------------------------------------+
| Firmware Requests[AE 10]:                       0 |
| Firmware Responses[AE 10]:                      0 |
+--------------------------------------------------+
| Firmware Requests[AE 11]:                       0 |
| Firmware Responses[AE 11]:                      0 |
+--------------------------------------------------+

非常感谢!

0 个答案:

没有答案