Using "-l" to ask user to choose from a list of pre-defined options

Download the following file to your PC, then upload it to remote.it:

 

The r3_header line

The r3_header line is a special line that starts with the hash/pound symbol #.  This is interpreted by the shell processor as a comment.  The remot3.it Bulk Scripting server reads this line, if present, when you upload the file, and keeps track of parameters that will be asked for whenever the script is executed.

The "Cloak-SSH.sh" script uses the "-l" parameter in an r3_header comment line near the top of the file, as shown:

# r3_header, -l (Dummy|Choose Action|Cloak SSH|Uncloak SSH)

 The information in parentheses should be interpreted as:

( Dummy Value | Prompt | Option 1 | Option 2 )
  • Dummy Value = a place holder, not used but has to be there
  • Prompt = What to show on the dialog when it appears
  • Option 1 = String representing the first option
  • Option 2 = String representing the second option
  • additional options are possible by following the pattern shown above

The given example shows the following dialog when you run "Execute Script".

mceclip1.png

Choose the desired option, then click on the "Finish" button.

Getting the information from the "-l" flag

Look for the following section near the end of the script.

################################################
# parse the flag options (and their arguments) #
################################################
while getopts l: OPT; do
case "$OPT" in
l)
# option selected from list in r3-header
Log "(l) $OPTARG"
action=$(echo "$OPTARG")
;;
*)
Log "$OPT"
;;
esac
done

At the end of the "getopts" loop, the string corresponding to the selection you made will be in the variable.  Then you simply compare that to the known options and execute the appropriate commands. 

if [ "$action" == "Cloak SSH" ]; then
cloakSSH
elif [ "$action" == "Uncloak SSH" ]; then
uncloakSSH
fi

 

This example allows you to cloak or uncloak the SSH server on a Raspberry Pi running Raspbian or Raspberry Pi OS.  Finally, the results of the script are shown in the status cells.

mceclip0.png

========

Was this article helpful?
0 out of 0 found this helpful