LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem using functions with bash (https://www.linuxquestions.org/questions/linux-newbie-8/problem-using-functions-with-bash-777934/)

isdigit 12-24-2009 03:42 PM

Problem using functions with bash
 
Code:

#!/bin/bash

#Functions
CallFunct() {
functio()
}

functio() {
echo -n
echo -n
}
#End

# MAIN
CallFunct()

exit 0

When I try to run the current code, it stops on the bracket after functio() in CallFunct(). The only way I could get this to run was by removing CallFunct and just calling functio.
Could someone point me in the right direction?

evo2 12-24-2009 03:49 PM

Hi,

what about this?
Code:

#!/bin/bash

#Functions
function CallFunct {
functio
}

function functio {
echo -n
echo -n
}
#End

# MAIN
CallFunct

exit 0


isdigit 12-24-2009 03:54 PM

I don't know why it works now but here is the revised code:
Code:

#!/bin/bash

MainFunction() {
isRoot

} #Calling function; for simiplicity
isRoot() {
if [[ "$USER" != "root" ]]
 then
  echo "Sorry! You Have to be root to use this script"
  echo
  exit 1
fi
clear
} #Checks to see if user is titled root


MainFunction
exit  0

Edit: My kernel - 2.6.29.4 - Backtrack 4

GooseYArd 12-24-2009 07:21 PM

It works because (as evo2 pointed out, without saying) functio() wasn't declared when CallFunct() was declared. The order matters!


All times are GMT -5. The time now is 01:23 AM.