Home > Software engineering >  Shell: run segment 1 or 2 of script dependent on $variable
Shell: run segment 1 or 2 of script dependent on $variable

Time:07-10

I've been trying to figure out a way in Shell as to how I could run a different segment of a script dependent on the variable that was given. Here, the variables are either X or W.

while true; do
    read -p "Do you want to run X or W? (X/W) " xw
        case $xw in
            [Xx]* ) echo -e "Starting X prompt..." && server=$x;;
            [Ww]* ) echo -e "Starting W prompt..." && server=$w;;
            * ) echo -e "Incorrect input detected, repeating prompt...";;
        esac
    done
    
    (server=$x)
    while true; do
    read -p "What X do you want to enter? (Y/N) " yn
        case $yn in
            [Yy]* ) echo -e "Starting...\n" && exec test;;
            [Nn]* ) echo -e "Aborting...\n" && exit;;
            * ) echo -e "Incorrect input detected, repeating prompt...";;
        esac
    done

    (server=$w)
    while true; do
    read -p "What W do you want to enter? (Y/N) " yn
        case $yn in
            [Yy]* ) echo -e "Starting...\n" && exec test;;
            [Nn]* ) echo -e "Aborting...\n" && exit;;
            * ) echo -e "Incorrect input detected, repeating prompt...";;
        esac
    done

Is there a way I can create a "goto"-ish statement here that'd allow me to perform one of the two prompts depending on the variable? I haven't been able to figure it out yet. I appreciate any and all help.

CodePudding user response:

In general, you would want to use functions for each case. Something like:

while read -p "Do you want to run X or W? (X/W) " xw; do
        case $xw in
        [Xx]*) run_x;;
        [Ww]*) run_w;;
        * ) echo -e "Incorrect input detected, repeating prompt..." >&2 ;;
        esac
done

In this case, there seems to be a lot of redundancy in the two code paths, so perhaps you just want something like:

#!/bin/sh


while read -p "Do you want to run X or W? (X/W) " xw; do
    case $xw in
    [Xx]* ) server=$x; prompt=X;;
    [Ww]* ) server=$w; prompt=W;;
    * ) printf '%s\n' "Incorrect input detected, repeating prompt..." >&2;;
    esac
    printf '%s' "Starting $prompt prompt..."
    while read -p "What $prompt do you want to enter? (Y/N) " yn; do
        case $yn in
        [Yy]* ) printf "Starting...\n"; exec mytest;;
        [Nn]* ) printf "Aborting...\n"; exit;;
        * ) printf "Incorrect input detected, repeating prompt...\n" >&2;;
        esac
    done
done

Don't call your executable test; there is already a common utility named test and using that name is bound to cause problems. Write all error messages to stderr. (It's not clear if aborting... is an error message; that's a judgement call. If you decide to make it an error message, make sure you exit 1.)

CodePudding user response:

Put all the code into the case statement.

while true; do
    read -p "Do you want to run X or W? (X/W) " xw
    case $xw in
        [Xx]* )
            echo -e "Starting X prompt..."
            server=$x
            while true; do
                read -p "What X do you want to enter? (Y/N) " yn
                case $yn in
                    [Yy]* ) echo -e "Starting...\n" && exec test;;
                    [Nn]* ) echo -e "Aborting...\n" && exit;;
                    * ) echo -e "Incorrect input detected, repeating prompt...";;
                esac
            done
            ;;              
        [Ww]* )
            echo -e "Starting W prompt..."
            server=$w
            while true; do
                read -p "What W do you want to enter? (Y/N) " yn
                case $yn in
                    [Yy]* ) echo -e "Starting...\n" && exec test;;
                    [Nn]* ) echo -e "Aborting...\n" && exit;;
                    * ) echo -e "Incorrect input detected, repeating prompt...";;
                esac
            done
            ;;
        * ) echo -e "Incorrect input detected, repeating prompt...";;
    esac
done
  • Related