#! /bin/bash
Y=1
cd /path/to/run/command/
X= ps -aef | grep autonomy | wc -l
#I am getting X as 6 which is expected
z=$X
#I am storing x in variable z but z is printing null. Always else condition is working.
#Till here z=null hence below condition is failing.
if [[ "${z}" -lt "${Y}" ]]; then
echo "$z"
echo "if is working"
mail -s "Warning: Process count low" xyz@gmail.com <<< "services has been restarted."
source /path/to/run/restartservices.sh
sleep 20
else
echo "else is working"
sleep 20
fi
Asked
Active
Viewed 177 times
0

Aserre
- 4,916
- 5
- 33
- 56

Vaibhav Borse
- 3
- 1
-
If condition is not working. It should execute in case of z less than Y. Main issue is X is not able to store value of count while it is printing value but not able to store. – Vaibhav Borse Nov 22 '21 at 13:15
-
2"I am getting X as 6 which is expected" No you don't. You need `X=$(command)`, not `X= command`. The latter means something entirely different. Note spaces are significant. – n. m. could be an AI Nov 22 '21 at 13:32
-
Running `set -x` to enable trace logging in your script is always a good step to follow before asking questions here; so is running that script through http://shellcheck.net/ – Charles Duffy Nov 22 '21 at 13:44
-
As for the value you're seeing printed, it's printed by the `X = ps ... | wc -l` line, **not** printed by the `echo "$z"` line, because the former line is not actually capturing the output of `wc` as you intend it to (for the reasons given in the linked duplicate questions). Knowing which line is emitting which output is one of the things `set -x` (or `bash -x yourscript`) is good for. – Charles Duffy Nov 22 '21 at 13:45
-
@n.1.8e9-where's-my-sharem. thanks. It worked. – Vaibhav Borse Nov 22 '21 at 14:29