Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Help with a bash script menu


    Hello everyone! I am a n00b to the LinuxForums, I hope one of the scripting gurus can help me, it is a very basic script and the problem is small I just cant see it!

    Synopsis:

    I am writing a basic bash script menu and in this script there a few requirements.

    Ensure that user enters either zero arguments or one argument after your script-name.

    If that user enters zero arguments after your script-name, then your script will prompt the user for an output html file pathname. Once output file is entered it starts the menu.

    If that user enters one arguments then it will start the menu.

    If the user enters two arguments it prompts
    "Error: Command requires zero or one argument"
    "USAGE: ./qpress.bash [html_file_pathname]"


    Now, when I issue my script with zero arguments it prompts me as it should, once I enter the filename it starts the menu.

    *Here is the problem*
    When I enter 1 argument it just clears the screen nothing happens. It should start the menu but it doesn't.

    When I enter two or more arguments it prompts me with the error msg as it should.

    here is what my script looks like. I know it has to do with one of the error checking statements. Someone please help me!


    #!/bin/bash


    if [ ! -f $2 ]
    then

    echo "Command requires zero or one argument" >&2
    echo "USAGE:./qpres.bash [httml_file_pathname]" >&2
    exit
    fi



    if [ $# -eq 0 ]
    then

    clear
    read -p "Enter output file pathname: $file ">&2
    else
    file=1
    clear
    exit
    fi


    clear
    cat <<+
    *======================================*
    *======================================*
    --------- <-QUICK PRESS MENU-> ---------
    ****************************************
    ****************************************

    1. Add Text
    2. Clear all Text
    3. Publish webpage
    4. Exit

    +
    read -p "Enter a menu item (1,2,3,4): " choice
    case $choice in


    1) echo "Item chosen: Add Text "
    ;;


    2) echo "Item chosen: Clear all Text "
    ;;


    3) echo "Item chosen: Publish Webpage "
    ;;


    4) echo "Item chosen: Exit "
    exit 0
    ;;


    *) echo "Invalid Selection: $choice." >&2
    ;;
    esac



    Thank you in advance

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    First off, in the future, please wrap your code in [code] tags.

    Anyway, your problem lies in this section here:
    Code:
    if [ $# -eq 0 ]
    then
        clear
        read -p "Enter output file pathname: $file ">&2
    else
        file=1
        clear
        exit
    fi
    Let's look at what happens here. If no arguments were given, we clear the screen and prompt for a file (note that your read will not store the chosen file into $file as I think you intend). However, if any arguments were given, we set $file to be 1, clear the screen, then exit the script.

    You actually have 3 cases here: 0 arguments, 1 argument, or other. I suggest you look into the elif control statement in bash: it will allow you to say something like this:
    Code:
    if [ "$#" -eq 0 ]; then
        echo "I was given no arguments."
    elif [ "$#" -eq 1 ]; then
        echo "I was given one argument."
    else
        echo "I was given more than one argument."
    fi
    I hope that helps.

  3. #3
    -->
    Quote Originally Posted by Cabhan View Post
    First off, in the future, please wrap your code in [code] tags.

    Anyway, your problem lies in this section here:
    Code:
    if [ $# -eq 0 ]
    then
        clear
        read -p "Enter output file pathname: $file ">&2
    else
        file=1
        clear
        exit
    fi
    Let's look at what happens here. If no arguments were given, we clear the screen and prompt for a file (note that your read will not store the chosen file into $file as I think you intend). However, if any arguments were given, we set $file to be 1, clear the screen, then exit the script.

    You actually have 3 cases here: 0 arguments, 1 argument, or other. I suggest you look into the elif control statement in bash: it will allow you to say something like this:
    Code:
    if [ "$#" -eq 0 ]; then
        echo "I was given no arguments."
    elif [ "$#" -eq 1 ]; then
        echo "I was given one argument."
    else
        echo "I was given more than one argument."
    fi
    I hope that helps.

    Thank you for your prompt reply. It wasn't exactly what I was looking for, but it did help me to figure out what I was doing wrong with what was needed in my script! Thank you very much!!

$spacer_open
$spacer_close

Posting Permissions

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