3

I'm creating a script to check the requirements about VM in one of my customer. The first part of the script is check if the OS is virtual or not. To achieve this goal I create the following script. The problem is that even the system is virtual it is not working, I mean the test if not working!!! Does anyone can help and say if I did something wrong (I'm beginner in shell script and probably that is something wrong).

CHECK_SYSTEM=`dmidecode | grep -i "Product Name: VMware Virtual Platform"`
if [ "$CHECK_SYSTEM" = "Product Name: VMware Virtual Platform" ] ; then
    printf "%s\n" "The system is Virtual"
else
    printf "%s\n" "The system is not Virtual"
    exit 192
fi

2 Answers 2

2

Use echo to inspect the $CHECK_SYSTEM string returned by your command, it is not what you think it is. You will need to further process it to remove some whitespace.

You may find the virt-what command more useful if it is installed on the system e.g.

# virt-what 
vmware

or

# virt-what
kvm

virt-what is a shell script so you can easily add a copy to your installation scripts.

Note also that it is generally preferred that you use $(...) rather than backticks these days.

1

@Iain is correct that this script is likely not doing what you need, however in general if you're looking to compare strings you're missing an equalsign, this would probably work better:

if [ "$CHECK_SYSTEM" == "Product Name: VMware Virtual Platform" ] ; then

See this excellent thread for more: https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash-script

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .