artfulbodger
Technical User
Hello all...
I recently took up bash to automate some repetitive tasks on my servers. I want to use the value from a function in an if/then conditional but I can't seem to get it to work. Here is what I have
function continue () {
This function asks the user if he wants to continue script execution.
It returns a 0 (zero) if "y" or "Y" is selected, a 1 (one) if "n" or
"N" is selected and exits the script if the user fails to enter a
valid choice after 3 attempts.
}
THE FOLLOWING CODE WORKS:
continue
if [ "$?" -ne "0" ]
then
exit
fi
I have tested all three conditions and it works flawlessly. What I would like to do is evaluate the result of the function within the conditional test. Something like this :
if [ "${ continue }" -ne "0" ]
then
exit
fi
THAT CODE DOES NOT WORK.
I know it's small, but I like to be as compact as possible. I also want to know who to do this kind of thing for future reference.
If you could also give me a pointer as to "why" it would be really appreciated. There is a lot on the web, but people's examples and explanations read like spaghetti.
Thanks in advance.
-----
Allan D. Reed
I recently took up bash to automate some repetitive tasks on my servers. I want to use the value from a function in an if/then conditional but I can't seem to get it to work. Here is what I have
function continue () {
This function asks the user if he wants to continue script execution.
It returns a 0 (zero) if "y" or "Y" is selected, a 1 (one) if "n" or
"N" is selected and exits the script if the user fails to enter a
valid choice after 3 attempts.
}
THE FOLLOWING CODE WORKS:
continue
if [ "$?" -ne "0" ]
then
exit
fi
I have tested all three conditions and it works flawlessly. What I would like to do is evaluate the result of the function within the conditional test. Something like this :
if [ "${ continue }" -ne "0" ]
then
exit
fi
THAT CODE DOES NOT WORK.
I know it's small, but I like to be as compact as possible. I also want to know who to do this kind of thing for future reference.
If you could also give me a pointer as to "why" it would be really appreciated. There is a lot on the web, but people's examples and explanations read like spaghetti.
Thanks in advance.
-----
Allan D. Reed