How to return a string?

function blah {
return "string"
}

it keeps saying string: not found

How can i do this guys? Because I'm trying to do something like this

function print_daemon_options {
	echo "Start Daemons - Please enter one or a combination of the following:"
	
	if isDatasubEnabled && isReconEnabled; then
		echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
		allowedInput = "1 2 3 4 5 6 7"
	
	elif isDatasubEnabled && isReconEnabled=0; then
		echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
		allowedInput = "1 2 3 4 6 7"

	elif isDatasubEnabled=0 && isReconEnabled; then
		echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
		allowedInput = "1 3 4 5 6 7"
		
	else
		echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
		allowedInput = "1 3 4 6 7"
	fi
	
	return $allowedInput
}

Try:

function blah {
  echo "string"
}

Functions return numbers (return codes).

echo the string, or set it in a variable.

i.e.

blah() {
  _RC="string"
}

blah
echo $_RC
blah() {
  echo string
}

_RC=$(blah)

Thanks for the reply Scott. I've got a function like this

function print_daemon_options {
    echo "Start Daemons - Please enter one or a combination of the following:"
    
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput = "1 2 3 4 5 6 7"
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput = "1 2 3 4 6 7"

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput = "1 3 4 5 6 7"
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput = "1 3 4 6 7"
    fi
    
    echo allowedInput
}


function stop_daemons {
    print_daemon_options

    echo $allowedInput
}

stop_daemons calls the print one which should echo the options and also return the string but the error I get is:

Start Daemons - Please enter one or a combination of the following:
1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit
print_daemon_options: ./wmfo_menu.sh[383]: allowedInput: not found
allowedInput

You need to remove the spaces from your variable assignments:

allowedInput="......"

I presume all of the isDatasubEnabled etc., are external programs?

I made the change and I still get an error (and also, yep isDatasubEnabled and isReconEnabled are functions within the same script)

function print_daemon_options {
    echo "Start Daemons - Please enter one or a combination of the following:"
    
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 5 6 7"
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 6 7"

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 3 4 5 6 7"
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 3 4 6 7"
    fi
    
    echo $allowedInput
}


function stop_daemons {
    print_daemon_options

    echo $(allowedInput)
}

Start Daemons - Please enter one or a combination of the following:
1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit
1 2 3 4 5 6 7
stop_daemons: ./wmfo_menu.sh[383]: allowedInput: not found

If this is the culprit

function stop_daemons {
    print_daemon_options

    echo $(allowedInput)
}
# then change to echo ${allowedInput}

Its still not working :frowning:

function print_daemon_options {
    echo "Start Daemons - Please enter one or a combination of the following:"
    
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 5 6 7"
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 6 7"

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 3 4 5 6 7"
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 3 4 6 7"
    fi
    
    echo $(allowedInput)
}

function stop_daemons {
    print_daemon_options

    echo $(allowedInput)
}

All I want it to do is print the options and then echo the allowedInput numbers :confused:

Start Daemons - Please enter one or a combination of the following:
1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit
print_daemon_options: ./wmfo_menu.sh[398]: allowedInput: not found

stop_daemons: ./wmfo_menu.sh[398]: allowedInput: not found

You seem to change things with each iteration you post. What you had originally seemed OK, except for the spaces in the variable declarations.

function print_daemon_options {
    echo "Start Daemons - Please enter one or a combination of the following:"
    
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 5 6 7"
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 6 7"

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 3 4 5 6 7"
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 3 4 6 7"
    fi
}

function stop_daemons {
    print_daemon_options

    echo $allowedInput
}

This is command substitution:

echo $(allowedInput)

meaning it will (try to) execute a program called "allowedInput" and return it's output to echo. That's why it gives you an error.

Thanks Scott. I have a further problem now.

# Validate users input when they start/stop daemons
function validateInput {
    input = "${$1}"
    allowedInput = "${2}"
    
    for allowedOption in ${allowedInput}
    do
        if [ ${input} -eq ${allowedOption} ]
        then
            return 0
        fi
    done
    
    return -1
}

function print_daemon_options {
    echo "Start Daemons - Please enter one or a combination of the following:"
    
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 5 6 7"
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 2 3 4 6 7"

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput="1 3 4 5 6 7"
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput="1 3 4 6 7"
    fi
}


function stop_daemons {

    print_daemon_options
    echo "$allowedInput"
    
    read daemonOption
    
    if [ "$daemonOption" = '7' ] ; then
           return
   
       else
       
           validateInput "$daemonOption" "$allowedInput"
           
           if [ ${?} -eq 0 ] ; then
               echo "good!"
           fi
    fi
}

It now echos the numbers that the user can enter. When I enter a number I get the error

validateInput: ./wmfo_menu.sh[427]: : bad substitution

any ideas guys?

You have spaces again, and an erroneous $ (not to mention lots of superfluous curly braces and quotes).

    input = "${$1}"
    allowedInput = "${2}"

Should be:

    input=$1
    allowedInput=$2

Interesting that your error is on line 427! Are you going to drip-feed us the whole script piece by piece?

Thanks Scott that worked. The line number it was erroring at was the last line in the file for some reason.