${!var} does not work in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ${!var} does not work in ksh
# 1  
Old 06-17-2010
${!var} does not work in ksh

Anyone knows why the following function does not work in ksh (it does in bash)?

Code:
var() # Displays var value; case insensitive
{ 
  _var="$1"    

  if [ -n "${!_var}" ] ; then 
    echo ${!_var} 
  else 
    _var=$(echo "$_var" | tr 'a-z' 'A-Z') 
    echo ${!_var} 
  fi

  unset _var
}

Code:
$ var home
ksh: "${!_var}": 0403-011 The specified substitution is not valid for this command.

Code:
$ var path
/usr/local/bin:/usr/bin:/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/System32/Wbem:/cygdrive/c/Program Files/Support Tools:/cygdrive/c/Program Files/Hummingbird/Connectivity/7.11/Accessories/

Thanks!

Vic.
# 2  
Old 06-17-2010
Not exactly sure, what ${!_var} does in Bash, but if you are just looking to get the value of a variable in ksh, you should use ${var}
# 3  
Old 06-17-2010
Quote:
Originally Posted by a_programmer
Not exactly sure, what ${!_var} does in Bash, but if you are just looking to get the value of a variable in ksh, you should use ${var}
I'm not trying to get the value of the variable, but use the value as a variable name.

Here's the difference:

BASH
Code:
$ _var=HOME
$ echo ${_var}
HOME
$ echo ${!_var}
/home/VMendonc

KORN
Code:
$ _var=HOME
$ echo ${_var}
HOME
$ echo ${!_var}
ksh: ${!_var}: 0403-011 The specified substitution is not valid for this command.

# 4  
Old 06-17-2010
oh sorry... overlooked the reference to a variable there.

There is no direct way of accessing a variable using name reference in ksh. You need to use eval to do that:

Code:
> var="This is a variable"
> _var=var
> echo $_var
var
> eval echo \$$_var
This is a variable

If you use ksh93, however, there is a way to create name references:
Code:
> /bin/ksh93   ## Start a ksh93 shell
> var="This is a variable"
> typeset -n _var=var
> echo $_var
This is a variable

"typeset -n" creates a nameref to the given variable in ksh93, but this feature is not available in ksh (i.e., ksh88)
# 5  
Old 06-17-2010
And if using a nameref in ksh93, ! is used to produce the actual reference:
Code:
$ nameref _var=HOME
$ echo $_var
/home/user
$ echo ${!_var}
HOME

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script: "mkdir -p" doesn't work with var(cat x)

Hello, :) I've an issue with the creation of a directory, All work without it :mad: So, below, my scripts with the debug output : #!/bin/bash # PATHS HOME_BACKUP="/home/backup" HOME_SCRIPT="/home/scripts/test/backup_server" TARGET="/var/www" # DATE DATE_Ymd=$(date +%Y-%m-%d) #... (1 Reply)
Discussion started by: Arnaudh78
1 Replies

2. Shell Programming and Scripting

ksh - building a var

This works #!/bin/ksh FILE="file.txt" dosumtin () { date >> FILE } for i in {1..5} do dosumtin done cat $FILE But instead of building a file, I want to do the same with a var or an array. That is, to build one that saves all 5 of the subs execution responses in a var or an... (8 Replies)
Discussion started by: popeye
8 Replies

3. Shell Programming and Scripting

ksh: what does var=$(command) mean?

hi, i can see in a script it contains var=$( myFile | grep -i err ) why has this person done it like this? why not just var=`myFile | grep -i err` thanks (9 Replies)
Discussion started by: JamesByars
9 Replies

4. Shell Programming and Scripting

Why does my for loop does not work right in ksh?

hi all, i have a for loop statement in my ksh code. it only returns the first value retrieved not the value for the other rows. ex: acct_id value = returned value in the for loop 1 = 1 2 = 1 (instead of 2) 3 = ... (1 Reply)
Discussion started by: ryukishin_17
1 Replies

5. Shell Programming and Scripting

How to use shell var for pattern string at KSH

Hi there, In the following test, how to use shell var for pattern, regular expression. I need to accept pattern at argument, use it to pattern matching at shell script. Test: #!/bin/ksh # name t.sh exp="a@(a|b)" touch aa ab ac echo "\nTest without variable" echo "---------------------"... (2 Replies)
Discussion started by: tkang007
2 Replies

6. Shell Programming and Scripting

export variable from ksh script doesn't work

Hi there, in a script I have #!/usr/bin/ksh TEST=hello export TEST Problem is, that the variable doesn't get exported. I also tried typeset -x TEST=hello When I put the two lines in my .profile, the variable is set fine. Whats could be the problem here? (4 Replies)
Discussion started by: doc_symbiosis
4 Replies

7. Shell Programming and Scripting

Date command does not work in a KSH when called from Cron

Hi, I'm trying to execute a job on the last day of every month, for which i'm using the following code in my Korn shell script : if ]; then echo allowed only on last day of month. Today is `date +%d` >t.stm echo Tomorrow is `date +%d -d tomorrow` >s.stm exit 0 fi ... ....... (7 Replies)
Discussion started by: devilsadvocate
7 Replies

8. Shell Programming and Scripting

how to make ksh to work on solaries

i have a script i which i am using #!/bin/ksh todate=$(date +%m%d%y) dir="$dir/port${todate}" the above one works in linux ...ut not working in solaries could some one please help me in that (13 Replies)
Discussion started by: mail2sant
13 Replies

9. UNIX for Dummies Questions & Answers

Ksh Why Won't IF Statement work?

I'm trying to figure out why this if statement won't work: if || $zipcount != 6 ]] then echo ${myline} echo "ZIPCODE WARNING! ${zipcode} ${zipcount}" fi if ]] then echo ${myline} echo "STATE WARNING!... (3 Replies)
Discussion started by: developncode
3 Replies

10. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies
Login or Register to Ask a Question