如何在函数调用下的 if-else 条件下用一些静态值填充缺失的列
How to fill the missing columns with some static values in if-else condition under a function call
如何在缺失的列中添加一些值,正如你们看到的,下面的代码是有效的,但在 else
条件下它没有填充列值。
我需要做什么才能将 somevalues
放在缺失的区域。特别是我想填写 nologin?
.
#!/bin/bash
###########
printf "\n"
marker=$(printf "%0.s-" {1..73})
printf "|$marker|\n"
printf "| %-15s | %-15s | %-35s |\n" "Hostname" "RedHat Vesrion" "Perl Version"
printf "|$marker|\n"
remote_connect() {
target_host=
remote_data=($(
ssh -i /home/nxf59093/.ssh/ssh_prod "root@${target_host}" -o StrictHostKeyChecking=no -o PasswordAuthentication=no "
rhelInfo=$(cat /etc/redhat-release | awk 'END{print $7}')
perlInfo=$(rpm -qa | grep -i mod_perl)
echo $rhelInfo $perlInfo
"))
rhelInfo=${remote_data[0]}
perlInfo=${remote_data[1]}
if [[ $? -eq 0 ]]
then
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "$rhelInfo" "$perlInfo"
else
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "?" "?"
fi
} 2>/dev/null
export -f remote_connect
< CVE-2011-2767-hostsList.txt xargs -P15 -n1 -d'\n' bash -c 'remote_connect "$@"' --
printf "|$marker|\n"
结果:
|---------------------------------------------------------------|
|Hostname | RedHat Vesrion | Perl Version |
|---------------------------------------------------------------|
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1243 | 6.9 | |
|fsx1244 | | |
|fsx1245 | | |
|---------------------------------------------------------------|
您应该使用默认值查看 PARAMETER Expansion of Bash。例如,在 bash wiki 页面中提供,对于下面的示例..
${PARAMETER:-WORD}
${PARAMETER-WORD}
其中,如果参数PARAMETER
未设置(从未定义)或null(空),则此扩展为WORD
,否则扩展为PARAMETER
的值,就好像它只是 ${PARAMETER}
。如果省略 :
(冒号),如第二种形式所示,默认值仅在参数为 unset 时使用,而不是在参数为空时使用。
查看您的代码,考虑到上述情况,您下面的代码行只需要稍作修改,就可以完成工作。
if [[ $? -eq 0 ]];then
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "$rhelInfo" "$perlInfo"
else
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "${rhelInfo:-?}" "${perlInfo:-?}"
fi
你会得到如下图:
|---------------------------------------------------------------|
|Hostname | RedHat Vesrion | Perl Version |
|---------------------------------------------------------------|
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1243 | 6.9 | |
|fsx1244 | ? | ? |
|fsx1245 | ? | ? |
|---------------------------------------------------------------|
希望这会有所帮助。
如何在缺失的列中添加一些值,正如你们看到的,下面的代码是有效的,但在 else
条件下它没有填充列值。
我需要做什么才能将 somevalues
放在缺失的区域。特别是我想填写 nologin?
.
#!/bin/bash
###########
printf "\n"
marker=$(printf "%0.s-" {1..73})
printf "|$marker|\n"
printf "| %-15s | %-15s | %-35s |\n" "Hostname" "RedHat Vesrion" "Perl Version"
printf "|$marker|\n"
remote_connect() {
target_host=
remote_data=($(
ssh -i /home/nxf59093/.ssh/ssh_prod "root@${target_host}" -o StrictHostKeyChecking=no -o PasswordAuthentication=no "
rhelInfo=$(cat /etc/redhat-release | awk 'END{print $7}')
perlInfo=$(rpm -qa | grep -i mod_perl)
echo $rhelInfo $perlInfo
"))
rhelInfo=${remote_data[0]}
perlInfo=${remote_data[1]}
if [[ $? -eq 0 ]]
then
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "$rhelInfo" "$perlInfo"
else
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "?" "?"
fi
} 2>/dev/null
export -f remote_connect
< CVE-2011-2767-hostsList.txt xargs -P15 -n1 -d'\n' bash -c 'remote_connect "$@"' --
printf "|$marker|\n"
结果:
|---------------------------------------------------------------|
|Hostname | RedHat Vesrion | Perl Version |
|---------------------------------------------------------------|
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1243 | 6.9 | |
|fsx1244 | | |
|fsx1245 | | |
|---------------------------------------------------------------|
您应该使用默认值查看 PARAMETER Expansion of Bash。例如,在 bash wiki 页面中提供,对于下面的示例..
${PARAMETER:-WORD}
${PARAMETER-WORD}
其中,如果参数PARAMETER
未设置(从未定义)或null(空),则此扩展为WORD
,否则扩展为PARAMETER
的值,就好像它只是 ${PARAMETER}
。如果省略 :
(冒号),如第二种形式所示,默认值仅在参数为 unset 时使用,而不是在参数为空时使用。
查看您的代码,考虑到上述情况,您下面的代码行只需要稍作修改,就可以完成工作。
if [[ $? -eq 0 ]];then
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "$rhelInfo" "$perlInfo"
else
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "${rhelInfo:-?}" "${perlInfo:-?}"
fi
你会得到如下图:
|---------------------------------------------------------------|
|Hostname | RedHat Vesrion | Perl Version |
|---------------------------------------------------------------|
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1243 | 6.9 | |
|fsx1244 | ? | ? |
|fsx1245 | ? | ? |
|---------------------------------------------------------------|
希望这会有所帮助。