0

I am trying to get this to run and am having no luck. I am trying to see if 2 services exist. If they exist and they are disabled, it should return pass. If not then fail.

#!/bin/bash
# echo "Reference Number~Result~Risk Rating~Service~Service Status~Startup Mode~Other Information~Last Results time"

z=$[svcs -a | grep comsat] 
y=$[svcs -a | grep comsat-udp]  

if [ "$z" = "online"* ] && [ "$y" = "online"* ]
then
       echo "SET-4555~Pass~High~~~~"
else
       echo "SET-4555~Fail~High~~~$z~"       
fi
5
  • This:t/TempLinux_8_.sh: line 3: svcs -a | grep comsat: syntax error in expression (error token is "comsat") [filepath].sh: line 4: svcs -a | grep comsat-udp: syntax error in expression (error token is "comsat-udp")","
    – ocdrew1503
    Sep 12, 2018 at 0:16
  • But when I run this it always passes no matter my "if" statement: #!/bin/bash # echo "Reference Number~Result~Risk Rating~Service~Service Status~Startup Mode~Other Information~Last Results time" z=$(svcs -a | grep comsat) y=$(svcs -a | grep comsat-udp) if [ "$z" = "disabled"* ] && [ "$y" = "enabled"* ] then echo "SET-4555~Pass~High~~~~" else echo "SET-4555~Fail~High~~~$z|$y~" fi
    – ocdrew1503
    Sep 12, 2018 at 0:22
  • I did that, see above and now no error but no matter what I use in the if portion it returns failed. Is it only reading Z and not Y? and THANK YOU
    – ocdrew1503
    Sep 12, 2018 at 0:28
  • I was using disabled. Let me try offline
    – ocdrew1503
    Sep 12, 2018 at 0:42
  • I finally got this to work by placing double brackets around the if variables instead of single. Big THANK YOU to everyone for your assistance.
    – ocdrew1503
    Sep 12, 2018 at 21:07

1 Answer 1

0

There are some errors in your code:

#!/bin/bash
# echo "Reference Number~Result~Risk Rating~Service~Service Status~Startup Mode~Other Information~Last Results time"

z=$(svcs -a | grep comsat) 
y=$(svcs -a | grep comsat-udp)  

if [ "$z" == "XXX" ] && [ "$y" == "XXX" ]
then
       echo "SET-4555~Pass~High~~~~"
else
       echo "SET-4555~Fail~High~~~$z~"       
fi

where XXX means the output of the variable z and y

2
  • 1
    Appreciated. I made the changes you suggested and it runs with no error, however, even though both are disabled in my system it is not returning a pass. What am I missing?
    – ocdrew1503
    Sep 12, 2018 at 0:41
  • 1
    == instead of the standard = is a needless Bashism here.
    – JdeBP
    Sep 12, 2018 at 8:15

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