Find the answer to your Linux question:
Results 1 to 7 of 7
hi guys, i am having two small issues with a function i have made. sorry if it is a mess, i am still learning bash. the first is calling the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    53

    [SOLVED] else & return values.


    hi guys,

    i am having two small issues with a function i have made.

    sorry if it is a mess, i am still learning bash.



    the first is calling the nonpersistssh function (second line) and assigning the return value to nonpersistdiag. the function returns 1, but nonpersistdiag seems to only contain 0. i am unsure on how to proceed.

    the second problem is the nested else clause on line 10. it is a syntactical error. how would i declare it correctly?

    Code:
    function endsession(){
    	nonpersistdiag=$[nonpersistssh]	# a function that returns an exit code
    	sudo /etc/init.d/ssh stop; sshdiag=$?
    	sudo ufw deny $port; ufwdiag=$?
    	if [ $nonpersistdiag -gt 0 ] || [ $sshdiag -gt 0 ] || [ $ufwdiag -gt 0 ]; then
    		if [ $nonpersistdiag -gt 0 ]; then zenity --info --text="there was a problem with ensuring a non-persistent ssh environment"; fi
    		if [ $sshdiag -gt 0 ]; then zenity --info --text="there was a problem stopping sshd"; fi
    		if [ $ufwdiag -gt 0 ]; then zenity --info --text="there was a problem removing firewall rule"; fi
    		else zenity --info --text="There was an unknown problem.\n your computer may be insecure. restarting should fix it."
    	else
    		zenity --info --text="session closed, your computer is safe"
    	fi
    
    }
    thank you.

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Your syntax for the first one is incorrect. The $[...] syntax allows you to do math in Bash. You want $(...) syntax, which executes the given command and stores the output.

    For your second, your syntax is a bit off. You get to have a single "if", any number of "elif"s, a single "else", and ending with a "fi". I don't know exactly what you want your logic to look like, but I am guessing that you want:

    Code:
    if [ $nonpersistdiag -gt 0 ] || [ $sshdiag -gt 0 ] || [ $ufwdiag -gt 0 ]; then
        if [ $nonpersistdiag -gt 0 ]; then zenity --info --text="there was a problem with ensuring a non-persistent ssh environment"
        elif [ $sshdiag -gt 0 ]; then zenity --info --text="there was a problem stopping sshd"
        elif [ $ufwdiag -gt 0 ]; then zenity --info --text="there was a problem removing firewall rule"
        else zenity --info --text="There was an unknown problem.\n your computer may be insecure. restarting should fix it."; fi
    else
        zenity --info --text="session closed, your computer is safe"
    fi

  3. #3
    Just Joined!
    Join Date
    Dec 2009
    Posts
    53
    Thanks, what i was trying to achieve was a number of clauses that could be executed rather than just one.

    for example, if two of the conditions were met, then i was hoping that two if statements would be executed. afaik elifs only execute if the first if condition is false.

    i guess i will have to rewrite that function to

    Code:
    if[condition]
    code
    fi
    if[condition]
    code
    fi

    thanks.

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Correct. You can either have a single tree of statements, only one of which will be executed, or multiple separate statements.

    In the example you gave, the inner else clause will never be executed (to enter the if, one of three conditions must hold, and you check all three explicitly), so it should be simple to just make separate if statements with no else.

  5. #5
    Just Joined!
    Join Date
    Dec 2009
    Posts
    53
    ty, yes i rewrote the function, but i am still having issues assigning a return value from a function to a variable.


    Code:
    function nonpersistssh() {
    .....
    
    return 1;
    }
    
    nonpersistdiag=$(nonpersistssh)
    
    echo $nonpersistdiag
    $nonpersistdiag gives a null value.


    thanks.

  6. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    So in Bash, functions don't really have a concept of a "return value".

    The $(...) syntax means "run this command and return its output". So in your case, nonpersistdiag would only have a value if the nonpersistssh function actually wrote any output, which it probably doesn't.

    For return values, they are basically treated like exit values. So this would work:
    Code:
    function foo {
        return 1;
    }
    
    foo
    value=$?
    echo $value # prints 1

  7. #7
    Just Joined!
    Join Date
    Dec 2009
    Posts
    53
    Code:
    For return values, they are basically treated like exit values.
    aha, ty mate, it works perfectly now!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •