LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   echo from a function (https://www.linuxquestions.org/questions/linux-newbie-8/echo-from-a-function-720349/)

JohnnyBoy123 04-20-2009 03:05 AM

echo from a function
 
I have a function which works fine.
However, if I put it in a test statement then the echo does not work.

I expected to see
"calling my function"
echoed to the screen, when I call it in these two places.
if test myfun
if [ myfun ]

#here is my script
function myfun {
echo "calling my function"
return 1
}

myfun #this works

if test myfun
then
echo one 1
else
echo zero 0
fi

if [ myfun ]
then
echo one 1
else
echo zero 0
fi

ChrisAbela 04-20-2009 03:13 AM

test works on the exit code; "$?", like this:

$ myfun
$ echo $?

If $? is 0, then the test worked, otherwise it is a failure.

Suggestion: Say myfun gives 1 as output when it should test positively:

MYFUN=$( myfun )
if [ "$MYFUN" -eq 1 ]
then
echo one 1
else
echo zero 0
fi

colucix 04-20-2009 03:15 AM

The behavior is correct in both cases, but maybe it is not what you want. The first if/then check the exit status of the command
Code:

test myfun
this does not execute the function, but check for the length of the string "myfun". The test command without options is equal to test -n, which checks if the length of STRING is nonzero. Hence the test is true. Maybe you mean this:
Code:

if myfun
then
  echo one 1
else
  echo zero 0
fi

this checks the exit code of your function. The second if/then does exactly the same, since [ is a command equivalent to test. In this case you cannot test for the exit code of a command (the correct way is the first one, without square brackets and without test).

JohnnyBoy123 04-20-2009 03:48 AM

No, I mean, when I call the function in the "if [ myfun ] " line, I would expect "calling my function" to go to the screen. Also if I redirect the output from the body of the function, it does not go to output.txt as I would expect.


function myfun {
echo "calling my function"
echo "this will go to output " > output.txt
return 99
}

if [ myfun ]
then
echo one
else
echo zero
fi

colucix 04-20-2009 04:32 AM

I try to be more clear: when you use the syntax
Code:

if [ expression ]
then
  do something
fi

you evaluate an expression, not a command! That is, you don't execute myfun, but test for the literal string "myfun", which is always true! If you want to test a command you have to use this syntax:
Code:

if command
then
  do something
fi

without the square brackets. In this case the function myfun is actually executed and the output is displayed in the terminal (or redirected to a file).

shpenat 04-20-2009 05:33 AM

All of the posts here are actually correct, but they may appear little confusing. So I will try to sort them out a little.

The functions in bash are handled different from C/C++. Let's say you define
Code:

function myfun {
  echo "calling my function"
  return 1
}

Than if you call it this way:
Code:

myfun
the function is executed and writes "calling my function" to the screen. The return value is stored in variable "$?". Ultimately
Code:

myfun
MYFUN_RET=$?

will behave exactly as function in C/C++ storing returned value in MYFUN_RET variable.

But be careful.
Code:

MYFUN=$(myfun)
MYFUN_RET=$?

MYFUN_RET will still hold return value of your function, but otherwise it will behave different. It will store output of "myfun" function to variable MYFUN. So there will be no output to screen. But if you later do
Code:

echo $MYFUN
you will get "calling my function". In other words construction VARIABLE=function does not store return value into VARIABLE, but output of the function.

Now to that if commands. I think the difference between
Code:

if [ expression ]
and
Code:

if command
is explained quite clearly in the above post. But there is one small trap hidden. When using
Code:

if command
it does not evaluate the return code of "command" if "command" is your internal function. It will return the value of call to that function.

So if you want a function to do something and than evaluate its return value, the construction should look like
Code:

myfun
if [ $? ]
then
  echo one 1
else
  echo zero 0
fi


colucix 04-20-2009 05:50 AM

Quote:

Originally Posted by shpenat (Post 3514896)
it does not evaluate the return code of "command" if "command" is your internal function. It will return the value of call to that function.

This is not really clear to me. Why it should not evaluate the return code of a function? What is the "value of call to that function"? The return statement causes the function to exit with a specific return code, whereas if omitted, the return code is that of the last command executed inside the function.

shpenat 04-20-2009 07:12 AM

Coluxit you are right. I wrote it little bit confusingly. My error.

Code:

if command
then
  echo "true"
else
  echo "false"
fi

This will run
Code:

echo "true"
if return value of "command" is 0. In any other cases it will run
Code:

echo "false"
That is what I actually meant. The behaviour is quite opposite of what you get in C/C++

JohnnyBoy123 04-20-2009 07:28 PM

thanks
 
Thank you for all those replies. Very good stuff.


All times are GMT -5. The time now is 02:57 PM.