Reputation: 783
if [ $(ps -ef | grep date) ] && [ $(ps -ef | grep time) ]
then
echo "success"
else
exit 1
fi
is giving [: too many arguments error
What is going wrong?
From a comment:
There are two processes running and I want to check if these two processes are running if executing my automated tool. For that I need to check if those two processes are running, you are right when you say
grep
is included in arguments it always succeeds I want to check only if two processes are running leaving outgrep
part. Can I do some thing like checking$? == 0
but how would I do that for both processes?
Upvotes: 0
Views: 158
Reputation: 185229
Better do this, it will be lighter than multiple subshells $( )
and pipes |
(many fork()
s in the background) :
if pidof &>/dev/null date && pidof &>/dev/null time; then
echo "success"
else
exit 1
fi
No need the test commands :
[ # POSIX test
or
[[ # bash enhanced test
or
test # word test
we use boolean logic here.
Upvotes: 1
Reputation: 754090
The [
command expects rather limited list of values. The output of your ps -ef
commands could be generating a lot of data, which won't look like [ value1 = value2 ]
etc which is what [
expects.
You could try:
if [ "$(ps -ef | grep date)" ] && [ "$(ps -ef | grep time)" ]
then echo "success"
else exit 1
fi
This will report success if there is at least one command referencing date
and at least one command referencing time
(and since the grep
commands includes that argument, it should always succeed), but that may not be what you're after. With a single argument, as enforced by the double quotes, the [
command checks whether the argument is an empty string or not (success if it is not).
What are you really trying to do?
Upvotes: 1