Shell queries?

Gr
5

Hi, I was so wondering how to create queries! Example… I'm writing a Minecraft Installer Script, but want to ask which version of Minecraft (Bedrock or Java) and which minor version (1.8, 1.13 etc.) … Depending on what you choose, different commands should be executed…

Ra

You can save user input in a variable with read and act accordingly. As an example, a simple selection menu:

echo "a) A"
echo "b) B"
echo "What?"

read selection
case $ selection in
a) echo "A" ;;
b) echo "B" ;;
*) echo "error" ;;
esac

Depending on the shell used, there are other options (e.g. Select in Bash, …). You can also create a graphical menu e.g. With ncurses (Dialog, whiptail, zenity, etc.).

Gr

Where do I have to use the commands if, for example, A is to install the 1.8 and B the 1.16 version of Minecraft?

Ra

If A

In my example a (lower case) is used, i.e. If A is entered, *) echo "Error" ;; executed. If it does not matter whether lower or upper case letters are entered, you have to adapt the code accordingly (e.g. With [aA], [bB], etc.).

Where do I have to use the commands?

In the case after a) come the respective commands that are to be executed when selecting a. With ;; tell case the end of the command block.

E.g.:

case $ selection in
[Aa]) echo "A"
Command 1
Command 2
last command for selection a
;;
b) echo "B" ;;
*) echo "error" ;;
esac

Gr

What if I want to make more branches? Stop asking if Minecraft PE or Java and then as a sub question which version?

Ra

Then you just do the same thing again under the respective selection. To keep the whole thing a bit clearer, you can also work with functions.

Example without functions:

#! / bin / sh

echo "------------------------------------------------ --- "
echo "| Super Minecraft Installation Script Deluxe Plus |"
echo "------------------------------------------------ --- "echo" a) Minecraft PE "
echo "b) Minecraft Java"
echo "Which Minecraft?"
read selection
case $ selection in
[Aa])
echo "1) 1.11"
echo "2) 2.22"
echo "Which version?"
read selection
case $ selection in
1) echo "Install Minecraft PE Version 1.11" ;;
2) echo "Install Minecraft PE Version 2.22" ;;
*) echo "Wrong selection" ;;
esac
;;
[Bb])
echo "1) 1.11"
echo "2) 2.22"
echo "Which version?"
read selection
case $ selection in
1) echo "Install Minecraft Java Version 1.11" ;;
2) echo "Install Minecraft Java Version 2.22" ;;
*) echo "Wrong selection" ;;
esac
;;
*) echo "error" ;;
esac

Example with functions:

#! / bin / sh

submenu () {
echo "1) 1.11"
echo "2) 2.22"
echo "Which version?"
read selection_submenu
case $ selection_submenu in
1) version = "Version 1.11" ;;
2) version = "Version 2.22" ;;
*) echo "Wrong selection" ;;
esac
}

main menu() {
echo "a) Minecraft PE"
echo "b) Minecraft Java"
echo "Which Minecraft?"
read selection
case $ selection in
[Aa]) submenu
echo "Install Minecraft PE $ version" ;;
[Bb]) submenu
echo "Install Minecraft Java $ version" ;;
*) echo "error" ;;
esac
}

echo "------------------------------------------------ --- "
echo "| Super Minecraft Installation Script Deluxe Plus |"
echo "------------------------------------------------ --- "

main menu

P.s. Don't just copy, but also understand. You should read something about the shell and shell scripting, first of all the manpage (man sh). With an extended shell, also its manpage (e.g. Man bash).

As already mentioned, with bash you also have the option of using "select" to create selection menus.