0

have the following code

#!/bin/bash

#testing script
#commands
server= $(hostname)
cmd1= "$(cat /etc/system-release | grep "7." | wc -l)" 
cmd2= "$(cat /etc/system-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 it's given me the following errors:

user@server-name:[~]:$ ./checking.sh
./checking.sh: line 5: server-name: 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
##########################

basically am doing a count to check if is centos 6 or 7, but not able to determinate why doesn't work properly on the script, any advise will be appreciated!

command example:

user@server-name:[~]:$ cat /etc/system-release | grep "7." | wc -l
1
user@server-name:[~]:$ cat /etc/system-release | grep "6." | wc -l
0
M. Bueso
  • 1
  • 2
  • 1
    Don't put spaces around the `=` in an assignment command ([shellcheck.net](https://www.shellcheck.net) would've pointed out this problem). Actually, it'd be even better to skip the variable, `cat`, and `wc` entirely, and just use `if grep -q "7[.]" /etc/system-release; then echo "Centos 7"` etc. – Gordon Davisson Sep 20 '22 at 03:16
  • Oh, and you might need to adjust those patterns; `7.` will match a "7" anywhere in the file except at the end of a line (because in a regex, `.` matches any single character), and `7[.]` will match "7." anywhere, including in something like "6.7.1234". – Gordon Davisson Sep 20 '22 at 03:26

0 Answers0