0

I've asked this question on StackOverflow yesterday, made a complete mess of it, and generally wasted people's time due to my lack of understanding of Linux.

Hopefully, I will do a better job now. I tried going about it in many different ways but ended up just confusing the people here on what I actually need.

I need to make a script that will call other sub-menu scripts based on if the user has passed any of the arguments. If the argument is "help", I need to clear the screen and display a short message telling the user how to use the help command. Otherwise, the script is to display the appropriate sub-menu

Further arguments are "File" "Text" "Status" which if used will call an Expert script otherwise it will call a Novice script from the user menu.

I have created a mockup of a total of 6 other script placeholders named fileExpert, fileNovice, textExpert, textNovice, and so on which I would like to execute based on the condition on whether the user has passed some argument.

So far based on my research I get that I would need to set up a condition test like if $>0 then go into expert or if there are no arguments execute novice Other than that I'm completely lost.

Hopefully, this does not read like complete BS and that it makes some sense.

2
  • 2
    Instead of writing a new question without existing code you should try to improve the original question. It was closed because some users think your question about command line arguments is already answered. If the other question/answers are not sufficient to solve your problem, please explain this in your question and tell what specifically you need help with. Your original question can be reopened if you explain why it is not really a duplicate. Why did you accept the answer if it is not sufficient? I don't know if you can undo this.
    – Bodo
    Commented Dec 20, 2020 at 11:36
  • I think you are right. I am still new to Stack and have much to learn.
    – Milan
    Commented Dec 20, 2020 at 13:51

1 Answer 1

-1

Hope the logic is fine here, you can use both short and long opt versions :

Test cases :

./Main.sh  ## Error
./Main.sh -i Text ## Expert 
./Main.sh -i dummy ## Novice
./Main.sh -i File foobar ## Error
./Main.sh -i ## Error 
./Main.sh -h ## Help

Main.sh :

#!/bin/bash

usage="############################ HELP ############################
Main.sh [-h] [-i input] -- call submenus

Where:
    -h,--help  :  Show this helper
    -i,--input :  Could be either File | Text | Status\n"
    
unset -v INPUT
err=false
POS=()
while [[ $# -gt 0 ]]
do
opt="$1"

case $opt in
    -i|--input)
    INPUT="$2" 
    if [ -z "${INPUT}" ]; then 
        echo "Syntax Error : Argument for ${opt} cannot be empty "
        printf "$usage"
        exit 1
    fi
    shift 2
    ;;
    -h|--help)
    printf "$usage"
    echo -e "\r"
    exit 0
    ;;
    *)    
    POS+=("$1") 
    shift
    err=true
    echo "Syntax Error : Additional argument not used"
    printf "$usage"
    exit 1
    ;;
esac
done
set -- "${POS[@]}"

## Your logic here
if [ -z "${INPUT}" ] && [ "${err}" = false ] ; then
        echo "Syntax Error : -i option is necessary"
        printf "$usage"
        exit 1
elif  [ -z "${INPUT}" ] && [ "${err}" = true ]  ; then
        echo "Syntax Error : -i option is necessary"
        exit 1
elif [ "${INPUT}" == "File" ] || [ "${INPUT}" == "Text" ] || [ "${INPUT}" == "Status" ] ; then
    # call Expert scripts
    <path_to_fileExpert> 
    <path_to_textExpert>
    exit 0
else
    # call Novice scripts
    <path_to_fileNovice>
    <path_to_textNovice>
    exit 0
fi 
3
  • Thank you @Reda. I need to study this a bit but I think I can use this. Appreciate it.
    – Milan
    Commented Dec 20, 2020 at 17:29
  • ok take your time, please don't forget to make it as resolved thanks !
    – Reda Salih
    Commented Dec 20, 2020 at 17:31
  • Why a down vote ???
    – Reda Salih
    Commented Dec 21, 2020 at 0:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .