如何在缺失的列中添加一些值,正如你們看到的,下面的代碼正在作業,但在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=$1
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 | | |
|---------------------------------------------------------------|
uj5u.com熱心網友回復:
您應該使用默認值查看Bash 的引數擴展。例如,請在 bash wiki 頁面中提供,例如以下 ..
${PARAMETER:-WORD}
${PARAMETER-WORD}
其中,如果引數PARAMETER未設定(從未定義)或 null(空),則此引數擴展為WORD,否則擴展為 的值PARAMETER,就好像它只是 一樣${PARAMETER}。如果省略:(冒號),如第二種形式所示,默認值僅在引數未設定時使用,而不是在引數為空時使用。
查看您的代碼,考慮到上述情況,您下面的代碼行只需要稍作修正,這應該可以為您完成作業。
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 | ? | ? |
|---------------------------------------------------------------|
希望這會有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460414.html
上一篇:簡寫陣列if陳述句
