1

I'm trying to create a bash script that lets the user use both a menu style and also a command line switch style interface. I was wondering if there was a way to pass a parameter to a menu option in select as if it were just an argument to a script. For example, in my script I can specify a file to move to my junk folder by saying junk -d file.txt. But if the user does not specify any params it should use the select option. i.e. 1) move file 2) clear junk folder 3) watch folder etc.

On this menu, I want to be able to write the option and then send an argument to that option. Like 1 filetobemoved.txt. I have tried putting $OPTARG after the method that gets called by the menu but this doesn't seem to work.

Here is the code I have for the menu at the moment, I'm trying to call the recover method and pass an argument (the filename) to it.

if (( $# == 0 ))
then if (( $OPTIND == 1 )) 
 then select menu_list in list recover delete total watch kill exit
     do case $menu_list in
     "list") list;;
     "recover") recover $GETOPT;;
     "delete") delete;;
     "total") total;;
     "watch") _watch;;
     "kill") _kill;;
     "exit") exit 0;;
     *) echo "unknown option";;
     esac
  done
fi

Thanks for any help.

1 Answer 1

2

Put the code that executes the cases in a function. Then you can check for the command line option and skip the select, and just call the function.

do_command() {
    case "$1" in
     "list") list;;
     "recover") recover $GETOPT;; # had -a after recover
     "delete") delete;;
     "total") total;;
     "watch") _watch;;
     "kill") _kill;;
     "exit") exit 0;;
     *) echo "unknown option";;
    esac
}
if (($# == 0))
then if (( $OPTIND == 1 )) 
    then select menu_list in list recover delete total watch kill exit
         do do_command "$menu_list"
         done
    fi
elif [[ "$1" = "-d" ]]
then do_command delete
...
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.