![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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!! |
|
||||
|
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"
|
|
||||
|
Hi Raju, A few remarks:
Code:
fn(){ ... }
instead of the ksh style: Code:
function fn{ ... }
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 |
|
||||
|
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!! |
|
||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|