am creating a script to validate what the centos version is running on several VMs, here is the script am using for testing:
#testing script
#commands
cmd1= cat /etc/system-release | grep "7." | wc -l
cmd2= cat /etc/system-release | grep "6." | wc -l
#creating Function
function os_check(){
echo "#######"
echo "The VM $server is running on: "
if [$cmd1 -ne 0]; then
echo "Centos 7"
elif [$cmd2 -ne 0]; then
echo "Centos 6"
else
echo "Something went wrong"
fi
echo "#######"
}
#running function
os_check
but am getting the following error:
./checking.sh: line 10: [: missing `]'
./checking.sh: line 12: [: missing `]'
made some changes to the code:
#!/bin/bash
#testing script
#commands server= $(hostname)
cmd1= $(cat /etc/*-release | grep "7." | wc -l)
cmd2= $(cat /etc/*-release | grep "6." | wc -l)
#creatingFunction
function os_check(){
echo "#######-OS_Checker-#######"
echo "The VM $server is running on: "
if [ $cmd1 -ne 0 ]; then
echo "Centos 7"
elif [ $cmd2 -ne 0 ]; then
echo "Centos 6"
else
echo "Something went wrong"
fi
echo "##########################" }
#running function
os_check
but receiving the following error:
user@server-name:[~]:$ ./checking.sh
./checking.sh: line 5: tordzabbixap1.sandals.com: command not found
./checking.sh: line 6: 1: command not found
./checking.sh: line 7: 0: command not found
#######-OS_Checker-#######
The VM is running on:
./checking.sh: line 13: [: : integer expression expected
./checking.sh: line 15: [: : integer expression expected
Something went wrong
##########################