mardi 28 août 2018

ESXi shell scripting getting rid of a message

I'm working on a small VMware ESXi project (personal project, not for any company). Im trying to build a html file that contains a table with some information from ESXi OS, like time/date, OS version, patch number, etc. However there are some commands that give no output and then my table has an empty box (cell). What i am trying to do..and terribly failing at ...is that i am trying to put a simple if-else-fi statement in the shell script that would check if the output is empty or not. Here is the command i use the check if there is an output to the command or not:

if [ $(esxcli hardware ipmi bmc get |grep -i gate |awk '{print $2}') != " "  ]; then echo "Not Empty!"; else echo "Empty!"; fi

The problem with this is, that while it gives the correct result, it also prints out the following:

sh:  : unknown operand
Empty!

Yes, the result is supposed to be "Empty!", but i cant get rid of the "sh: : unknown operand" message. It seems it does not like that the != operand is not close to ")".

If however i put the "!=" operand close to the ")", like this:

if [ $(esxcli hardware ipmi bmc get |grep -i gate |awk '{print $2}')!=" "  ]; then echo "Not Empty!"; else echo "Empty!"; fi

..it no longer gives the "sh: : unknown operand" message but it gives the wrong result "Not Empty!". If however i insert a command in the if-else-fi statement that gives an output, for example:

if [ $(esxcli system time get) != " "  ]; then echo "Not Empty!"; else echo "Empty!"; fi

..it give no "sh: : unknown operand" messages and gives the correct result as "Not Empty!"

I have tried in the following ways but it gave the same "sh: : unknown operand" message:

if [[ $(esxcli hardware ipmi bmc get |grep -i gate |awk '{print $2}') != " "  ]]; then echo "Not Empty!"; else echo "Empty!"; fi

if [ -n $(esxcli hardware ipmi bmc get |grep -i gate |awk '{print $2}') ]; then echo "Not Empty!"; else echo "Empty!"; fi

if [ -z $(esxcli hardware ipmi bmc get |grep -i gate |awk '{print $2}') ]; then echo "Not Empty!"; else echo "Empty!"; fi

if "$(esxcli hardware ipmi bmc get |grep -i gate |awk '{print $2}')" == " " ; then echo "Not Empty!"; else echo "Empty!"; fi

if $(esxcli hardware ipmi bmc get |grep -i gate |awk '{print $2}')==" "; then echo "Not Empty!"; else echo "Empty!"; fi

How can i get rid of that message...What else can i do?

Aucun commentaire:

Enregistrer un commentaire