Why not keep it simple:
Code:#!/bin/sh if [ `ps ux |grep -v grep | grep mozilla` ] then echo "Mozilla is dead" else echo "Mozilla is running" fi
I was trying to make a script for someone that will open a url in Mozilla. It should open Mozilla if it's not already, or open a new window if it is. I was going one step at a time, getting one part to work then going to the next. But so far, I can't even get the conditional statement to work. Here's the code so far.
It gripes about too many arguments. I have tried doing it different ways, but to no avail. If someone can help me figure out the if part, I'm confident I can fill in the then and else parts (and would like the challenge anyway).Code:if [ -z $(ps ux | grep mozilla) ] then echo true fi
Why not keep it simple:
Code:#!/bin/sh if [ `ps ux |grep -v grep | grep mozilla` ] then echo "Mozilla is dead" else echo "Mozilla is running" fi
It's still giving me the error. I only changed the condition part, but obviously the rest is correct anyway. Let me reprint my code along with the error. I tried using sh as well like you have, but it also gave pretty much the same error so I changed it back to bash. Did you try this code out yourself by the way? FreeBSD uses Gnu Bash so it shouldn't be operating system differences.
Code:if [ `ps ux | grep -v grep | grep mozilla` ] then echo true fiCode:bash-2.05b$ ./mozilla.sh ./mozilla.sh: line 2: [: too many arguments
Alright, I tried it exactly as you put it, just changing what's in the echo quotes. It gives this error instead.
Code:bash-2.05b$ ./mozilla.sh ./mozilla.sh: line 4: syntax error near unexpected token `else' ./mozilla.sh: line 4: `else'
but change phoenix to mozillaCode:#!/bin/bash /opt/phoenix/phoenix -remote "openURL($@, new-tab)" || exec /opt/phoenix/phoenix "$@";
whats happening is we just assume moz (or phoenix in my case) is running and connect and open a new window in it, if we fail to its not running so start it.
to avoid assuming its running:
the problem you had was if `ps ux | grep -v grep | grep mozilla` == "" then the syntax is wrong (if [ ]; then... )Code:if pidof mozilla-bin > /dev/null; then #its running so we can open a new window else #start it fi
Or, another possibility:
Code:#!/bin/bash if ps ux | fgrep mozilla | grep -v fgrep >/dev/null ; then # It's running else # It's not running fi
[quote author=gorn link=board=9;threadid=5984;start=0#57073 date=1041495607]
the problem you had was if `ps ux | grep -v grep | grep mozilla` == "" then the syntax is wrong (if [ ]; then... )
[/quote]
Ah. I understand now. No wonder the script actually worked when Mozilla was open. I thought that was very strange. I'll try your quite simple script.
Bookmarks