0

Newbie to Shell scripting...

I'm looking to have an interactive setup script. It will ask questions and require the user to make a selection before continuing to the next step.

The problem I'm having is how to keep the code clean and isolated to specific functions for each step, while allowing each function to output directions and ask the user to make a selection. Within that very same function, capture their selection, to then pull data from an array and return a value to be used later on within the setup function.

Here's what I have so far, and I'm stuck...

# Domains
DOMAINS=(
    "domain.com"
    "example.com"
)

function set_domain(){

    echo ""
    echo "Select a Domain:"
    echo ""
    echo "0. to exit."
    echo "1. ALL DOMAINS"

    for ((i=0; i<${#DOMAINS[*]}; i++));
    do

    if [[ $((i+2)) < "10" ]];
    then
        echo $((i+2))".  "${DOMAINS[i]}
    else
        echo $((i+2))". "${DOMAINS[i]}
    fi

    done

    echo ""

    read -s -n1 DOMAIN

    if [[ ${DOMAIN} == "0" ]]; then
        exit
    fi

    if [[ ${DOMAIN} == "1" ]]; then
         return DOMAINS
    fi

    return ${DOMAINS[DOMAIN-2]}

}

function do_setup(){

    local DOMAIN=$(set_domain)

    echo DOMAIN

}

do_setup

The script doesn't even seem to trigger the set_domain function. How can I create a function to output directions, ask the user to make a selection, then base on their selection, return a value to be stored in a variable from the main calling function?

Basically, I want to take the output from set_domain and store it in a variable in the do_setup function to then later be passed off to yet another function to make use of that data.

What am I doing wrong?

2

Functions in bash have the same limitations and behavior as external scripts. They cannot return arbitrary data – return from a function only specifies the same sort of "exit code" (ranging from 0 to 127) as exit from a script. So you can return 0/1 and use it for success/failure checks or retrieve it from $?, but it cannot be used for arbitrary data.

The $(...) operator doesn't capture a command's "return value", it captures that command's output, i.e. text written to stdout. You can use it here, but the function needs to echo its result, not "return" it.

testfunc() {
    echo "Hello!"
}

var=$(testfunc)

echo "var = $var"

Note: It happens that bash actually has a built-in for making menu prompts, so you don't always need to reinvent that part:

PS3="Your choice: "
select DOMAIN in "${DOMAINS[@]}"; do
    if [[ $DOMAIN ]]; then
        echo "You entered '$REPLY' and selected '$DOMAIN'."
        break
    else
        echo "Error: Invalid choice '$REPLY'"
    fi
done
  • Sorry it took me so long to accept your answer. This is exactly what I was looking for. Thank you for your time and knowledge. – Michael Ecklund Apr 13 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.