1

Is there any way for me to loop through a specific part of a script until a specific variable is set?

What I mean by this is, I have something like this:

#!/bin/bash

# Check if sudo
if [ $UID -ne 0 ]; then
        echo "You have to run this as sudo" 1>&2
        exit 1
fi

# Get the date for the check
read -p "Please input a date, format 'Jan 12': " chosendate

# Get the time for the check
read -p "Please input the time, format '13:55', leave blank for no time: " chosentime

# Get last results based on input
gotresults=$(last |grep "$chosendate $chosentime" |awk '{print $1" " $5" " $6" " $7" " $9}')

if [[ $(echo "$gotresults"|wc -l) -ne 1 ]]; then
        echo "There are multiple entries corresponding to your input"
        echo
        echo "$gotresults"
        echo
read -p "Please select which entry you desire by typing in the time: " chosentime
        echo "$gotresults" |grep $chosentime
else
        echo "$gotresults"
fi

I want to replace this with something like this:

#!/bin/bash

# Check if sudo
if [ $UID -ne 0 ]; then
        echo "You have to run this as sudo" 1>&2
        exit 1
fi

**FLAG1**
# Get the date for the check
read -p "Please input a date, format 'Jan 12': " chosendate

# Get the time for the check
read -p "Please input the time, format '13:55', leave blank for no time: " chosentime

# Get last results based on input
gotresults=$(last |grep "$chosendate $chosentime" |awk '{print $1" " $5" " $6" " $7" " $9}')

if [[ $(echo "$gotresults"|wc -l) -ne 1 ]]; then
        echo "There are multiple entries corresponding to your input"
        echo
        echo "$gotresults"
        echo
        echo "Please select a date/time that only returns one value"
        **GO TO FLAG1**

else
        echo "$gotresults"
fi

This way I could loop through that one part (reading the user input and then doing the operation) until based on the user input there's only one value returned.

I think this is possible with a "for" loop, but I would find it easier if such a thing would exist (I think this system was implemented in some kind of program I used to use).

The reason I would prefer the FLAG and GO TO FLAG system that I mentioned, is because I could go back to a flag whenever I want throughout the script, and I would have higher control over the flow of the script. So I could have FLAG1 somewhere, and then GO TO FLAG1 in multiple parts of my script, not just one, which would be difficult to do with for loops.

  • 2
    I would use functions for such a task. – eblock Oct 22 '18 at 13:17
  • Yes I just thought of it as well :) but how come bash doesn't have this concept of GO TO? I'm told c++ has it – user317076 Oct 22 '18 at 13:23
  • Go to statement considered harmful, a concept as valid nowadays as when the paper was written in 1968 :-) – nohillside Oct 22 '18 at 13:25
  • 2
    What do you want to achieve with that script ? – pLumo Oct 22 '18 at 13:49
1

With just the script from the question I probably would do something like (ignoring other potential improvements for now)

 have_results=0
 while [[ $have_results -eq 0 ]]; do
     read -p "Please input a date, format 'Jan 12': " chosendate
     read -p "Please input the time, format '13:55', leave blank for no time: " chosentime

     gotresults=$(last |grep "$chosendate $chosentime" |awk '{print $1" " $5" " $6" " $7" " $9}')

    if [[ $(echo "$gotresults"|wc -l) -ne 1 ]]; then
        echo "Please select a date/time that only returns one value"
    else
        have_results=1
    fi
done
  • Thanks for the input! But I'd specifically try to avoid for or while loops – user317076 Oct 22 '18 at 13:25
  • 3
    @user317076 You can't have loops without loops :-) And your question asks for a loop. – nohillside Oct 22 '18 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.