Issue
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.
Solution
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
Answered By - Barmar Answer Checked By - Gilberto Lyons (WPSolving Admin)