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
#!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.
You can get at the variable in a number of ways: -
#! /bin/ksh -x
C_fun() {
TEXT=$1
}
C_fun test
echo "Value of TEXT = $TEXT"
#! /bin/ksh -x
C_fun() {
eval ${2}=${1}
}
C_fun test TEXT
echo "Value of TEXT = $TEXT"
Hi Raju,
A few remarks:
- The shebang is wrong and should be
text #!/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: (
text fn(){ ... }
instead of the ksh style:
text function fn{ ... }
- If you are using ksh93 you could use nameref instead of eval
#!/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
Hi Guys,
Thanks for your help it is working fine if I do as you said
#!/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
#!/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
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.
#!/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
#!/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
?????
The correct shebang is: #!/bin/ksh
A shebang is a comment and starts with #.
Thanks! I corrected the typo...