I have a simple bash script that checks if a program is running and actions accordingly.
#!/bin/bash
check_running=$(pgrep -x redshift)
if [[ -n "$check_running" ]]; then
echo "1"
else
echo "0"
fi
If I execute the script normally (./script) then it will always return 1. But if I use "bash -x script" then it returns the correct outcome
❯ bash -x redshift
++ pgrep -x redshift
+ check_running=
+ [[ -n '' ]]
+ echo 0
0
I have a similar script checking if openvpn is running and it returns the correct value via regular execution.
Here it is in full:
~/.config/polybar/scripts
❯ pgrep -x redshift
~/.config/polybar/scripts
❯ ./redshift
1
~/.config/polybar/scripts
❯ bash -x redshift
++ pgrep -x redshift
+ check_running=
+ [[ -n '' ]]
+ echo 0
0
What am I doing wrong?