Simple Variable substitution in ksh not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple Variable substitution in ksh not working
# 1  
Old 11-23-2015
Simple Variable substitution in ksh not working

Hi Gurus,
Not able to catch what's going wrong here. I just want to get output as "tree".

Code:
server:/mk/app/nexapp $ echo $SHELL
/usr/bin/ksh
server:/mk/app/nexapp $ export db_name1="tree"
server:/mk/app/nexapp $ export i=1

1st try:
Code:
server:/mk/app/nexapp $ echo $(db_name$i)
ksh: db_name1:  not found.

2nd try:
Code:
server:/mk/app/nexapp $ echo $((db_name$i))
ksh: tree: 0403-009 The specified number is not valid for this command.

3rd try:
Code:
server:/mk/app/nexapp $ echo $(`(db_name$i)`)
ksh: db_name1:  not found.

Please help me understand why substitution is not working here. and what could be done to get the output as tree

---------- Post updated at 01:55 AM ---------- Previous update was at 01:17 AM ----------

variable substitution in ksh

Last edited by Don Cragun; 11-23-2015 at 03:12 AM.. Reason: Add CODE tags.
# 2  
Old 11-23-2015
Hi,

Just do

echo $db_name$i

It would print fine.
# 3  
Old 11-23-2015
mukesh.lalwani,
With any Korn shell, you can use arrays with a numeric subscript for subscripts from 0 to about 2047. With ksh93 and with bash you can use numeric subscripts with larger values or with string valued subscripts. The syntax is:
Code:
$ i=1
$ dbname[1]="tree"
$ echo "${dbname[$i]}"
tree
$

or for multiple array elements:
Code:
$ dbname=("forest" "tree")
$ for ((i = 0; i < ${#dbname[@]}; i++))
> do	printf 'dbname[%d]="%s"\n' "$i" "${dbname[$i]}"
> done
dbname[0]="forest"
dbname[1]="tree"
$

Or, with any shell based on Bourne shell syntax, you can use eval as shown in the link you referenced. Note that eval can be a security risk if any of the code that you are feeding to eval is supplied by a user. But, eval is generally safe if all of the strings being evaluated by eval are hardcoded into your script.

RTY,
Code:
$ echo $dbname$i

prints whatever $dbname expands to followed by whatever $i expands to not to the contents of the variable named by dbname followed by the expansion of $i.

And:
Code:
$ echo ${dbname$i}
ksh: syntax error: `$' unexpected
$

doesn't work either.
# 4  
Old 11-23-2015
Quote:
Originally Posted by mukesh.lalwani
.
.
.
Please help me understand why substitution is not working here. and what could be done to get the output as tree
.
.
.
Well, substitution IS working here, but it may not yield what you expected.
$(db_name$i) is an instance of
Quote:
Command Substitution
Command substitution allows the output of a command to replace the command name. There are two forms:

$(command)
or
`command`

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.
$((db_name$i)) performs
Quote:
Arithmetic Expansion
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:

$((expression))

The old format $[expression] is deprecated and will be removed in upcoming versions of bash.

The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo
parameter and variable expansion, command substitution, and quote removal. The result is treated as the arithmetic expression to be evaluated. Arithmetic expansions
may be nested.

The evaluation is performed according to the rules listed below under ARITHMETIC EVALUATION. If expression is invalid, bash prints a message indicating failure and no
substitution occurs.
$(`(db_name$i)`) is, mmmm, say, a duplicate command substitution. Results, if any, may be dubious.

---------- Post updated at 10:38 ---------- Previous update was at 10:34 ----------

And, the db_nameis not evaluated as the $ sign is missing.

And, what do you expect "the output as tree" to look like?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A simple variable subst is not working

Hi what i want: listing files in a special range ls -lrt 20120601{05..06}* ... -rw-rw-r-- 1 imp imp 279 1. Jun 07:51 201206010550 -rw-rw-r-- 1 imp imp 279 1. Jun 07:01 201206010600 -rw-rw-r-- 1 imp imp 279 1. Jun 07:11 201206010610 -rw-rw-r-- 1 imp imp 279 1. Jun 07:21... (1 Reply)
Discussion started by: IMPe
1 Replies

2. Shell Programming and Scripting

Using Sed to do a substitution inside ksh

I'm trying to do an ls from inside of a ksh script. I loop through the results one line at a time and attempt to do a substitution using sed to convert YYYYMMDD from the older files into the newer files. Basically sometimes the ETL load runs over midnight and half the files are off by one day... (3 Replies)
Discussion started by: Calbrenar
3 Replies

3. Shell Programming and Scripting

bad substitution error in ksh

Hello, In bash I can use the following: TMP=12345 MID=${TMP:1:1} the expected result is: 2 but when using KSH I'm getting a ''bad substitution" error. What is the correct syntaxin ksh? Thanks (2 Replies)
Discussion started by: LiorAmitai
2 Replies

4. Shell Programming and Scripting

Simple sed variable substitution

Give the code: set line = 2 set year = `sed -n '2p' file while ($line < 500) echo $line > f.txt @ line = $line + 1 end How do I utilize the variable $line in the code instead of the number 2. I'm using this in a while loop and counter. I've tried quoting it, double/single... (1 Reply)
Discussion started by: wxornot
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

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

7. Solaris

Substitution not working in ksh

Hi, Following code is working in bash but not in ksh. Can someone please send me an alternative? #!/bin/ksh fname="EOA.dmp" echo $fname logname=${fname/.dmp/.log} echo $logname I am getting below error in ksh "testcmd: logname=${fname/.dmp/.log}: 0403-011 The specified substitution... (3 Replies)
Discussion started by: arsheshadri
3 Replies

8. AIX

Substitution not working in ksh

Following code is working in bash but not in ksh. Can someone please send me an alternative? #!/bin/ksh fname="EOA.dmp" echo $fname logname=${fname/.dmp/.log} echo $logname I am getting below error in ksh "testcmd: logname=${fname/.dmp/.log}: 0403-011 The specified substitution is not... (2 Replies)
Discussion started by: arsheshadri
2 Replies

9. 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

10. Shell Programming and Scripting

ksh substitution

Hello, I thought it was possible to use several time a #! entry on a script but it doesn't seems to work. My need is to have a part of a ksh script without substitution so it would look like #!/bin/ksh -- first part --- #!/bin/ksh -f -- part without substitution -- #!/bin/ksh --... (2 Replies)
Discussion started by: solea
2 Replies
Login or Register to Ask a Question