You are not logged in.

#1 2019-02-13 08:41:31

philipW
Member
Registered: 2017-03-21
Posts: 145

[SOLVED]bash if doesn't work

Hello im trying to make a script that warns me if vmware is still running before i shutdown. but it always goes to else. does anyone know what i did wrong?

#!/bin/bash

if 	[[ $(pgrep -x 'vmware') ]] ; 
	then 
		notify-send "vmware is running!"
 	else
 		shutdown
fi

Last edited by philipW (2019-02-14 10:08:30)

Offline

#2 2019-02-13 11:03:31

Lupo Alberto
Member
From: Gomel, Belarus
Registered: 2013-11-25
Posts: 69

Re: [SOLVED]bash if doesn't work

#!/bin/bash

if 	[[ $(pgrep -c 'vmware') > 0 ]] ; 
	then 
		notify-send "vmware is running!"
 	else
 		shutdown
fi

Offline

#3 2019-02-13 12:59:54

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 21,713
Website

Re: [SOLVED]bash if doesn't work

Lupo, if yours would work, the original version would also work.  It would seem 'vmware' isn't a proper match for the full name of the process.  Check `pgrep -il vmware`.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2019-02-13 13:11:20

Lupo Alberto
Member
From: Gomel, Belarus
Registered: 2013-11-25
Posts: 69

Re: [SOLVED]bash if doesn't work

Trilby wrote:

Lupo, if yours would work, the original version would also work.  It would seem 'vmware' isn't a proper match for the full name of the process.  Check `pgrep -il vmware`.

You're right. Thanks.

Last edited by Lupo Alberto (2019-02-13 13:14:52)

Offline

#5 2019-02-13 14:21:39

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVED]bash if doesn't work

In a situation like this I would do

if pgrep -il vmware &>/dev/null; then
    notify-send "vmware is running!"
else
    shutdown
fi

Also FYI you don't need the semicolon on the 'if' line unless you put the 'then' on the same line like in my example.

Last edited by alphaniner (2019-02-13 14:22:05)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#6 2019-02-14 10:07:14

philipW
Member
Registered: 2017-03-21
Posts: 145

Re: [SOLVED]bash if doesn't work

I got it working with alphaniner's example. Trilby was also right there where more vmware processes that where running when i was not using the program. like vmware-usbarbit and vmware-hostd. I used pgrep -il "vmware-vmx" so it warns me when a vm is running and not when the program is open. Thanks for the help, guys!

Last edited by philipW (2019-02-14 10:07:40)

Offline

Board footer

Powered by FluxBB