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 writing a mount script for Windows share to run after login. I have done it with bash and zenity and it works, but now I need to make it better so if username field and password field are empty then return to input.

Example

    wUsername=`zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Kasutajanimi:"`
#if [ $? -ne 0 ]; then
#       exit 1
#fi
if [ -z "$wUsername" ]; then
        zenity --error --title="Viga kasutajanimes!" --text="Palun sisestage oma kasutajanimi"


# get the windows password
wPassword=`zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Parool:" --hide-text`
if [ $? -ne 0 ]; then
        exit 1
fi

So I want this script to trow user back to input if Kasutajanimi aka username or Parool aka password is empty. Even if space is pressed.

I have searched the all mighty Google for it and I know I can do it with return somehow.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

I would do it like this:

#!/usr/bin/env bash

## Define a function that launches the zenity username dialog
get_username(){
    zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Kasutajanimi:" 
}
## Define a function that launches the zenity password dialog
get_password(){
    zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Parool:" --hide-text
}

## Attempt to get the username and exit if cancel was pressed.
wUsername=$(get_username) || exit

## If the username is empty or matches only whitespace.
## See http://www.tldp.org/LDP/abs/html/string-manipulation.html
## for an explanation of this syntax. The . means any non-space
## character so when this is less than 1, the username is empty
## or just whitespace. Since this is a while loop, the process
## will be repeated until the username is correctly submitted.
while [ "$(expr match "$wUsername" '.')" -lt "1" ]; do
    zenity --error --title="Viga kasutajanimes!" --text="Palun sisestage oma kasutajanimi"
    wUsername=$(get_username) || exit
done

## Same as the previous loop but for the password. Sorry if
## the message is wrong, I don't speak this language :)
wPassword=$(get_password) || exit

while [ "$(expr match "$wPassword" '.')" -lt "1" ]; do
    zenity --error --title="Viga Parool!" --text="Palun sisestage oma Parool"
    wPassword=$(get_password) || exit
done
share|improve this answer
    
But if the user wants to press Cancel it does not let him cancel the login after I tested empty username or password cancel button does not work. Gives me the zenity error. Would it work if I added else exit to while loop? –  mYzk May 15 at 10:24
    
@user2021157 yes, sorry, I fixed it now. See updated answer, you do indeed need the || exit inside the loop as well. –  terdon May 15 at 10:30
    
Works like a charm :) –  mYzk May 15 at 10:31

You can try something like this:

# ask for username
while true # start infinity loop
do
    wUsername=`zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Kasutajanimi:"`

    # user abort
    if [ $? -ne 0 ]; then
          exit 0
    fi

    # remove spaces
    wUsername=$( echo "$wUsername" | tr -d ' ' )

    # check user input
    if [ -z "$wUsername" ]; then
        # user input is empty -> throw error and continue the loop
        zenity --error --title="Viga kasutajanimes!" --text="Palun sisestage oma kasutajanimi"  
    else # user input is not empty 
        break # leave loop
    fi
done

and the same for the password input.

share|improve this answer
    
This is good as well, but terdon's one is more suitable for me, but this works as well. :) –  mYzk May 15 at 10:31

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.