variable as variable name (ksh)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable as variable name (ksh)
# 1  
Old 02-14-2007
variable as variable name (ksh)

Greetings folks

I'm using ksh on aix and have gone around and around trying to use a variable as a variable name. I would like to set a variable like this...

ym200702="01 02 05 06 07 08 09 12 13 14 15 16 19 20 21 22 23 26 27 28"

and then check to see if the variable has value...

if [[ ! -z "ym$YEAR$MONTH" ]]; then
echo HIT
fi

and if so I would like to set another variable to the contents of $ym200702.

For instance
someVariable=ym$YEAR$MONTH

I would like to stick with using ksh but if there are other ideas with sed/awk/perl etc let me know.

Smoker
# 2  
Old 02-14-2007
Code:
eval someVariable=\$ym$YEAR$MONTH
if [[ ! -z "$someVariable" ]]; then
echo HIT
fi


jean-Pierre.
# 3  
Old 02-15-2007
Thank You jean-Pierre

Smoker
# 4  
Old 05-16-2008
Good input Jean-Pierre! I was looking for a solution for this problem too. A Google search brings me to here. Thanks a lot and cheers with a Premier Grand Cru Classé! Smilie

Best regards,
Luis Santos
# 5  
Old 05-16-2008
If you are using ksh93, you could also use a name reference (typeset -n) instead of eval:
Code:
#!/usr/bin/ksh93

YEAR=2007
MONTH=02

ym200702="01 02 05 06 07 08 09 12 13 14 15 16 19 20 21 22 23 26 27 28"

typeset -n someVariable="ym$YEAR$MONTH"

if [[ ! -z $someVariable ]]; then
   echo HIT
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escaping a variable in ksh

I am trying to echo a variable exactly to a script- echo "${var1} ${var2} >> output.output Gives me a blank file. I would like output.output to basically say: ${var1} ${var2} I think I need to use a special escape character for variables. Am I right in assuming that, and is it the... (8 Replies)
Discussion started by: jeffs42885
8 Replies

2. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

3. Shell Programming and Scripting

Passing value of variable in KSH

Here is a sample script demonstrating the issue x() { echo "foo" a=1 } ## the value of $a is 1 a=0 x echo $a ## the value of $a stays 0 a=0 x|sed "s/foo/bar/" echo $a Result: foo 1 (1 Reply)
Discussion started by: pbmarvin56
1 Replies

4. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

5. Shell Programming and Scripting

variable substitution in ksh

Hi I have a variable BIT1 which holds some value. Is there a way to retrieve the value of this variable indirectly via another variable, lets say SUBSET_BIT_NUM=1, so the call will look something like this: sundev1 $ echo ${BIT${SUBSET_BIT_NUM}} ksh: ${BIT${SUBSET_BIT_NUM}}: bad substitution ... (3 Replies)
Discussion started by: aoussenko
3 Replies

6. Shell Programming and Scripting

KSH variable substitution

Hi folks Please let me know if anyone knows how to handle this. My KSH script -> testscript.ksh cmd=$1 ENV="devl" echo $cmd This is how I call the script ./testscript.ksh 'ps -ef | grep br$ENV' How do I get this to print the below text i.e $ENV should be substituted with the value... (5 Replies)
Discussion started by: tipsy
5 Replies

7. Shell Programming and Scripting

ksh - get stdout name as variable

Writing a ksh script. If someone starts a process with: test.ksh > date.log How can I grab 'date.log' name as a variable in test.ksh? I need to get the 'date.log' name (not the contents) as a variable...without entering something like 'test.ksh date.log > date.log' (4 Replies)
Discussion started by: mhcueball2
4 Replies

8. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies

9. UNIX for Dummies Questions & Answers

ksh on HP-UX -- variable expansion

We have a script that runs in ksh on HP-UX 11.11. It takes three arguments. The last argument can be a filename or wildcard character. For example: script -s hello -t goodbye '*.d*' In a case such as this, I would wrap single quotes around the final argument because I dont want the shell to... (4 Replies)
Discussion started by: dangral
4 Replies

10. Shell Programming and Scripting

Using a variable list in ksh while

Hey all, been so long since I've done any KSH scriptting, I've forgotten exact sytax. 2 things, 1) I need to dot in an environment file that is a list of server names 2) I need to loop through though server names in a while loop, or until loop. Here's roughly what I have and trying to hammer out... (2 Replies)
Discussion started by: jpeery
2 Replies
Login or Register to Ask a Question