cstsang
1
Dear all
Would anyone tell me how to prevent user from input non asterisk(i.e. *) character via keyboard?
#!/bin/ksh
targetHour=-1
while [[ ($targetHour != "*") && (($targetHour -lt 0)||($targetHour -gt 23)) ]]
do
echo "Please input target hour":
read targetHour
done
When I execute the above coding, and then input a "j", it return the following message:
The specified number is not valid for this command.
Thank you very much
rdrtx1
2
try adding a line after read line:
echo "$targetHour" | grep -q "^[0-9]+$" || { echo "Invalid entry." ; targetHour=-1 ; }
cstsang
3
My requirement is only accept user input 0 ~ 23 and * ( not wildcard, asterisk only ).
Would you tell me how to change the code for archiving my goal?
pamu
4
Try
targetHour=-1
while [[ $targetHour != "*" && ( $targetHour -lt 0 || $targetHour -gt 23 || $(echo $targetHour | grep "[a-zA-Z]" -c) != 0 ) ]]
do
echo "Please input target hour"
read targetHour
done