Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If/Then Condition Based on Value Returned From a Function

Status
Not open for further replies.

artfulbodger

Technical User
Jan 3, 2005
109
US
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
 
Try this...

Code:
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.
}

continue

STATUS=$?

if [ "${STATUS}" -ne "0" ]
then
exit
fi

 
Thanks, however that is basically the same thing as my original, except now, another variable is being created.

I was hoping to do it all within the if/then statement. I want to evaluate the result of an operation:

Example Pseudocode:
if (2 + 4) > 5 then do something.

Instead of:
Let result = 2 + 4
if result > 5 then do something.

One line is better than two




-----

Allan D. Reed
 
Ok...

I think I have solved this problem. The trick is to use no brackets or quotes on the if/then statement:

function continue() {
Return 0 for a Y
Return 1 for a N

}

if continue
then
echo "This is the YES Result"
else
echo "This is the NO Result"
fi


If there is a better way to do this or if I am doing it wrong, I would love to know so I can fix my script. I hope this helps other newbies stuck on this issue.



-----

Allan D. Reed
 
I was able to get my code working. I have posted it in case someone could make use of it. And if there is a more efficient way of making this work, please let me know.

Bash:
#! /bin/bash
#
#
E_INVALIDARGUMENT=22							# Add to global variables

# ---- Continue Function  -------------------
# 
# Takes an optional variable "N" to change the default prompt/action if nothing 
# is entered by the user.  Using the N option assumes that operation is not
# to be continued.


function continue() {						
	local -i try=3						
	local choice
	local default="y"
	local prompt=" Continue [Y]/n?"
	
	if [[ "$1" == "N" ]]
		then
		  prompt=" Continue y/[N]?"
		  default="n"
		fi
	
	while [ $try -gt 0 ]					
	do
		printf "%s " $prompt
		read choice
		
		
	   	if [[ "$choice" == '' ]]
	   	then
	    	choice=$default
	   	fi
	    
		if [[ $choice == "y" || $choice == "Y" ]];
		then
			printf "\n"
			sleep 1
	  		return 0
  		elif [[ "$choice" == "n" || "$choice" == "N" ]];
  		then
  			printf "\n"
  	  		return 1
  		else
  	   		printf "\nInvalid entry detected.\n"
  	   		let "try--"
  	   	fi
  	done
  	
  	printf "Too many invalid entries.  Nothing Done.  Exiting.\n"
  	sleep 1
  	exit $E_INVALIDARGUMENT
}





if continue                           # Optional N Value. Remove to default to "Y"         
then
	echo "This is the YES Result"
else	
	echo "This is the NO Result" 
	
fi



-----

Allan D. Reed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top