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
EDIT: First version didn't do the input loop, sorry - fixed now.
----------
Hi,
Would something like this do the trick for you ?
#!/bin/bash
validinput=0
while [ "$validinput" != "1" ]
do
echo "Please enter an RFC number"
read rfcnumber
case $rfcnumber in
"")
echo Either type in your RFC number, or enter N/A if one is unavailable.
;;
N/A)
echo You have indicated an RFC number is not available.
validinput=1
;;
*)
if echo "$rfcnumber" | /bin/grep -E "^[0-9]{5,}$" >/dev/null 2>/dev/null
then
echo Valid RFC number entered.
validinput=1
else
echo "Invalid RFC number entered"
echo "Please enter a minimum of five digits only, or N/A if you don't have one"
fi
;;
esac
done
echo "You provided the following valid input:"
echo $rfcnumber
(You could keep using dialog
here, I just used the builtin read
for my own convenience when writing this).
Here's a sample session, showing it ony accepting input that conists of solely digits, and a minimum of five of them. It will keep prompting the user to enter an RFC until a valid one is provided.
$ ./script.sh
Please enter an RFC number
Either type in your RFC number, or enter N/A if one is unavailable.
Please enter an RFC number
N/A
You have indicated an RFC number is not available.
You provided the following valid input:
N/A
$ ./script.sh
Please enter an RFC number
123
Invalid RFC number entered
Please enter a minimum of five digits only, or N/A if you don't have one
Please enter an RFC number
ABC
Invalid RFC number entered
Please enter a minimum of five digits only, or N/A if you don't have one
Please enter an RFC number
a12345
Invalid RFC number entered
Please enter a minimum of five digits only, or N/A if you don't have one
Please enter an RFC number
12345a
Invalid RFC number entered
Please enter a minimum of five digits only, or N/A if you don't have one
Please enter an RFC number
1234-567
Invalid RFC number entered
Please enter a minimum of five digits only, or N/A if you don't have one
Please enter an RFC number
123456
Valid RFC number entered.
You provided the following valid input:
123456
Hope this helps. If this isn't quite right let me know and I'll see if I can tweak it.
Perhaps something like this, you'll get the idea
trap clear EXIT
DIALOG=${DIALOG=dialog}
while :
do
rfcval=$($DIALOG --title "RFC NUMBER" --clear \
--inputbox "Please enter an RFC Number" 16 17 2>&1 >/dev/tty)
retval=$?
case $retval in
0)
case $rfcval in
N/A|n/a)
exit 0 ;;
[!0-9])
: ;;
*[0-9][0-9][0-9][0-9][0-9]*)
echo "RFC Number: $rfcval" >> $accessfile
exit 0;;
esac ;;
1)
exit 1;;
255)
rm -f "$accessfile" "$tempfile" "$sitefile"
exit 1 ;;
esac
done
1 Like
Perfect...Thank you.
---------- Post updated at 11:40 PM ---------- Previous update was at 09:12 PM ----------
One more issue I'm encountering is trying to limit the regex a bit more.
I want to require a minimum of 15 characters but this code below does not work. The loop continues.
I've tried all of the following to no avail, however if I test these in an if statement they work.
*^[a-zA-Z0-9_]{15,}$)
*[a-zA-Z0-9_]{15,})
*[a-zA-Z0-9_]\{15,\})
*^[a-zA-Z0-9_]\{15,\}$)
case $retval in
0)
case $reasonval in
*^[a-zA-Z0-9_]{15,}$*)
echo Reason for System Access: $reasonval >> $accessfile
exit 0 ;;
esac ;;
Hi, case statements cannot use regular expressions, they use pattern matches, which have a different syntax.
Since there is no specification in this thread of what shell is going to be used, this is the best option, because a case statement works in any POSIX shell or even Bourne shell.
If your shell supports regex (modern Bash, ksh93, zsh) then you could do this (using the original example):
trap clear EXIT
DIALOG=${DIALOG=dialog}
while :
do
rfcval=$($DIALOG --title "RFC NUMBER" --clear \
--inputbox "Please enter an RFC Number" 16 17 2>&1 >/dev/tty)
retval=$?
case $retval in
0)
if [[ $rfcval =~ ^[0-9]{5,}$ ]]; then
echo "RFC Number: $rfcval" >> $accessfile
break
elif [[ $rfcval =~ ^(N/A|n/a)$ ]]; then
break
fi ;;
1)
exit 1;;
255)
rm -f "$accessfile" "$tempfile" "$sitefile"
exit 1 ;;
esac
done
exit 0
-- edit --
Just noticed the thread title mentions bash, oh well ...
1 Like