I've got the following situation:

I'm writing a script that will read its parameters either from a config file (if exists and parameter present) or asks the user to input said parameter if it's not present.

Since I'm doing this for a handful of parameters I thought writing a function would be the way to go.

However, as far as I understand it, the function returns the result value by echoing it or by assigning it to a global variable. I do want to echo to the screen in the function though, so it'll have to be option two. So i tried this:

# parameters: $1=name of parameter, $2=user prompt, $3=value read from config.cfg
function getParameter {
    # If the value couldn't be read from the config file
    if [ -z "$3" ]; then
        # Echo the prompt
        echo "$2"
        # Read the user input
        read parameter

        # If it's empty, fail
        if [ -z "$parameter" ]; then
            echo "Parameter $1 not found"
        # Else, try to assign it to $3   <---- This is where it fails

I call it like this:

getParameter "Database username" "Please enter database username" $database_username

The config.cfg file is sourced before the function is called and $database_username is one of the optional parameters there.

Now this obviously doesn't work. I can't assign to $3 and since I want the method to be generic, I can't do MY_VARIABLE=$parameter either.

Does anyone have any suggestions how I can achieve all of the below:

  1. Get variable value from either config.cfg or read it from the user input
  2. Do this in a generic fashion, i.e. don't repeat the above code (without a function) for each parameter

Not 100% sure that I follow, but let's say a config file looks like this:

database_user tom

The obvius value we want is that of database_user.

In a script you could simply put a line like this:

dbUser=$(sed -nE 's/database_user (.*$)/\1/p' config)

Then the variable $dbUser will contain this information:

echo $dbUser 
  • Yes, I get the values with source. That's all working fine. What I need is to ask the user for the value if it's not in the file. And ideally this should be genetic so that I can do it for multiple parameters – Baz Nov 2 '16 at 18:21

Alright, looks like I solved my own problem:

function getParameter {
    if [ -z "$3" ]; then
        # User read -p to show a prompt rather than using echo for this
        read -p "$2`echo $'\n> '`" parameter

        # Print to sdterr and return 1 to indicate failure
        if [ -z "$parameter" ]; then
            >&2 echo "Parameter $1 not found"
            return 1
            echo $parameter
        echo $3

By using echo -p I was able to show a prompt on the console and still be able to return a string from the function by using regular echo. That way, by calling the function with database_username=$(getParameter ...) I can assign it to a variable.

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.