Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 3200

Programming • [Bash] Improving menu selection

$
0
0
I am making a menu and was curious if there was a way to make it so quit was q rather than the last number. I have tried REPLY = "q" rather than

Code:

REPLY = 1 + ${#sorted_options[@]} 
which did not work. I tried a case statement but was not able to get it working.

The code (please steal if you find it helpful):

Code:

#!/bin/bash##Menu for filesprompt="Please select a file to compile or run:"options=( $(cd Java_Files && find . -maxdepth 1 -type f -name "*.java") )##code that sortsIFS=$'\n' sorted_options=($(sort <<<"${options[*]}"))unset IFS ##unset, undefines a variablePS3="$prompt "select opt in "${sorted_options[@]}" "Quit" ; do    if (( REPLY = 1 + ${#sorted_options[@]}  )) ; then        exit    elif (( REPLY > 0 && REPLY <= ${#sorted_options[@]} )) ; then        echo  "You picked $opt which is file $REPLY"        break    else        echo "Invalid option. Try another one."    fidone## this grabs the file name you selectedfile=$opt##This searches for the .java file to see if it exists##This is unneeded but is here being its working code i can use in the futuresearch_dir= "" ##put your dir herefile_name=$fileif [[ -d "$search_dir" ]]; then  if [[ -f "$search_dir/$file_name" ]]; then    echo "File $file_name exists in directory $search_dir."  else    echo "File $file_name does not exist in directory $search_dir."  fielse  echo "Directory $search_dir does not exist."fi##This searches for the .class to see if it existsfileClass=$optfileClass=${fileClass//.java/}fileClass+=".class"if [[ -d "$search_dir" ]]; then  if [[ -f "$search_dir/$fileClass" ]]; then  echo "File $fileClass exists in directory $search_dir." && classExists="y"  elseecho "File $fileClass does not exist in directory $search_dir." && classNotExists='y'  fielse  echo ""fi##This next segment needs to be edited so if .class does not exist it compiles and then does java $file##Compiles code if class does not existif [[ "$classNotExists" = "y" ]]; thencd $search_dir && javac $file;sleep 1 && cd $search_dir && java $file; ##has not been testedfi##Compiles the code if you say yesif [[ "$classExists" = "y" ]]; thenecho "would you like to recompile the java code? y/n"read -r Answerfiif [[ "$Answer" =~ [yY] ]]; thencd  $search_dir && javac $file;java $filefi##Just runs the code if you say noif [[ "$Answer" =~ [nN] ]]; thencd $search_dir && java $file && java $filefi

Statistics: Posted by abbatrombone — 2024-02-21 00:10 — Replies 1 — Views 60



Viewing all articles
Browse latest Browse all 3200

Trending Articles