Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2192  / 2 Years ago, sun, april 10, 2022, 9:12:02

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.


More From » bash

 Answers
4

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

[#25209] Monday, April 11, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oredoise

Total Points: 66
Total Questions: 116
Total Answers: 111

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;