LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-20-2009, 03:05 AM   #1
JohnnyBoy123
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 0
Wink 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
 
Old 04-20-2009, 03:13 AM   #2
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware
Posts: 572

Rep: Reputation: 154Reputation: 154
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
 
Old 04-20-2009, 03:15 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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).
 
Old 04-20-2009, 03:48 AM   #4
JohnnyBoy123
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 04-20-2009, 04:32 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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).
 
Old 04-20-2009, 05:33 AM   #6
shpenat
Member
 
Registered: Dec 2008
Distribution: LFS
Posts: 99

Rep: Reputation: 21
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
 
Old 04-20-2009, 05:50 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by shpenat View Post
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.
 
Old 04-20-2009, 07:12 AM   #8
shpenat
Member
 
Registered: Dec 2008
Distribution: LFS
Posts: 99

Rep: Reputation: 21
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++
 
Old 04-20-2009, 07:28 PM   #9
JohnnyBoy123
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
thanks

Thank you for all those replies. Very good stuff.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 08:25 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 01:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration