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 ...
- 10-12-2010 #1Just 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?
thank you.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 }
- 10-12-2010 #2
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" fiDISTRO=Arch
Registered Linux User #388732
- 10-12-2010 #3Just 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.
- 10-13-2010 #4
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.DISTRO=Arch
Registered Linux User #388732
- 10-13-2010 #5Just 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.
$nonpersistdiag gives a null value.Code:function nonpersistssh() { ..... return 1; } nonpersistdiag=$(nonpersistssh) echo $nonpersistdiag
thanks.
- 10-14-2010 #6
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 1DISTRO=Arch
Registered Linux User #388732
- 10-14-2010 #7Just Joined!
- Join Date
- Dec 2009
- Posts
- 53
aha, ty mate, it works perfectly now!Code:For return values, they are basically treated like exit values.


