Reputation: 1
When I count process's number to use wc -l
, The number is 2 in the command line, but I found it is 4 in the shell scripts, what's happened?
$ ps -ef |grep -v grep |grep etcd |wc -l
2
$ bash -x count.sh etcd
++ ps -ef
++ grep -v grep
++ grep etcd
++ wc -l
+ num=4
+ case $1 in
+ echo 4
4
The shell script
#!/usr/bin/env bash
num=$(ps -ef |grep -v grep |grep etcd |wc -l)
case $1 in
etcd)
echo ${num}
;;
*)
echo "other"
;;
esac
Upvotes: 0
Views: 53
Reputation: 158090
I guess you are calling the script with an argument: count.sh etcd
. The script itself will be part of the ps output, which does add to the results.
Use pgrep
, it is meant for that
pgrep -c etcd
I further recommend to use the -x
(exact match) argument, to prevent it from matching etcdctl
, for example:
pgrep -xc etcd
#!/usr/bin/env bash
num=$(pgrep -xc etcd)
case $1 in
etcd)
echo "${num}"
;;
*)
echo "other"
;;
esac
Upvotes: 1