Hello everyone,
I am curious to find a possible way of doing something like this in ksh:
call a function and have that function set the value of the variable that the function knows by the name of $1....
example:
#! /bin/ksh
set_var(){
case $1 in
var1) this is where I would like to set var1 to a particular value;;
var2) this is where I would like to set var2 to a particular value;;
var3) this is where I would like to set var3 to a particular value;;
esac
}
set_var "var1"
set_var "var2"
set_var "var3"
# Now I would like to see what these values where set
echo $var1
echo $var2
echo $var3
Is this possible?
Hope you can help give me ideas.
Thanks.
#!/bin/ksh
set_var()
{
case "$1" in
"var1") var1="houston";;
"var2") var2="boston";;
"var3") var3="new york";;
esac
}
set_var "var1"
set_var "var2"
set_var "var3"
# Now I would like to see what these values where set
echo "${var1}"
echo "${var2}"
echo "${var3}"
Well, what I would like to achieve inside the function is a statement somewhat like:
$1="houston"
inside my case statement, see, I do not want to have to explicitly key the var name since it has been passed to the function and it knows its name .....
Will var1, var2, etc. always be set to Houston? If so, then
export $1="Houston"
will work. If not, then the name of the variable must be in the case statement. Otherwise, how will the function know which value to assign? The explicit names of the variables will have to be typed as part of the case statement.
#! /bin/ksh93
set_var()
{
case $1 in
var1) eval $1=1 ;;
var2) eval $2=2 ;;
var3) eval $3=3 ;;
esac
}
set_var "var1"
set_var "var2"
set_var "var3"
# Now I would like to see what these values where set
echo $var1
echo $var2
echo $var3