You are not logged in.

#1 2017-03-18 15:27:38

woodson2
Member
Registered: 2014-06-30
Posts: 5

Require user inout(non-empty string) in bash "dialog" box

Hello.

Any help would be greatly appreciated.

Right now I have the following input box that works fine and well, however I would like to wrap this is a loop that requires input. Right now the script will happily continue on if the user just hits enter. I'd like to require a minimum of a 5 digit number or n/a or N/A as the only viable options otherwise you should get prompted to re-enter information.

DIALOG=${DIALOG=dialog}
$DIALOG --title "RFC NUMBER" --clear \
        --inputbox "Please enter an RFC Number" 16 17 2> $rfcfile

retval=$?
rfcval=`cat $rfcfile`

case $retval in
  0)
    echo RFC Number: "$rfcval" >> $accessfile;;
  1)
    exit 1;;
  255)
    rm -rf $accessfile && rm -rf $tempfile && rm -rf $rfcfile && rm -rf $sitefile && exit 1;;
  esac

Offline

#2 2017-03-18 16:06:14

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,452
Website

Re: Require user inout(non-empty string) in bash "dialog" box

You already described your solution.  Wrap that in a loop and test whether your conditions are met to exit the loop.  Which part of that do you need help with?  To match your conditions you could just do something like the following:

until [[ "$input" =~ ^([0-9]{5}|[Nn]\/[Aa])$ ]]; do
   # read input from dialog
done

I did take some allowance with your criteria, as this will allow mixed-case N/As include N/a and n/A, that'd be easy to change though if you wanted.  Alternatively, if you didn't care about case at all you could simplify it my removing the character classes and using `shopt -s nocasematch` before the loop.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2017-05-24 15:39:23

adam_danischewski
Banned
Registered: 2016-05-06
Posts: 23
Website

Re: Require user inout(non-empty string) in bash "dialog" box

Like Trilby said, you just need to wrap a loop around it, here is a modified version, just hack it back to your logic - it's a little bit dangerous to post example code that has rm's in it.

#!/bin/bash 
DIALOG=${DIALOG=dialog}
rfcfile=/tmp/rfcfile.txt
while :; do 
$DIALOG --title "RFC NUMBER" --clear \
        --inputbox "Please enter five digit RFC Number:" 16 17 2> "${rfcfile}"
retval=$?
((${retval})) && break ## Cancel button was pressed, retval=1. 
rfcval=$(< "${rfcfile}") ## Same as $(cat "${rfcfile}"). 
grep -Eiq "^(([0-9]{5})|(n/a))$" <<< "${rfcval}" && break || ${DIALOG} --clear --msgbox "*** Error: Input of 5 digits is required" 5 50
done
echo "rfcfile is ${rfcfile}, retval is ${retval}"
case ${retval} in
  0)
    echo RFC Number: "${rfcval}";;
  1)
    exit 1;;
  255)
    echo "remove some stuff" 
esac
exit 0

Edit note: I moved the break logic to the same test where the error dialog is displayed that way it is only executed once. Cleaned up the code a bit.

Last edited by adam_danischewski (2017-05-25 02:19:26)

Offline

#4 2017-05-24 23:28:40

glyons
Member
From: Europa
Registered: 2016-10-14
Posts: 37

Re: Require user inout(non-empty string) in bash "dialog" box

In addition to the loop

You can rename the OK button "RFC number" and then an add extra button named "N/A" to make it easier on user.
Add
--ok-label "RFC" --extra-button  --extra-label "n/a"
to the dialog options

and to read the extra option in the case statement with code 3
      3)
      echo "extra pressed."
      ;;

Offline

Board footer

Powered by FluxBB