LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-27-2009, 06:03 PM   #1
senthilmuthiah
Member
 
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56

Rep: Reputation: 16
Question How to Write & Call Functions Within Shell Scripts


All:

I have a script like the following:


ABCfunction()
{
if [ SomeConditionIsTrue ]
then
echo "1"
else
echo "0"
fi
}

parameter="Cool"
flag=`ABCfunction $parameter`

I don't know why but the function call doesn't work. I hence wanted to verify with you guys whether I am on the right track (also, a basic question: in linux scripting we return values using echo command right? )

Looking forward for some help!

Thanks in advance!
 
Old 03-27-2009, 06:19 PM   #2
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html - search google for "bash functions"

echo just prints stuff to screen, the values your talking about are likely to be stored inside a variable and then you echo the variable
echo $flag
in your case...

hope it helps

Last edited by helptonewbie; 03-27-2009 at 06:21 PM.
 
Old 03-27-2009, 06:36 PM   #3
senthilmuthiah
Member
 
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56

Original Poster
Rep: Reputation: 16
Lightbulb

Thanks helptonewbie!

You are right. A function can return values using 'return' (which is used for returning the exit values) or 'echo' (this is for the command line) or directly altering what you have in the script already.

If I want a variable's value to be altered through a function, I first initiate its value in the script and then call the function. Inside the function, I just assign the value to the variable. And I am done!

Example:

ABCfunction()
{
if [ thisconditionistrue ]
then
a=5
else
a=10
fi
}

#Main Script
a=-1
parameter=Cool
ABCfunction $parameter

Reference: http://steve-parker.org/sh/functions.shtml
 
  


Reply

Tags
calls, function, linux, script


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
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
Help!Write xml document with shell scripts wmh830621 Programming 4 08-11-2006 04:55 AM
Can't run shell scripts I write? JustinCoyan Slackware 2 08-06-2004 12:50 PM
shell scripts & change dir pit1516 Linux - Newbie 6 06-22-2004 10:42 AM
how i can list directory by using read() & write() kernll functions ashamry Programming 2 05-10-2004 01:44 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration