Home > Back-end >  How to fill the missing columns with some static values in if-else condition under a function call
How to fill the missing columns with some static values in if-else condition under a function call

Time:04-16

How to put some values in the missing columns, as you guys see the below code is working but in the in else condition it not filling the column values.

What i need to do to put the somevalues in missing areas. Specially i want to fill like 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"

Result:

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

CodePudding user response:

You should look at PARAMETER Expansion of Bash by using a default value. For an example, provide in the bash wiki page, for an example below ..

${PARAMETER:-WORD}

${PARAMETER-WORD}

Where, If the parameter PARAMETER is unset (never was defined) or null (empty), this one expands to WORD, otherwise it expands to the value of PARAMETER, as if it just was ${PARAMETER}. If you omit the : (colon), like shown in the second form, the default value is only used when the parameter was unset, not when it was empty.

Looking at your code, taking the above in consideration, your below line of code just need minor correction and that should do the Job for you.

   if [[ $? -eq 0 ]];then
       printf "| %-15s | %-15s | %-35s |\n" "$target_host" "$rhelInfo" "$perlInfo"
   else
       printf "| %-15s | %-15s | %-35s |\n" "$target_host" "${rhelInfo:-?}" "${perlInfo:-?}"
   fi

You will get like below:

|---------------------------------------------------------------|
|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     | ?              | ?                               |
|---------------------------------------------------------------|

hope this will be usefull.

  • Related