Results 1 to 9 of 9

Thread: Help with a bash script

  1. #1
    Join Date
    Jan 2007
    Beans
    601
    Distro
    Ubuntu

    Help with a bash script

    I have written the following bash script for a friend to add as a menu item and would like some input on it:

    Code:
    #!/bin/bash
    # Filename: browseropen.sh
    # Version: 0.1
    # Date: April 13, 2010
    # Author: Ian MacGregor (aka ardchoille)
    # License: GPL
    # Requires: firefox, zenity
    # Description: This script opens a user-supplied URL in the firefox web browser
    
    myurl=`zenity --entry --title="Firefox - Open Webpage" --text="Please enter a URL:" --entry-text ""`
    
    # The zenity text entry dialog consist of “ok” and “cancel” buttons.
    # When user clicks “ok” it returns 0, “cancel” returns 1.
    
    # If user clicks the "cancel" button
    if [ $? == 1 ]
    then
    	exit 1
    # If user clicks the "ok" button and text box is empty
    elif [ -z $myurl ]
    then
    	exit 1
    fi
    
    # Open the specified URL in a new tab if firefox is running.
    # Otherwise open in a new instance of firefox.
    firefox -new-tab $myurl
    
    exit
    The script works correctly, I was just wondering if the syntax is correct or if it would cause any unforeseen problems.
    Last edited by ardchoille42; April 14th, 2010 at 07:45 AM. Reason: Solved

  2. #2
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: Help with a bash script

    Looks alright to me.
    Code:
    [ -z $myurl ]
    is technically wrong but won't cause any problems.

  3. #3
    Join Date
    Jan 2007
    Beans
    601
    Distro
    Ubuntu

    Re: Help with a bash script

    Quote Originally Posted by prodigy_ View Post
    Looks alright to me.
    Code:
    [ -z $myurl ]
    is technically wrong but won't cause any problems.
    How would I change that to be technically correct?

  4. #4
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: Help with a bash script

    Quote Originally Posted by ardchoille42 View Post
    How would I change that to be technically correct?
    Just use double quotes. Like:
    Code:
    [ -z "$foo" ]
    Again, in case of test -z it doesn't really matter. I say it's wrong only because it may easily lead to wrong assumptions. For example
    Code:
    [ -n $foo ]
    returns "true" if $foo isn't defined at all. Not quite as expected, eh?

  5. #5
    Join Date
    Jan 2007
    Beans
    601
    Distro
    Ubuntu

    Re: Help with a bash script

    Quote Originally Posted by prodigy_ View Post
    Just use double quotes. Like:
    Code:
    [ -z "$foo" ]
    Again, in case of test -z it doesn't really matter. I say it's wrong only because it may easily lead to wrong assumptions. For example
    Code:
    [ -n $foo ]
    returns "true" if $foo isn't defined at all. Not quite as expected, eh?
    Very good point, I'll use double quotes. Thank you very much.

  6. #6
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Help with a bash script

    hi
    i think what you want is to insert a command or something else ?
    that's the way it works:
    #!/bin/bash
    #
    command=`zenity --title "run a command" --entry --text "give me the command" --width 1000 --height 50`
    firefox -new-tab $command
    exit 0

    have fun
    ciao
    "What is the robbing of a bank compared to the FOUNDING of a bank?" Berthold Brecht

  7. #7
    Join Date
    Jan 2007
    Beans
    601
    Distro
    Ubuntu

    Re: Help with a bash script

    Quote Originally Posted by rnerwein View Post
    hi
    i think what you want is to insert a command or something else ?
    that's the way it works:
    #!/bin/bash
    #
    command=`zenity --title "run a command" --entry --text "give me the command" --width 1000 --height 50`
    firefox -new-tab $command
    exit 0

    have fun
    ciao
    The name of the variable doesn't really matter, I was just worried about error checking, which is why there is so much more code in my script. What if the user doesn't enter anything but hits the OK button anyway? What if the user hits the Cancel button? With your code, a new firefox window opens if the user doesn't specify a URL or hits the cancel button, and that's what I was trying to alleviate. Or did I misunderstand your post?
    Last edited by ardchoille42; April 14th, 2010 at 10:36 AM.

  8. #8
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Help with a bash script

    Quote Originally Posted by ardchoille42 View Post
    The script works correctly, I was just wondering if the syntax is correct or if it would cause any unforeseen problems.
    To be technically correct, I think [ $? == 1 ] should be [ $? -eq 1 ] because it is integer comparison.
    Last edited by kaibob; April 14th, 2010 at 05:10 PM.

  9. #9
    Join Date
    Jan 2007
    Beans
    601
    Distro
    Ubuntu

    Re: Help with a bash script

    Quote Originally Posted by kaibob View Post
    To be technically correct, I think [ $? == 1 ] should be [ $? -eq 1 ] because it is integer comparison.
    That works, I'll make that change.. thanks!
    Last edited by ardchoille42; April 14th, 2010 at 09:00 PM. Reason: Fixed error in reply

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
  •