The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calling a function through a variable Irrational UNIX for Dummies Questions & Answers 5 09-29-2009 06:27 AM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 12:39 PM
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 08:28 PM
passing variable to function Knotty UNIX for Dummies Questions & Answers 4 04-05-2007 01:49 AM
Passing a variable name to be created within a function 435 Gavea Shell Programming and Scripting 2 02-04-2004 03:20 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 3 Weeks Ago
somu_june somu_june is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 6
How to see a variable value outside a function in kshell

Hi,

I wrote a small shell script which had function C_fun() and script name is same C_fun.ksh

Here is the program inside the script


Code:
#!bin/ksh -x
C_fun()  {
  typeset TEXT=${1}
}
echo Value of TEXT $TEXT

When Im running the above script with Parameter "R" as the option . The echo statement is not throwing me a value
The output is echo Value of TEXT and there is no value available "R" . How can I catch the variable value outside the function. I tried with export option instead of typeset but no help. I Want to catch the value of the $TEXT outside the function. I hope some one can help me in this.


Thanks,
Raju.

Last edited by Franklin52; 3 Weeks Ago at 05:59 PM.. Reason: Please use code tags!!
  #2 (permalink)  
Old 3 Weeks Ago
steadyonabix steadyonabix is offline
Registered User
  
 

Join Date: Oct 2009
Location: UK
Posts: 184
You can get at the variable in a number of ways: -


Code:
#! /bin/ksh -x

C_fun() {
        TEXT=$1
}

C_fun test 

echo "Value of TEXT = $TEXT"


Code:
#! /bin/ksh -x

C_fun() {
        eval ${2}=${1}
}

C_fun test TEXT

echo "Value of TEXT = $TEXT"

  #3 (permalink)  
Old 3 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 741
Hi Raju,

A few remarks:
  1. The shebang is wrong and should be
    Code:
    #!/bin/ksh

  • Your script is not calling the function C_fun so the variable TEXT never gets set
  • You are using the typeset declaration which declares a variable as local to the function, so in fact you are demanding that the variable be unknown outside the function.
  • You'll discover that the variable is global anyway, since you are using the posix style function declaration: (
    Code:
    fn(){ ... }

    instead of the ksh style:
    Code:
    function fn{ ... }

  • If you are using ksh93 you could use nameref instead of eval

  • Code:
    #!/bin/ksh
    C_fun() {
      typeset TEXT=${1}
    }
    echo Value of TEXT $TEXT
    
    Value of TEXT
    ----
    #!/bin/ksh
    C_fun() {
      typeset TEXT=${1}
    }
    C_fun test
    echo Value of TEXT $TEXT
    
    Value of TEXT test
    ----
    #!/bin/ksh
    C_fun() {
      TEXT=${1}
    }
    C_fun test
    echo Value of TEXT $TEXT
    
    Value of TEXT test
    ----
    #!/bin/ksh
    function C_fun {
      typeset TEXT=${1}
    }
    C_fun test
    echo Value of TEXT $TEXT
    
    Value of TEXT
    ----
    #!/bin/ksh
    function C_fun {
      TEXT=${1}
    }
    C_fun test
    echo Value of TEXT $TEXT
    
    Value of TEXT test
    ----
    #!/bin/ksh
    function C_fun {
      nameref var=$2
      var=$1
    }
    C_fun test TEXT
    echo Value of TEXT $TEXT
    
    Value of TEXT test


    Last edited by Scrutinizer; 3 Weeks Ago at 10:53 PM.. Reason: Change bangshe to shebang in .1 Thanks CFA
      #4 (permalink)  
    Old 3 Weeks Ago
    cfajohnson's Avatar
    cfajohnson cfajohnson is offline Forum Advisor  
    Shell programmer, author
      
     

    Join Date: Mar 2007
    Location: Toronto, Canada
    Posts: 2,365
    Quote:
    Originally Posted by Scrutinizer View Post
    [*]The shebang is wrong and should be
    Code:
    !#/bin/ksh

    ?????

    The correct shebang is: #!/bin/ksh

    A shebang is a comment and starts with #.
      #5 (permalink)  
    Old 3 Weeks Ago
    Scrutinizer Scrutinizer is offline
    Registered User
      
     

    Join Date: Nov 2008
    Posts: 741
    Quote:
    Originally Posted by cfajohnson View Post

    ?????

    The correct shebang is: #!/bin/ksh

    A shebang is a comment and starts with #.
    Thanks! I corrected the typo...
      #6 (permalink)  
    Old 3 Weeks Ago
    somu_june somu_june is offline
    Registered User
      
     

    Join Date: Nov 2009
    Posts: 6
    Hi Guys,

    Thanks for your help it is working fine if I do as you said


    Code:
    #!/bin/ksh
    C_fun() {
      typeset TEXT=${1}
    }
    C_fun ${1}
    echo Value of TEXT $TEXT

    I have one more question on this. How can I Pass multiple parameters to a function above I'm passoing C_fun ${1} only . I want to pass $2 also as an parameter to a function I tried by placing comma like C_fun ${1},${2} but this is giving me Value of Text F,Fail. How to pass multiple parameters to a function and see its value.

    ---------- Post updated at 04:51 PM ---------- Previous update was at 04:45 PM ----------

    Here is my script



    Code:
    #!/bin/ksh
    C_fun() {
    typeset TEXT=${1}
    typeset  Stat=${2}
    }
    C_fun ${1},${2}
    echo Value of TEXT $TEXT
    echo Value of Stat $Stat


    But it is giving me

    Code:
    Value of TEXT F,Fail
    Value of Stat

    Value of Stat is empty I need to get its value as Fail and Text Value as F since $1 is F and $2 is Fail

    I think this post will be more clear then previous one.

    Thanks,
    Raju.

    Last edited by Franklin52; 3 Weeks Ago at 05:58 PM.. Reason: Please use code tags!!
      #7 (permalink)  
    Old 3 Weeks Ago
    Scrutinizer Scrutinizer is offline
    Registered User
      
     

    Join Date: Nov 2008
    Posts: 741

    Code:
    #!/bin/ksh
    C_fun() {
      TEXT=$1
      Stat=$2
    }
    C_fun "That was fun" "While it lasted"
    echo Value of TEXT $TEXT
    echo Value of Stat $Stat
    
    Value of TEXT That was fun
    Value of Stat While it lasted


    Code:
    #!/bin/ksh
    C_fun() {
      TEXT=$1
      Stat=$2
    }
    C_fun food barf
    echo Value of TEXT $TEXT
    echo Value of Stat $Stat
    
    Value of TEXT food
    Value of Stat barf

    Reply

    Bookmarks

    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    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 On
    Pingbacks are On
    Refbacks are On




    All times are GMT -4. The time now is 06:09 AM.


    Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
    vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
    The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

    Content Relevant URLs by vBSEO 3.2.0