0

I have this bash code that checks if the OS is Linux or Mac, and I use the function isWhat to be invoked from other functions.

function isWhat 
{
  if [ `uname` == $1 ];
  then
    return 1
  else
    return 0  
  fi
}

function isLinux 
{
    return isWhat("Linux")
}

function isMac
{
    return isWhat("Darwin")
}

However, I got these errors:

/functions.sh: line 13: syntax error near unexpected token `('
/functions.sh: line 13: `    return isWhat("Linux")'
runme.sh: line 7: isMac: command not found

What might be wrong?

3
  • Also, don't use the function keyword at all; it makes your script incompatible with POSIX sh for no good reason. isWhat() { is enough to start a function, and using that form retains compatibility with pure POSIX shells. Commented Nov 4, 2015 at 22:14
  • 1
    You've got other bugs in here too, by the way -- you're missing quotes around expansions (that one's a "real" bug, even if your only target is bash), and == isn't guaranteed to work inside [ ] by the POSIX standard (the standardized string comparison operator is =, not ==). Try running through shellcheck.net for an automated check for the quoting errors. Commented Nov 4, 2015 at 22:18
  • ...and as a matter of practice, if you're writing something to be sourced in as an include file and using bash-only syntax, name it .bash, not .sh. Commented Nov 4, 2015 at 22:24

3 Answers 3

5

That's not how you call functions in bash. They work just like other shell commands, i.e.:

function isLinux 
{
    isWhat "Linux"
}

Also, the return is redundant the function will return the exit status of the last command run. If you wanted to be explicit, you'd write it like:

function isLinux 
{
    isWhat "Linux"
    return $?
}
Sign up to request clarification or add additional context in comments.

2 Comments

If we're pointing folks towards best practices, I'd tend to suggest POSIX-compliant function call syntax as well (isLinux() { with no function).
@CharlesDuffy: Good point. OP did say bash but since the point of this is to detect platform, making it more cross platform makes a lot of sense, so I'd tend to agree.
0

This is the working code after fixing some bugs.

function isWhat 
{
  if [ "`uname`" = $1 ];
  then
    echo 1
  else
    echo 0  
  fi
}

function isLinux 
{
    isWhat "Linux"
}

function isMac
{
    isWhat "Darwin"
}

The usage is (assuming the previous functions are in abc.sh)

source abc.sh
echo $(isMac)
if [ $(isMac) == 1 ];
then
    echo "A"
fi

I'm not sure exactly why, but I had to add function to make the code work anyway, I use Mac OS X 10.11.1.

1 Comment

fwiw, the way you had it before with returns, if you swapped the 0/1 you could simply use if isLinux; then ... fi.
-1

You're calling the bash functions incorrectly.

e.g.

return isWhat("Linux")  

Should be

return isWhat "Linux"

etc

2 Comments

...and there's no point to the return; just making it isWhat "Linux" will pass the return value from the isWhat function through to the caller.
moreover, the documentation for return defines it to take an integer, not a command to call at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.