LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-09-2011, 01:58 PM   #1
kdx1980
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Rep: Reputation: Disabled
Im having trouble calling a function


Ive made a function and I cant seem to call it. it will run when I use it as a script, as soon as I make it a function glass()
{dls;ajgfl;dsajg;sl
gfdo;s'gkjfd'}
then I tried to put it into a script called run_glass
glass().

I get an error on line 4 doesnt recognize end of file. Basically I just put it into the file run_glass to see if it would run and it wouldnt. Do i need to put something before or after the function designation to get it to call and run properly? Hopefully this makes sense to someone. Thank you for reading

KDX1980
 
Old 08-09-2011, 02:10 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Hello,

Can you post the actual code of your script for us? The gibberish text you typed in doesn't help one bit man... Not trying to be a d*ck either, but without seeing code, how can we help you troubleshoot?

Cheers,

Josh
 
Old 08-09-2011, 02:21 PM   #3
kdx1980
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
LOL no offense taken, Hey I just want to help you help me. Here is the code all I want to do is be able to call this function into another script that only contains this function merely to prove the function can be called and works
glass()
{
echo "enter in a number: "
read a
echo "enter in another number: "
read b
echo "the number $a to the power of $b is"
expr ="$(( $a ** $b )) "
}

Last edited by kdx1980; 08-09-2011 at 02:23 PM.
 
Old 08-09-2011, 02:37 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Ahh ok.... You can use the source() call within your script to include functions from another script:
Code:
#!/bin/bash
source script_containing_function
glass argument
Hope this helps!

Cheers,

Josh
 
Old 08-09-2011, 05:20 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by corp769 View Post
source() call
It's not a system call, just a bash builtin command.

The kernel has no concept of a shell, and they aren't integrated with each other.

Last edited by MTK358; 08-09-2011 at 05:21 PM.
 
Old 08-09-2011, 05:39 PM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by MTK358 View Post
It's not a system call, just a bash builtin command.

The kernel has no concept of a shell, and they aren't integrated with each other.
Oh I know, I was just showing to use source like a function. Like I said in the past, I sometimes have a hard time explaining things :/
 
Old 08-09-2011, 05:52 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by corp769 View Post
Oh I know, I was just showing to use source like a function.
Function calls in bash don't use parentheses. Or are you talking about creating functions?
 
Old 08-09-2011, 05:54 PM   #8
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
I used the parenthesis to let the OP that source was the command to use that would be used in importing a function from a different script. I didn't intend at all to mislead the OP......
 
Old 08-09-2011, 06:44 PM   #9
kdx1980
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
I was talking about creating a function once you create it How do you add it to a script. Like for instance I want to run the function glass (that i created) in a script called run_glass. Where the only contents of the script run_glass is the glass function. how would I make that run? the script is above.
 
Old 08-09-2011, 07:18 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
So the file "run_glass" contains the function "glass"? If so:

Code:
source run_glass

# now you can call the "glass" function here
 
  


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
calling a function every 1 second soltanloo Programming 12 07-25-2009 11:15 AM
calling a function from within another function in php jayakrishnan Programming 2 06-19-2007 08:36 AM
calling a function within a task manas_sem Programming 1 02-23-2007 03:51 PM
Question for calling function harrylee2003 Programming 3 08-06-2006 07:04 AM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM

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

All times are GMT -5. The time now is 12:47 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