0

This has been already discussed, but I have a more different problem: I have a function that needs to be called with $@ as parameter.

If I put var=$(function $@) I just receive errors for every line where the function actions.

Meanwhile I used a workaroud:

  1. I called the function first: function $@
  2. Then I stored into the variable the result from the function: var=$?

But this works just if the function return is "succes" or "fail". Any thoughts?

Code:

function()
{
    if [ $1 -gt $x ]
    then
        return 0
    fi

    if [ $1 -eq $x ]
    then
        return 1
    fi

    if [ $1 -lt $x ]
    then
        return 2
    fi
}

I want to store in my variable 0 , 1 or 2. For this:

menu ()
{
    if [ $# -gt 5 ] || [ $# -lt 1 ]
    then
        echo "Error! Script is: " $0
        return
    fi

    echo "Insert reference number: "
    read x

    while [ $# -gt 0 ]
    do
        rez=$(function $@)

        if [ $rez -eq 0 ]
        then
            echo "Nr >!" $1
        fi

        if [ $rez -eq 1 ]
        then
            echo "Nr =!" $1
        fi

        if [ $rez -eq 2 ]
        then
            echo "Nr <!" $1
        fi
        shift
    done
}
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Marko
  • 407
  • 1
  • 7
  • 19
  • Please show some actual code and exact (verbatim) output/errors. – Mat Mar 20 '16 at 10:29
  • A bush function cannot return anything except success or failure. http://m.linuxjournal.com/content/return-values-bash-functions – n. m. could be an AI Mar 20 '16 at 10:30
  • `function` is a bash builtin keyword. I suggest to peplace both "function" by "foobar". – Cyrus Mar 20 '16 at 10:41
  • Yes, sorry. I just replaced my functions name (that was not in English) in a common name, for not creating any confusions. – Marko Mar 20 '16 at 10:44
  • Does this answer your question? [How do I set a variable to the output of a command in Bash?](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash) – Dave Jarvis Jul 04 '23 at 21:08

1 Answers1

2
  1. Maybe use elifs so that you don't receive multiple values that are returned (also a case statement might be a better solution).

  2. var=$(function $@ >/dev/null 2>&1; echo $?) should do what you want, I believe?

Geoff Nixon
  • 4,697
  • 2
  • 28
  • 34
  • My task says so: return 0 for ... 1 for ... and 2 for .... I just need those returns. What exactly is that code line doing, can u please explain? It works just as I want, indeed. – Marko Mar 20 '16 at 10:42
  • It's executing your function while redirecting both stdout and stderr to `/dev/null`, ensuring there's no output from your function unintentionally stored in the variable. It then echoes the return value, which gets saved in the variable. – Geoff Nixon Mar 20 '16 at 10:45
  • If the functions return somehow an error, will it still be visible if I use /dev/null? – Marko Mar 20 '16 at 10:58
  • 1
    No; if you want to keep some verbosity (but still only record the return value in the variable), you could use: `var=$(function $@ >&2; echo $?)` instead, which would redirect all output except the return value to stderr. – Geoff Nixon Mar 20 '16 at 11:02