Ksh riddle: interpret variable two times?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ksh riddle: interpret variable two times?
# 1  
Old 04-10-2002
Ksh riddle: interpret variable two times?

exam is a ksh script. In command line I enter: exam 3 param_2 param_3 param_4.

In exam how can I get the value of the parameter which position is specified by the first argument.
Simply doing this DOES NOT work:

offset=$1
value=$$offset

can you figure out any possible way to interpret a variable two times
# 2  
Old 04-11-2002
Check out the "shift" command. I think it will do what you are looking for.
# 3  
Old 04-11-2002
Try:
eval value=\$$offset
# 4  
Old 04-11-2002
An alternative to shifting the parameters down would be: (oops, posted at same time - gotta be fast to beat Perderabo, even this early in the morning! Smilie )
Code:
eval myparm=$"$1"

Jimbo
# 5  
Old 02-22-2008
I'd like to correct: last one is not the same!
I've tryed it:
Code:
bash-845:/home/dca0701/develop/src> nn=aa
bash-846:/home/dca0701/develop/src> aa=final
bash-851:/home/dca0701/develop/src> eval dn=\$$nn
bash-852:/home/dca0701/develop/src> echo $dn
final
bash-853:/home/dca0701/develop/src> eval dn=$"$nn"; echo $dn
aa

I do not even understand why it is happened this way?!
First time by 'eval' the $nn should be substituted for 'aa' and '"' should be remuved (as I understand!). So result should be
Code:
dn=$aa

and that by next processing should be as expected! But it prints only aa ?!
-------------------------
Ok, I've get it! It should be little bit different:
Code:
eval dn="$"$nn; echo $dn

Now it is going to result the same!

So: the 'eval' do NOT evaluates value in double quotations or after backslash, but removes that quotations and backslashes!
# 6  
Old 04-28-2009
The details

>So: the 'eval' do NOT evaluates value in double quotations or after backslash, but removes that quotations and backslashes!

Not exactly. Remember, the shell parses the entire line one time before "eval" gets to interpret it. Walking through:

The original line looks like this:

Code:
eval dn="$"$nn; echo $dn

after the shell parses it one time, it looks like this:

Code:
eval dn=$aa; echo final

Since the value of $aa is "final", thats what gets assigned to dn. But notice that $dn already had a value from a previous statement, and it was that value which got expanded into the literal string "final" before eval interpreted the code.

Also note that "$" evaluates to a literal dollar sign, because there is no way the shell can construe it as a variable indicator

further details on my wiki...

Last edited by vbe; 04-29-2009 at 05:18 AM.. Reason: Removed faulty URL (forum conformance...)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

2. Shell Programming and Scripting

Print one's place for 1 to N times, ksh Perl whatever?

Hello all, I would like to create a for loop or whatever is quick that will print the one’s place of a number for 1-N times say for example a printed page formatting is 132 characters wide, I would like a single line 123456789012345678901234567890... ...012 That is 132 characters long. I... (11 Replies)
Discussion started by: KmJohnson
11 Replies

3. Shell Programming and Scripting

Tabbing a line a variable number of times in shell

Hi, I had another question. I was wondering if there was a way to tab a line a variable number of times in tcsh. To go into details, I want to tab a line by how deep a file is in its path. So here is an example code: set filea=/blah1/blah2/blah3 set fileb=/blah1/blah2/blah3/blah4 set... (4 Replies)
Discussion started by: chu816
4 Replies

4. Shell Programming and Scripting

need help using one variable multiple times

I apologize for the title but I am not even sure myself what to call this. I am going to use an example of a pizza delivery. I need to make an interactive script that allows users to order a certain number of pizzas, and then choose what they want on each pizza. Here is my code so far.... ... (1 Reply)
Discussion started by: cstadnyk1
1 Replies

5. UNIX for Dummies Questions & Answers

Output from who command - quick riddle

Hi all. So I have a problem. I have been doing real good figuring this stuff out on my own but Im a newbie and stuck on something that is probably real basic. I want to get the following output from the who command: User TTY Date Time gd22a12 pts/1 Feb 1 11:34 gd22a13 pts/3 Feb 1... (13 Replies)
Discussion started by: losingit
13 Replies

6. Shell Programming and Scripting

KSH problem - how do i redirect three times?

i need to output an ls command to a file but also capture any errors from that command and output them to a log file and the screen. if it's only possible to output them to a log file and not the screen then that's fine. this is what i've tried so far, but it won't populate log.txt. i've... (16 Replies)
Discussion started by: mjays
16 Replies

7. Shell Programming and Scripting

Can't interpret variable in if statement

Can someone help me out here. I can't get this piece of code to work. i.e. $ALL_EVENTS does not get interpreted in the if brackets. The first part is the code, the second part is the execution of the code. Note: $ALL_EVENTS does equal 2, but there is no value once passed to the if statement. ... (4 Replies)
Discussion started by: jwholey
4 Replies

8. Shell Programming and Scripting

Riddle - solve it if you can

I don't understand this, can anyone explain the evaluation logic used here, and I would really appreciate a general explanation for it. ---------------------- Here's the korn script: -------------------- #! /usr/bin/ksh if ] then echo true else echo false fi if (( 2 > 10 )) then... (1 Reply)
Discussion started by: numstr
1 Replies

9. 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
Login or Register to Ask a Question