Solved #!/bin/sh how to save a shell command output into a string variable

Hello fellow freeBSD forum members,

Can you please help

Is there anyway to save the following into a variable?
Screenshot 2023-08-24 at 1.02.54 PM.png



What I would like to do is check to see if this output is != null when true adjust some LEDS

The following does not function as expected:
Code:
#!/bin/sh

str=`pfctl -vvss | grep ‘, rule 79’` 

str = echo $str

if [! -n “$str” ]

then

  sysctl -q dev.gpio.2.led.2.pwm=1

  gpioctl -f /dev/gpioc2 6 duty 1 >/dev/null

  fi

fi
What I would like to occur is save the output as a string variable into str with echo

use the if statement to check if it is not null if its not it will run the two commands after

that's it.
 
man grep()
Code:
EXIT STATUS
     The grep utility exits with one of the following values:

     0     One or more lines were selected.
     1     No lines were selected.
Try to use grep's exitstatus as an argument for 'if'.

Simple example:
Code:
#!/bin/sh

if ifconfig | grep inet
then echo x
fi

p.s. also I see one 'if' but two 'fi' in your script example
 
man grep()
Code:
EXIT STATUS
     The grep utility exits with one of the following values:

     0     One or more lines were selected.
     1     No lines were selected.
Try to use grep's exitstatus as an argument for 'if'.

p.s. also I see one 'if' but two 'fi' in your script example
Something like this??

Code:
#!/bin/sh
/root/deviceonline
if [“$(pfctl -vvss | grep ', rule 79’)” -ge 1];
then
    sysctl -q dev.gpio.2.led.2.pwm=1
    gpioctl -f /dev/gpioc2 6 duty 1 >/dev/null
    fi
 
More like:

Code:
#!/bin/sh

pfctl -vvss | grep ', rule 79' >/dev/null
res=$?

if [ $res = 0 ]; then
  sysctl -q dev.gpio.2.led.2.pwm=1
  gpioctl -f /dev/gpioc2 6 duty 1 >/dev/null
fi

The key part is $? contains the exit code of the last executed program.

To be fair though, the checking of output should have worked too (your previous idea):

Code:
#!/bin/sh

str="$(pfctl -vvss | grep ', rule 79')"

if [ -z "$str" ]; then
  sysctl -q dev.gpio.2.led.2.pwm=1
  gpioctl -f /dev/gpioc2 6 duty 1 >/dev/null
fi

-z is to check if variable string length is zero. -n to check if string variable length is non-zero.
 
Thank you everyone that worked the LED lights when the firewall rule is active now.

 
I found one more that you could set up the case command to iterate over with the full pfctl -vvss output

Code:
#!/bin/sh
state=$( pfctl -vvss )
res=1
resb=1
case "$state" in
  *, rule 79*)
    res=0
    ;;
esac
case "$state" in
  *192.168.1.11*)
    resb=0
    ;;
esac

if [ $res = 0 ] && [ $resb = 0 ];
then
  sysctl -q dev.gpio.2.led.1.pwm=1
  gpioctl -f /dev/gpioc2 3 duty 50 >/dev/null
  sysctl -q dev.gpio.2.led.2.pwm=1
  gpioctl -f /dev/gpioc2 7 duty 0 >/dev/null
  gpioctl -f /dev/gpioc2 6 duty 50 >/dev/null
elif [ $res = 0 ];
then
  sysctl -q dev.gpio.2.led.1.pwm=1
  gpioctl -f /dev/gpioc2 3 duty 0 >/dev/null
  sysctl -q dev.gpio.2.led.2.pwm=1
  gpioctl -f /dev/gpioc2 7 duty 0 >/dev/null
  gpioctl -f /dev/gpioc2 6 duty 50 >/dev/null
elif [ $resb = 0 ];
then
  sysctl -q dev.gpio.2.led.2.pwm=1
  gpioctl -f /dev/gpioc2 7 duty 0 >/dev/null
  gpioctl -f /dev/gpioc2 6 duty 0 >/dev/null
  sysctl -q dev.gpio.2.led.1.pwm=1
  gpioctl -f /dev/gpioc2 3 duty 50 >/dev/null
else
  sysctl -q dev.gpio.2.led.1.pwm=1
  gpioctl -f /dev/gpioc2 3 duty 0 >/dev/null
  sysctl -q dev.gpio.2.led.2.pwm=1
  gpioctl -f /dev/gpioc2 6 duty 0 >/dev/null
  gpioctl -f /dev/gpioc2 7 duty 50 >/dev/null

fi

I wanted to share
 
Last edited by a moderator:
Back
Top