0

I am having trouble with the following until loop in bash. I want a Zenity popup box to appear as long as the time values that are entered are outside of a valid range. The popup opens up fine. However it keeps on popping up whether valid data is entered or not (I have to exit the terminal to break out of the loop).

#!/bin/bash

startt=30:20 finisht=12:45

until [[ $startt =~ [0-2][0-9]:[0-5][0-9] ]] && [[ $finisht =~ [0-2][0-9]:[0-5][0-9] ]]; do var2="$(zenity --forms --title="start_time and/or finish_time are incorrect" --text "Add a start_time and a finish_time" --separator=","
--add-entry="WARNING! Something went wrong. Please enter a valid start_time: "
--add-entry="WARNING! Something went wrong. Please enter a valid finish_time: ")" done

Much appreciated if someone can help with this.

fpmurphy
  • 1,661

1 Answers1

1

I know now what has gone wrong here. First there is no connection between var2 and the input data (startt & finisht). So there is a breakdown in logic. The Zenity part actually enters the input data that is then saved to the variable var2. The variable var2 essentially contains two variables separated by a comma (30:20,12:45). To separate them I have created two variables b1 (30:20) and b2 (12:45) via awk that are then passed to the until condition. This now says that UNTIL b1 AND b2 are WITHIN the specified condition keep opening the Zenity input boxes:

#!/bin/bash

until [[ $b1 =~ [0-2][0-9]:[0-5][0-9] ]] && [[ $b2 =~ [0-2][0-9]:[0-5][0-9] ]]; do var2="$(zenity --forms --title="start_time and/or finish_time are incorrect" --text "Add a start_time and a finish_time" --separator=","
--add-entry="WARNING! Something went wrong. Please enter a valid start_time: "
--add-entry="WARNING! Something went wrong. Please enter a valid finish_time: ")" b1=$(echo $var2 | awk -F, -v OFS=, '{print $1}') b2=$(echo $var2 | awk -F, -v OFS=, '{print $2}') done

However this does not answer the following. What I really want is that the zenity popup boxes open so I can re-enter data IF startf OR finishf are NOT within the valid range UNTIL the correct data for b1 and b2 is entered into the Zenity boxes. So the final code should be something like this:

input=30:20,12:45

startt=$(echo $input | awk -F, -v OFS=, '{print $1}') finisht=$(echo $input | awk -F, -v OFS=, '{print $2}')

if [[ ! $startt =~ [0-2][0-9]:[0-5][0-9] ]] || [[ ! $finisht =~ [0-2][0-9]:[0-5][0-9] ]]; then until [[ $b1 =~ [0-2][0-9]:[0-5][0-9] ]] && [[ $b2 =~ [0-2][0-9]:[0-5][0-9] ]]; do

                            var2="$(zenity --forms --title="start_time and/or finish_time are incorrect" --text "Add a start_time and a finish_time"  --separator="," \
                                  --add-entry="WARNING! Something went wrong. Please enter a valid start_time: " \
                                  --add-entry="WARNING! Something went wrong. Please enter a valid finish_time: ")"

                            b1=$(echo $var2 | awk -F, -v  OFS=, '{print $1}')
                            b2=$(echo $var2 | awk -F, -v  OFS=, '{print $2}')

                            done

fi

echo $var2

Variable var2 can now be used for further validation tests and contains data that is within the specified range.