How to see a variable value outside a function in kshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to see a variable value outside a function in kshell
# 1  
Old 11-06-2009
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; 11-06-2009 at 05:59 PM.. Reason: Please use code tags!!
# 2  
Old 11-06-2009
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  
Old 11-06-2009
Hi Raju,

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

  2. Your script is not calling the function C_fun so the variable TEXT never gets set
  3. 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.
  4. 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{ ... }

  5. 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; 11-06-2009 at 10:53 PM.. Reason: Change bangshe to shebang in .1 Thanks CFA
# 4  
Old 11-06-2009
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; 11-06-2009 at 05:58 PM.. Reason: Please use code tags!!
# 5  
Old 11-06-2009
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

# 6  
Old 11-06-2009
Quote:
Originally Posted by Scrutinizer
[*]The shebang is wrong and should be
Code:
!#/bin/ksh


?????

The correct shebang is: #!/bin/ksh

A shebang is a comment and starts with #.
# 7  
Old 11-06-2009
Quote:
Originally Posted by cfajohnson

?????

The correct shebang is: #!/bin/ksh

A shebang is a comment and starts with #.
Thanks! I corrected the typo...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from one function to another function?

updateEnvironmentField() { linewithoutquotes=`echo $LINE | tr -d '"'` b() } I want to pass variable named $linewithoutquotes to another method called b(), which is called from updateEnvironmentField() method. How to do the above requirement with shell script (1 Reply)
Discussion started by: pottic
1 Replies

2. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

3. Shell Programming and Scripting

Please help with Kshell Script!

I am an AIX noobie, and have a question around an AIX 5.2 script that I need to run. Security Audit requires us to have no World Writable files on our server, but every time we restart the Domino server on AIX it re-flags two files as World Writable. I have a little script that I created that... (5 Replies)
Discussion started by: Nebs
5 Replies

4. Shell Programming and Scripting

function for variable files

hi all this is my function #! /bin/sh awk '/ Type/ { print "m;" $4 } /IDf/ {print $3 } /IuSac/ { print $3 } /IuSac/ { print $1 }' / IBM.txt |tr '\n' ';'| perl -pi -e 's/;m//g'|cut -d ";" -f 2-5 ' >> 2m 1) i wanna make it to save output of awk in a file named by date in order not to... (3 Replies)
Discussion started by: teefa
3 Replies

5. UNIX for Dummies Questions & Answers

Passing variable to Alias in Hp kshell

Hi all, I have a series of directories which i open regularly. I want create an alias so that i can pass the direcotry name to alias and then this commands makes Cd to the path i need. COuld you please help on how to create an alias ex of what i am trying but couldn't succeeded #alias... (1 Reply)
Discussion started by: firestar
1 Replies

6. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

7. UNIX for Dummies Questions & Answers

function not see variable in script

Hi Forum Can anyone tell me whats wrong with my script. What i want to do read in values from a input file using a while loop then taking that input from the file into a function that i created. Every time i execute the script it goes through the while loop but the function doesn't see the... (10 Replies)
Discussion started by: ShinTec
10 Replies

8. Shell Programming and Scripting

Variable value not retaining outside function

Hi All, As per my understanding, value of variable is retained outside function. But the value of array myarrayDriver is not retained outside function. Could you please tell the reason for the same.. code: readingConfigFile() { search_keyword="$1" i=0 for pointer in $(cat... (7 Replies)
Discussion started by: ajincoep
7 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

10. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies
Login or Register to Ask a Question