LinuxQuestions.org
Review your favorite Linux distribution.
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 12-24-2009, 03:42 PM   #1
isdigit
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 0
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?
 
Old 12-24-2009, 03:49 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

what about this?
Code:
#!/bin/bash

#Functions
function CallFunct {
functio
}

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

# MAIN
CallFunct

exit 0
 
Old 12-24-2009, 03:54 PM   #3
isdigit
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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

Last edited by isdigit; 12-24-2009 at 04:10 PM.
 
Old 12-24-2009, 07:21 PM   #4
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
functions in bash scripting wvw Programming 4 05-14-2009 05:22 PM
Functions in bash script som_kurian Programming 3 01-01-2008 09:12 AM
LXer: Using Functions in a BASH Prompt LXer Syndicated Linux News 0 06-11-2007 02:31 AM
Order of functions in a bash script essdeeay Programming 1 05-02-2006 10:52 PM
[bash-scripting]functions + arguments hylke Programming 14 10-05-2004 01:48 AM


All times are GMT -5. The time now is 11:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration