Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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 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.


    wUsername=`zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Kasutajanimi:"`
#if [ $? -ne 0 ]; then
#       exit 1
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

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
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
    zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Kasutajanimi:" 
## Define a function that launches the zenity password dialog
    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
## 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

## 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
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 '14 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 '14 at 10:30
Works like a charm :) – mYzk May 15 '14 at 10:31

You can try something like this:

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

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

    # 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

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 '14 at 10:31

Your Answer


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.