Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am making a script (for educational purposes) but I am kind of stuck here.. The program asks for an existing filename and checks if the filename actually exists. If it doesn't the loop repeats itself until you fill in a file that does exist. So far so good! But when you enter a filename that does exist I want the script to enter the next while loop, to enter the path file.. But it doesn't continue. How can I make enter the next while statement???

clear

echo "Your filesystem is threatned, files should be moved in order to guarantee safety!!!!"

while read FILE

do

        if [ -f $FILE ];
                then
                        echo "File is safe to secure"
                else
                        echo "Too late, we lost the file, safe another!"
fi

done


echo "Time is running out, we must secure this file inmidiately, quick give me a safe location!"



while read PATH

do

if [ -d $PATH ] && [ -f $FILE ];

        then
                echo "the location is secure! Move the file!"
        else
                echo "Either the file or the safehouse is corrupt, quick try again!"


fi

done
share|improve this question
    
You are never giving it any reason to leave the first while loop. The if statement an the wile loop are separate. The first while loop goes on as long as someone gives it inputs. You need the wile to check for a variable which is set in the if statement and to put the read inside the while. –  Julian Stirling Jan 4 at 11:37
    
Just use the break statement in the true branches of the if. –  falconer Jan 4 at 11:41
1  
Also, you should use "$FILE" in double quotes in case the file name contained special characters like spaces, or if the user just pressed Enter without giving any file name. –  choroba Jan 4 at 15:15
    
Do not use uppercase variable names. You risk overriding environment variables and special shell variables. In fact your script is doing just that by overriding the special PATH variable; the script will fail horribly after read PATH. So use file and path instead of FILE and PATH. –  geirha Jan 8 at 12:19

1 Answer 1

This works:

clear

echo "Your filesystem is threatned, files should be moved in order to guarantee safety!!!!"

while read file; do
    if [[ -f "$file" ]]; then
        echo "File is safe to secure"
        break
    else
        echo "Too late, we lost the file, safe another!"
    fi
done

echo "Time is running out, we must secure this file inmidiately, quick give me a safe location!"

while read path; do
    if [ -d "$path" ]; then
        echo "the location is secure! Move the file!"
        break
    else
        echo "Either the file or the safehouse is corrupt, quick try again!"
    fi
done

You need to add break statements to exit the loop when a valid file/path is found. Also, don't use CAPITALS for variable names.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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