0

I have a script that mounts a windows share and I worked on it to learn bash scripting.

The problem I am facing is that there is a result written in zenity as once the share is mounted it gives out OK when it fails to mount it gives out FAIL.

Now I want to ask user his password one more time if the output was FAIL so if she/he had a mistake in the password she/he can rewrite it.

Input

## define a function that launched the zenity username dialog
get_password(){
    zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Password:" --hide-text
}

# attempt to get the password and exit if cancel was pressed
wPassword=$(get_password) || exit

# if the username is empty or matches only whitespace.
while [ "$(expr match "$wPassword" '.')" -lt "1" ]; do
    zenity --error --title="Error in password!" --text="Please check your password! Password field can not be empty!" || exit
    wPassword=$(get_password) || exit
done

Output:

# show if mounting was OK or failed
if [ $? -eq 0 ]; then
        zenity --info --title="Mounting public share succeeded!" --text="Location Documents/Shares/public!"
else
        zenity --error --title="Mounting public did not succeed!" --text="Please contact system administrator!"
fi

So I need the script to rerun input if output was a fail. I hope you understand what I need.

2
  • You need a while loop, and break upon success Commented May 22, 2014 at 13:24
  • Can you explain with a simple example? Commented May 22, 2014 at 13:28

1 Answer 1

1

I think what you're after is something like:

while wPassword=$(get_password)
do
    if mount …options for mount…
    then
        zenity --info --title="Mounting public share succeeded!" \
               --text="Location Documents/Shares/public!"
        break
    else
        zenity --error --title="Mounting public did not succeed!" \
               --text="Please contact system administrator!"
    fi
done

This runs the get_password function and saves the output; if the get_password function returns with a non-zero status, the loop will terminate. In the body of the loop, it runs the mount command and if that succeeds, reports success and breaks the loop. If it fails, it gives an error message (with a no longer entirely appropriate message), and then goes back to the loop to read a new password.

… but the problem is that I have save password function in the script and I need that to be started before it gives a success status.

Multiple options are available:

while wPassword=$(get_password)
do
    if ! password_saver "$wPassword"
    then break    # Password saver reported error, presumably
    fi
    if mount …options for mount…
    …

Or:

while wPassword=$(get_password) &&
      password_saver "$wPassword"
do
    if mount …options for mount…
    …

If the password saver only indicates an error rather than reporting it, then you'll need to report the problem in your code; that would make the first option better. You could replace the break with continue to go around the loop again. If 'stop loop on password saver error' is correct, then the second is more succinct.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes this works, but the problem is that I have save password function in the script and I need that to be started before it gives a success status. Example: while .... then save after give success else error
So after the do you run the save operation and then the mount. It's up to you whether you error check the save status.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.