INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

If/Then Condition Based on Value Returned From a Function

If/Then Condition Based on Value Returned From a Function

If/Then Condition Based on Value Returned From a Function

(OP)
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
http://www.linkedin.com/in/adreed

RE: If/Then Condition Based on Value Returned From a Function

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 

RE: If/Then Condition Based on Value Returned From a Function

(OP)
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
http://www.linkedin.com/in/adreed

RE: If/Then Condition Based on Value Returned From a Function

(OP)
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
http://www.linkedin.com/in/adreed

RE: If/Then Condition Based on Value Returned From a Function

(OP)
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.

CODE --> 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
http://www.linkedin.com/in/adreed

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close