LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 01-26-2004, 08:49 AM   #1
Bassam
Member
 
Registered: Mar 2003
Location: Malaysia
Posts: 63

Rep: Reputation: 15
Question How to change function parameter value and return back to the main shell program


Hi All,
In shell programming, I am trying to call a function and pass parametrs to it, I want to change the values of the parameters and return them back to the main shell script, so anyone could help me in this matter???

example:

Code:
#!/bin/bash
function names ()
{
        echo -e "Inside the function $1"
        echo -e "Inside the function $2"

        #here I am changing the values of the parameters.
        $1=Sahab
        $2=Alaa

        return
}

name1=Bassam
name2=Farah

echo -e "$name1\n"
echo -e "$name2\n"

names $name1 $name2

echo -e "$name1\n"
echo -e "$name2\n"
that was the first part of my question. Now the second part is that is it possible to return a value with the return keyword from inside the function to the main program?

example
Code:
#!/bin/bash
function addition ()
{
        return `expr $1 + $2`
}

echo < addition 5 10
Please correct if I am mistaken


Regards
Bassam
 
Old 01-26-2004, 10:02 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
You have to use variables that retain scope in and out of the function
Code:
#!/bin/bash
name1=""
name2=""
function names ()
{
        echo -e "Inside the function $1"
        echo -e "Inside the function $2"

        #here I am changing the values of the parameters.
        # $1 and $2 -- these are local ONLY to the function

        name1=$1 
        name2=$2 
        
        return
}

name1=Bassam
name2=Farah

echo -e "$name1\n"
echo -e "$name2\n"

names "XXX" "ZZZZ"

echo -e "$name1\n"
echo -e "$name2\n"
 
  


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 05:54 PM
how to change and pass thread parameter back to main dragondad Programming 3 09-19-2005 01:58 PM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 09:52 AM


All times are GMT -5. The time now is 06:50 PM.

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