Troubleshooting whiptail

Here is a code snippet using whiptail , it fails to complete giving me long list of options available for whiptail .

That is great, but how do I determine which of the current opinions is wrong?
I did tried inserting comment (#) into options and it just did not work.
Deleting the option created another errors.

So in case of "checklist" - can I start with minimum code which would compile and then add "options" to it ?

Cheers

PS

My main problem using bash is having or not having spaces in right places.

That is hard on old eyes. Maybe that is the issue here.

    case $ADVSEL in
 108         1)
 109             echo "Option 1"
 110 pause 
 111             whiptail \
 112                 --title "checklist test Option 1" \
 113                 --checklist \
 114                 --separate-output "Choose: " 20 78 15 \ 
 115 
 116 "John" "" on \
 117 "Glen" "" off \
 118 "Adam" "" off 2>results
 119
 120  echo "HERE Option 1"
 121
 122 pause
 123
 124 while read choice
 pause
 123
 124 while read choice
 125 do
 126         case $choice in
 127                 John) firstroutine
 128                 ;;
 129                 Glen) secondroutine
 130                 ;;
 131                 Adam) thirdroutine
 132                 ;;
 133                 *)
 134                 ;;
 135         esac
 136 done < results
 137
 138 exitstatus=$?
 139 if [ $exitstatus = 0 ]; then
 140     echo "Test Status OK " $OPTION
 141 else
 142     echo "Test Status failed"
 143 fi












I think it's you blank line at 115 and --separate-output placed in the middle of your --checklist options

This seems to work fine for me:

whiptail \
    --title "checklist test Option 1"  \
    --separate-output \
    --checklist "Choose: " 20 78 3 \
    "John" "" on  \
    "Glen" "" off  \
    "Adam" "" off 2> results

Yes,

blank line causes problem , but I get a specific error now

 #pause 
  55  whiptail \
  56  --title "$1"  \
  57
  58  --checklist  "Choose: " 20 78 15 \
  59  "$2" "" on  \
  60  "Glen" "" off \
  61  "Adam" "" off
  62
 
/usr/bin/raspi-config-DEBUG.sh: 58: /usr/bin/raspi-config-DEBUG.sh: --checklist: not found
Test Status OK 
Press [Enter] key to continue..

Funny
all sample codes have same issue with mixing
these
--separate-output \ --checklist "Choose: " 20 78 3 \

I am not using --separate_output for now

Thanks for reply I'll mark it solved as soon as I solve the multiple
redirections issues. See below.

 2>results
  64  # redirected whiltail output "choice"  to results
  65  # where is choise  defined ? 
  66  # echo "HERE Option 1"
  67  #pause
  68  # TODO functions do not execute 
  69 while read choice
  70  do
  71          case $choice in
  72                  John) firstroutine
  73                  ;;
  74                  Glen) secondroutine
  75                  ;;
  76                  Adam) thirdroutine
  77                  ;;
  78                  *)
  79                  ;;
  80          esac
  81  done < results
  82  exitstatus=$?


------ Post updated at 09:03 PM ------

OK, I a

 "$4" "" off  2>choice
  83  echo "cat< choise get contents of choice file  print it" Y
  84  cat<choice     #TOK  here 
  85 echo "cat choise " 
  86 pause 
  87 echo "while read  file  choice "
  88 pause 
  89 while read choice
  90  do
  91          case $choice in
  92                  "$2") 
  93                         echo " first selection " 
  94                         firstroutine
  95                  ;;








------ Post updated at 09:20 PM ------

OK, here is an update.
I understand whiptail puts the selection result in file - in my case "choices".
I can check the contents with cat.

Now how do I check line 89?

It is not doing anything and I do not know how to verify its function.

 "$4" "" off  2>choices
  83  echo "cat< choise get contents of choice file  print it" Y
  84  cat<choices     #TOK  here 
  85 echo "cat choises " 
  86 pause 
  87 echo "while read  file  choices "
  88 pause 
  89 while read choices
  90  do
  91          case $choice in
  92                  "$2") 
  93                         echo " first selection " 
  94                         firstroutine
  95                  ;;
  96                  $3) secondroutine
  97                  ;;
  98                  $4) thirdroutine
  99                  ;;
 100                  *) 
 101                         echo " default selection " Y
 102                  ;;
 103          esac
 104  done < results
 105  exitstatus=$?
 106  if [ $exitstatus = 0 ]; then
 107      echo "Exit file read Test Status OK " $choice
 108  else
 109      echo "Exit Test Status failed"
 110  fi




on line 104 you are reading the contents of file results and whiptail output is being re-directed to file choices .

Note that this file name will cause issues if you have 2 processes running the script at the same time. Can I suggest you use /tmp to store the output file and give it a unique name eg using $$ for the process ID eg:

whiptail \
    --title "checklist test Option 1"  \
    --separate-output \
    --checklist "Choose: " 20 78 3 \
    "John" "" on  \
    "Glen" "" off  \
    "Adam" "" off 2> /tmp/wtout_$$

while read choice
do
    case $choice in
        John)
...
   esac
done < /tmp/wtout_$$

# clean up temp file
rm /tmp_wtout_$$
Moderator comments were removed during original forum migration.
1 Like

Whiptail produces output on stderr that indicates the interactions taken by the user. Your script works by redirecting this output to a file as hi-lighted below:

whiptail \
    --title "checklist test Option 1"  \
    --separate-output \
    --checklist "Choose: " 20 78 3 \
    "John" "" on  \
    "Glen" "" off  \
    "Adam" "" off 2> whiptail.out

Run the above code and make various choices in the dialog and then check the contents of the whiptail.out file.

So after the user has selected their preferences these need to be read in from the file and acted upon.

while read choice
do
    echo "Line read: $choice"
done < whiptail.out

The above code fragment loops for each line of the file whiptail.out setting the variable choice to the contents of the line.

You can take particular actions based on the file contents using something like a case statement within the while loop eg:

case $choice in
    John)
          echo "User selected John"
   ;;
   Glen)
         echo "User selected Glen"
   ;;
   *)
         echo "User selected $choice - which in neither John or Glen"
   ;;
esac

I have build a new function with limited scaffold.

It 'runs" with an error @ line 67, not sure if I should be concerned about the error at

this time.

I know how it suppose to work, the part I do not understand is

where does the file "choice" @ line 58 come from.

The selection is in "results" and I am reading "choice".

It just " does not compute ".

As written now I have deliberately deleted lines of code between lines 60 and 61 -

no selection from menu is implemented.

However

I do expect lines 60 and 61 to execute to indicate that the process went thru them.
THAT is the issue - if I add the " do /done" selection code it never gets processed and

I DO NOT understand why.

                         whiptail_DEBUG(){
   44 echo " whiptail_DEBUG  @ line $LINENO"
   45 echo "Passed parameter #1 is $1"
   46 echo "Passed parameter #2 is $2"
   47 echo "Passed parameter #3 is $3"
   48 echo "Passed parameter #4 is $4"
   49 whiptail \
   50 --title "$1" \
   51 --separate-output  \
   52 --checklist  "Choose: " 20 78 15 \
   53 "$2" "" on  \
   54 "$3" "" off \
   55 "$4" "" off
   56 2>results               selection goes here 



   57 echo "function whiptail_DEBUG  @ line $LINENO"
   58 while read choice                  why am I reading "choise" and not "results" ? 

   59 do
   60 echo "START do/ while read @ Line $LINENO read  $choice"
   61 echo "END   do/ while read @ Line $LINENO read  $choice"
   62 done < results
   63 echo "czech file results  @ Line $LINENO "
   64 if [ $exitstatus = 0 ]; then
   65 echo "TOK " $choice
   66 else
   67 echo "function status failed "
   68 fi
   69 echo "function  whiptail_DEBUG  @ line $LINENO"
   70 }
 

Results is the name of the file that contains the output from the whiptail command.

choice is a bash variable into which each line of the results file is read. Input is redirected from the file at line 62.

You can call the variable whatever you like get choice/line/result, the important distinction is that this is populated with each line from the results file one line at a time (try selecting more than one entry from the checklist and see how the file contains multiple lines).

Error at line 67 is because the variable exitstatus has not been assigned, you should assign exitstatus with exitstatus=$? following the whiptail command (ie after line 56 and before line 57).

I am really confused

49 whiptail \    50 --title "$1" \    51 --separate-output  \    52 --checklist  "Choose: " 20 78 15 \    53 "$2" "" on  \    54 "$3" "" off \    55 "$4" "" off    56 2>results   prints selection in "" here , '
if I select all three options they all print here in one line
all in ""  
49 whiptail \    50 --title "$1" \    51 --separate-output  \ line removed !!!!    52 --checklist  "Choose: " 20 78 15 \    53 "$2" "" on  \    54 "$3" "" off \    55 "$4" "" off    56 2>results   
if i remove line 51 
no more prints here !!!

Either way - I cannot retrieve selection using while do /done syntax!
The while do/done code block never executes.

I can understand NOT printing the "results" , don't need the print anyway.
But the while do/done not executing got me stumped.
Better luck tomorrow.

I guess cut and paste from quote is a no no, Tempted to delete whole mess,,,

Good night

------ Post updated 08-14-18 at 09:00 PM ------

Well I finally found the problem.

Being lazy I was trying to replace endless typing of commands while troubleshooting WiFi issue.

In the process i got cute and started passing parameters to functions.

Works fine if the parameter passed is single "string" with no spaces.

Such as "lsusb" is fine but "lsusb -v" can be passed as a parameter but not to --checklist AND using --separate-output ! It does makes sense since the "choice" selection cannot find a match when _separate-output present only part of the passed parameter for match.

What got me spinning in circles was when ALL choices where implemented the while do/ done part of the code was bypassed.

Now when I select parameter with spaces - such as "lsusb -v" I get "invalid option " - perfect!

As soon as I figure out how to pass command with options I'll post it here.

Here is current version of code and its output.

Option 3
TRACE whiptail_DEBUG_BASE   @ line 41
Passed parameter #1 is DEBUG USB 
Passed parameter #2 is lsusb
Passed parameter #3 is test parameter ! 
Passed parameter #4 is test parameter 2 
TOK  @ line 46
You chose lsusb 
Invalid entry 
Invalid entry 
Press [Enter] key to continue...^C


echo "Passed parameter #1 is $1"
  43  echo "Passed parameter #2 is $2"
  44  echo "Passed parameter #3 is $3"
  45  echo "Passed parameter #4 is $4"
  46  echo "TOK  @ line $LINENO"
  47 whiptail \
  48 --separate-output \
  49 --title "Test" \
  50 --checklist  "Choose:" 20 78 15 \
  51 "$2" "" on \
  52 "$3" "" off \
  53 "$3" "" off 2>results
  54 while read choice
  55 do
  56         case $choice in
  57                 $2) echo "You chose $2 "
  58                 ;;
  59                 $3) echo "You chose $3 "
  60                 ;;
  61                 $4) echo "You chose $4"
  62                 ;;
  63                 *)  echo "Invalid entry "
  64                 ;;
  65         esac
  66 done < results
  67 pause 
  68 }


Thanks for all the help I have received.
Cheers