Newbie bash scripting help requested

Hi,

I'm very new to bash scripting and Linux in general. I'm running Ubuntu Server 10.04 and trying to write a bash script to launch a program. In doing so, I've come across a couple of things that I obviously don't understand. Here is a short script that exemplifies those things:

#!/bin/bash

testfunc() {
    echo "Inside function testfunc()"
    return 0
}

if [ testfunc ]; then
    echo "testfunc() returned true"
else
    echo "testfunc() returned false"
fi

exit 0

The output of this script is:

testfunc() returned true

My questions:

  1. Why does the 'echo "some text"' within the function not produce any output? (Actually, I can't even tell if the function is being called.)

  2. Why does the function appear to return a true value?

Like I said, there are obviously some things I don't understand. :slight_smile:

Hi.

You never do call the function. Your if statement is testing a literal string "testfunc", which would always be "true" (as a non empty string)

Correct would be:

#!/bin/bash

testfunc() {
    echo "Inside function testfunc()"
    return 0
}

if testfunc; then
    echo "testfunc() returned true"
else
    echo "testfunc() returned false"
fi

Or:

#!/bin/bash

testfunc() {
    echo "Inside function testfunc()"
    return 0
}

testfunc
if [ $? -eq 0 ]; then
    echo "testfunc() returned true"
else
    echo "testfunc() returned false"
fi

Modify yours test :

#!/bin/bash

testfunc() {
    echo "Inside function testfunc()"
    return 0
}

if testfunc ; then
    echo "testfunc() returned true"
else
    echo "testfunc() returned false"
fi

exit 0

Output:

Inside function testfunc()
testfunc() returned true

Jean-Pierre.

Thank you both. However, the results aren't what I'd expect.

Inside function testfunc()
testfunc() returned true

Shouldn't I be seeing 'false' by returning 0 from the function?

No. A zero exit code is taken to mean "true". See this extract from the man page:

if list ;then list [ ;elif list ;then list ] ... [ ;else list ] ;fi
              The  list  following  if is executed and, if it returns a zero exit status, the list following the first then is
              executed.

Ah, ok... Is that because the non-empty string is evaluated as 'true', or does it mean that non-zero will be evaluated as 'false'?

I came across something that said that function return values are much like exit codes, so I'm guessing that they're not intended to be used to return the "results" of executing the function. How then would you normally define and use a function to return a boolean value?

A function returns the result (a number) of the last command executed in it (unless you tell it otherwise):

# cat some_script
function some_function {
 grep x some_file
}

some_function
echo $?

touch some_file
some_function
echo $?

echo x > some_file
some_function
echo $?

# ./some_script
grep: some_file: No such file or directory
2
1
x
0

If you want it to return exactly 0 or 1, then you should code your function to do this, although a non-zero result is generally taken to mean failure.

(to your first statement, change "or does it mean" to "and does it mean", and the answer would be "yes" to both)

:slight_smile:

How can a non-empty variable evaluate to 'true' _and_ a variable containing, say, the value '1' evaluate to 'false'?

You're maybe confusing return codes with variables / strings that functions echo or print. The two have nothing to do with each other.

$ cat some_script
function some_func {
  echo 1
  return 0
}

if some_func; then
  echo TRUE
else
  echo FALSE
fi

some_func


$ ./some_script
1
TRUE
$ cat some_script
function some_func {
  echo 1
  return 1
}

if some_func; then
  echo TRUE
else
  echo FALSE
fi

some_func


$ ./some_script
1
FALSE

Thanks. I think I've got it now.