Function Returns

I'm having a little trouble returning a value from a function or calling it, I'm not quite sure.

I'm calling the function here

function region_lookup_with_details {
        results = $(set_region)
        echo $results
}

This is the function I'm calling

function set_region {
        zonecheck eq 0
        while $zonecheck eq 0
        do
                echo "Please enter the region you want to use\n"
                read zone
                if $zone eq 4 || $zone eq 5 || $zone eq 6 || $zone eq 7  || $zone eq 10 || $zone eq 11 || $zone eq 20 || $zone eq 21  || $zone eq 23 || $zone eq 25
                then
                        $zonecheck eq 1
                else
                        echo "That's not a region\n"
                fi
        done
        return $zone
}

This is the error I'm getting

./test[150]: region_lookup_with_details[57]:set_region[15]:2: not found [No such file or directory]

That seems a strange syntax to me. What shell are you using?

./test[150]: region_lookup_with_details[57]:set_region[15]:2: not found [No such file or directory]

To clarify, this is how I understand this error message:

  • it's a call from region_lookup_with_details(line 57)
  • next deeper call is set_region(line 15)
  • there the error message is 2: not found [No such file or directory]

I'm not sure what zonecheck is and what it does. But what I see is this:

zonecheck eq 0
while $zonecheck eq 0

You call it as function/command/alias. Next line you call a variable with the same name. Is that a typo?

zonecheck is just purely a local variable to hold the while loop in place so it continues to make sure your using a valid zone.

As to the first question I'm using ksh, but might be getting bash mixed in by accident my scripting skills aren't overly great.

On top of several syntax errors:

function region_lookup_with_details {
        results = $(set_region)                         # ---> returns=$(set_region)
        echo $results
}



function set_region {
        zonecheck eq 0                                  # ---> zonecheck=0
        while $zonecheck eq 0                           # ---> while [ ... ]
        do
                echo "Please enter the region you want to use\n"
                read zone
                if $zone eq 4 || $zone eq 5...          # ---> if [ ... ]; [ $zone -eq 4 ]
                then
                        $zonecheck eq 1                 # ---> if [ ... -eq ... ]
                else
                        echo "That's not a region\n"
                fi
        done
        return $zone
}

, you have the logical error that the "command substitution" will replace the function with its output to stdout while the (exit) code conveyed by the return command can be retrieved in the $? parameter.

Thanks so much, and just so I understand your quote above. Does this mean that the $zone value I'm returning from the function set_region will just be wiped out everytime?

Also does this look better for the if statement, I'm not sure if || means or, because I'm only looking for those numbers?

if [$zone eq 4] || [$zone eq 5] || [$zone eq 6] || [$zone eq 7] || [$zone eq 10] || [$zone eq 11] || [$zone eq 20] || [$zone eq 21] || [$zone eq 23] || [$zone eq 25]
then
                        $zonecheck=1
                else
                        echo "That's not a region\n"
                fi

---------- Post updated at 04:01 PM ---------- Previous update was at 03:31 PM ----------

Now it just hangs and throws the same error when you hit enter.

Using comments might be easier for me to explain what I'm trying to do:

#A function to call another function to get a user to enter a valid region in a variable
#Use that variable in another command
#Print out the results from the command

#A function for getting the users region and making sure it's in a valid list
#Request the user input a region and get it in a variable
#A while loop that keep going as long as the user input doesn't match a valid region
#A for loop to test the user input against the valid regions
#Matching with a valid region exit the while loop
#Not matching rerun through the while loop
#Return the zone that was found to be valid

Saying you're getting an error when you hit return without showing us the exact error you are getting wastes all of our time and delays getting help from us. But, in this case it is obvious that you still have the syntax errors in the tests in your if statement, ksh (and any other shell conforming to the standards) requires whitespace separating all of the arguments in a test command (AKA [ expression ] command), and the numeric equal comparison operator in that expression is -eq ; not eq . This might come closer to what you're trying to do, but without seeing how you are calling the functions you're talking about and seeing how you're testing the results produced by this code segment, we don't know whether or not it will do what you need:

if [ $zone -eq 4 ] || [ $zone -eq 5 ] || [ $zone -eq 6 ] || [ $zone eq 7 ] || [ $zone -eq 10 ] || [ $zone -eq 11 ] || [ $zone -eq 20 ] || [ $zone -eq 21 ] || [ $zone -eq 23 ] || [ $zone -eq 25 ]
then
	$zonecheck=1
else
	echo "That's not a region\n"
fi

or with more readable lines:

if [ $zone -eq 4 ] || [ $zone -eq 5 ] || [ $zone -eq 6 ] || [ $zone eq 7 ] || \
    [ $zone -eq 10 ] || [ $zone -eq 11 ] || [ $zone -eq 20 ] || [ $zone -eq 21 ] || \
    [ $zone -eq 23 ] || [ $zone -eq 25 ]
then
	$zonecheck=1
else
	echo "That's not a region\n"
fi

and, for something with this many tests, a case statement might be a better choice than an if statement.

...or use regex(bash only)...

if [[ "$zone" =~ ^([4567]|10|11|20|21|23|25)$ ]]; then
        zonecheck=1
else
        echo "That's not a region"
fi
#
# Alternate, not so efficient way in ksh 
# if echo "$zone" | grep -qE '^([4567]|10|11|20|21|23|25)$' ; then

The if statement:

if [[ $zone =~ ^[4567]|10|11|20|21|23|25$ ]]; then
        zonecheck=1
else
        echo "That's not a region\n"
fi

does not work in bash . The 1st anchor only anchors the 1st alternative in that RE and the last anchor only anchors the last alternative in that RE. So, with both bash and 1993 or later versions of ksh , that if statement will accept zones 79, 125, 110, 239, and lots of others that should not be accepted. If you use the same ERE that you used in the grep -E command, both recent versions of bash and 1993 or later versions of ksh give the correct results for all expansions of $zone that do not contain any embedded whitespace characters. A test that would work in either of those shells for any settings of the zone variable would be:

if [[ "$zone" =~ ^([4567]|10|11|20|21|23|25)$ ]]; then
        zonecheck=1
else
        printf "That's not a region\n\n"
fi

Note that depending on operating systems and shell options, the command:

echo "That's not a region\n"

will either print the string That's not a region\n followed by a single <newline> character or the string That's not a region followed by two <newline> characters. I used printf instead of echo to be sure that you get the output I think you wanted no matter which operating system, version of the shell, and shell options you're using.