Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bash script that I want to do something like

 read one_thing
 read another_thing

And then it runs some code to see if another_thing is available or already taken, and if it is taken, it should warn the user and again run

read another_thing

so we can have a new value, leaving one_thing undisturbed. Since there's no goto in bash, I'm wondering how to do that. My best guess so far is that maybe I should wrap read another_thing inside a function, so that if needed, it will just call itself, but I feel there must be a “cleaner” way to do it. I'm looking for suggestions on efficient ways to do it.

share|improve this question
3  
This question would be better suited to stackexchange. Anyway, just enclose your read statement in a while loop, something like while $a==0 and change $a to 1 when another_thing has been read correctly. – terdon Jan 7 '13 at 17:57
    
Provide some real code so we can get a better idea of what you are trying to do. – JamesBarnett Jan 7 '13 at 18:21
    
Scripts and some scripting language questions are ok on SuperUser however most programming issues are better solved using StackOverflow. – KronoS Jan 7 '13 at 19:13
    
This question may stay here. We've always considered bash scripts on topic, since they're mostly used by power users. – slhck Jan 7 '13 at 19:31
up vote 1 down vote accepted

If terdon adds his suggestion as an answer, I'll mark that one as the solution.

If not, here's an overview on how to implement it.

#!/bin/bash

read -p "Input something >" one_thing

condition_to_check="False"
while [[ ${condition_to_check} == "False" ]]; do
    read -p "Input something else >" another_thing

    # Write some code to check if the requirements are met
    # Let's say in this case they are when the variable “thing_to_work” equals “abcde”

    if [[ ${thing_to_work} == "abcde" ]]; then
        condition_to_check="True"
        # This means the rest of the script can go on
    else
        echo "The requirements were not met, so the loop will start again"
    fi
done
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .