Results 1 to 7 of 7

Thread: Bash scripting question

  1. #1

    Bash scripting question

    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.

    Code:
    if [ -z $(ps ux | grep mozilla) ]
    then
        echo true
    fi
    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).

  2. #2
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re:Bash scripting question

    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

  3. #3

    Re:Bash scripting question

    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
    fi
    Code:
    bash-2.05b$ ./mozilla.sh 
    ./mozilla.sh: line 2: [: too many arguments

  4. #4

    Re:Bash scripting question

    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'

  5. #5

    Re:Bash scripting question

    Code:
    #!/bin/bash
    /opt/phoenix/phoenix -remote "openURL($@, new-tab)" ||
            exec /opt/phoenix/phoenix "$@";
    but change phoenix to mozilla

    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:
    Code:
    if pidof mozilla-bin > /dev/null; then
      #its running so we can open a new window
    else
      #start it
    fi
    the problem you had was if `ps ux | grep -v grep | grep mozilla` == "" then the syntax is wrong (if [ ]; then... )

  6. #6
    Associate
    Join Date
    Dec 2002
    Posts
    13

    Re:Bash scripting question


    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

  7. #7

    Re:Bash scripting question

    [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.

Similar Threads

  1. Bash scripting and the stderr
    By 10Dedfish in forum Programming
    Replies: 2
    Last Post: 12-30-2003, 01:12 PM
  2. bash scripting question
    By seanmclemon in forum Programming
    Replies: 7
    Last Post: 09-07-2003, 12:58 AM
  3. need some bash scripting help
    By mountainman in forum Programming
    Replies: 2
    Last Post: 08-30-2002, 12:44 PM
  4. bash scripting and quoting
    By in forum Programming
    Replies: 5
    Last Post: 03-21-2002, 12:51 AM
  5. Bash Scripting
    By cuban in forum Programming
    Replies: 3
    Last Post: 09-10-2001, 01:03 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •