Script de login bash com zenity para montagem CIFS

3

Estou escrevendo um script de montagem para o compartilhamento do Windows para ser executado após o login. Eu fiz isso com bash e zenity e funciona, mas agora eu preciso fazê-lo melhor, então se o campo username e password estiverem vazios, retorne à entrada.

Exemplo

    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

Então eu quero que este script traga o usuário de volta para a entrada se Kasutajanimi também é conhecido como nome de usuário ou Parool está vazia. Mesmo se o espaço for pressionado.

Eu pesquisei o poderoso Google por ele e sei que posso fazê-lo de alguma forma com o retorno.

    
por mYzk 15.05.2014 / 08:39

2 respostas

4

Eu faria assim:

#!/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
    
por terdon 15.05.2014 / 11:26
3

Você pode tentar algo assim:

# 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

e o mesmo para a entrada de senha.

    
por TuKsn 15.05.2014 / 10:36