Question

I'm quite new to bash scripting and i've been working on a small bash file that can do a few things for me. First of all, i'm calling this from a C function using system() and i'd like the script to return a value (1 for error, 0 for OK). Is this possible?

int kinit() {
    int err = system("/home/bluemoon/Desktop/GSSExample/kinitScript.sh");
}

Second, using Zenity, i managed to create a pop up window to insert user/password. Now, according to what the user does, multiple things should happen. If he closes the window or clicks "cancel", nothing should happen. If he clicks "OK", then i should check the input (for empty text boxes or something).

Assuming a correct input, i will use Expect to run a kinit program (it's a promt related with Kerberos) and log in. Now, if the password is correct, the prompt closes and the script should return 0. If it's not, the prompt will show something like "Kinit: user not found". I wanted to, in any case of error (closing window, clicking cancel or wrong credentials) return 1 in my script and return 0 on success.

#!/bin/bash
noText=""
ENTRY=`zenity --password --username`
case $? in
         0)
        USER=`echo $ENTRY | cut -d'|' -f1`
        PW=`echo $ENTRY | cut -d'|' -f2`

        if [ "$USER"!="$noText" -a "$PW"!="$noText" ] 
        then
            /usr/bin/expect -c 'spawn kinit '`echo $USER`'; expect "Password for '`echo $USER`':" { send "'`echo $PW`'\r" }; interact'
        fi
        ;;
         1)
                echo "Stop login.";;
        -1)
                echo "An unexpected error has occurred.";;
esac

My if isn't working properly, the expect command is always run. Cancelling or closing the Zenity window also always lead to case "0". I've also tried to return a variable but it says i can only return vars from inside functions?

Well, if anyone could give me some pointers, i'd appreciate it. Dave

Was it helpful?

Solution

i'd like the script to return a value

Sure, just use exit in appropriate places

exit: exit [n]
    Exit the shell.

    Exits the shell with a status of N.  If N is omitted, the exit status
    is that of the last command executed.

My if isn't working properly, the expect command is always run.

if [ "$USER" != "$noText" -a "$PW" != "$noText" ]
#           ^  ^                  ^  ^
#           |  |                  |  |
#           \----- notice spaces ----/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top