Scenario
I'm trying add the following flag before the execution of main process in shell script to make sure that this script is running only once at a time by checking the number of pids of same-name script:
this_bash=$(basename $0)
this_pid=${$}
is_running="$(pidof -x $this_bash -o $this_pid | wc -l )"
I found it always returns 1, even when there is no other script with the same name running.
Investigation
For further information I tried this:
z=$(pidof -x $this_bash -o $this_pid)
echo "[$z]"
echo "[$(pidof -x $this_bash -o $this_pid)]"
echo "[$($z | wc -l )]"
echo "[$(pidof -x $this_bash -o $this_pid | wc -l )]"
the square brackets are to make sure there are no hidden white space chars.
The result was:
[]
[]
[0]
[1]
Question
I don't understand why storing pidof
as variable returns the expected result, while piping the commands directly does not.