ksh: finding the value of a value in a for loop

ksh: finding the value of a value in a for loop

Post by Lars Clause » Tue, 06 Oct 1998 04:00:00




Quote:> Hi all,
>     part of one of my scripts (AIX 4.1.5) has a structure like this:

> #!/bin/ksh
> dog=123
> cat=456

>         for item in dog cat
>         do
>            echo "The value of $item is ${${item}}"
>         done

> I want the output to look like:
>   The value of dog is 123
>   The value of cat is 456

> but I get back:
>   adam[7]: "The value of $item is ${${item}}": 0403-011 The specified
> substitution is not valid for this command.

> I know the "${${item}}" is wrong, but I can't find a way to tell it I
> want to get the value of item, which is dog, then the value of dog, which
> is 123.  I've tried many,many combinations, but nothing has worked.

I don't know if this works for ksh (don't have that on my system -- yet),
but I think bash is similar enough:

bash$ FOO=PATH
bash$ eval echo $`echo $FOO`
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin

-Lars

--

A *real* smart bomb would call in sick, perhaps move to another country,
changing its name in the process, open a beach bar maybe and live out its
days in safe anonymity.                          -- Barry O'Neill in rhod

 
 
 

ksh: finding the value of a value in a for loop

Post by DFRusse » Thu, 08 Oct 1998 04:00:00


|> Hi all,
|>     part of one of my scripts (AIX 4.1.5) has a structure like this:
|>
|> #!/bin/ksh
|> dog=123
|> cat=456
|>
|>         for item in dog cat
|>         do
|>            echo "The value of $item is ${${item}}"
|>         done
|>
|> I want the output to look like:
|>   The value of dog is 123
|>   The value of cat is 456

With the thought that this might be a good place for an
associative array instead....

To do what you ask you can:

#!/bin/ksh
dog=123
cat=456
for item in dog cat
    do
    eval print "The value of $item is" '$'${item}
    done

--
Views expressed are personal and not necessarily shared by my employer.

 
 
 

1. Find numeric value in xml file, recalculate, and replace old value

Hi!

I'm trying to write a shell-script (using SUN OS at the moment) that reads
an xml-file, find a tag that contains a value, recalculate that value and
replace the old value and write it to a new file.

Say i have a tag in the xml source file that is: <myval="45">, i wan't to
recalculate 45 by multiplying it with 2 and replace 45 in the above tag with
the sum (90) as: <myval="90"> while saving it over to a new xml-file.

I manage to replace the original value with a predefined value using 'sed
s/myval="45"/myval="90"/', but i wan't to recalculate it on the fly cause
next file might have a other value than 45 and here's where i'm stuck. I
know it's doable in perl but prefer to keep perl out of it if possible.

Right now i'm trying to learn some "awk" but i'm not sure that's the right
tool either.

Any suggestions welcome! Cheers!

Mats

2. How to determine in shell if a certain program is running?

3. syscall parameters length

4. Using "if" in "ksh" to change "for loop" values....

5. Ksh... disabling ^D from logging out

6. Variable in while loop not returning value outside the loop.?

7. SCO UNIX and 3Com 589 ethernet controller

8. Can I find the value of -c "string" in ksh?

9. Echoing value of variable when value contains "$"

10. Stripping out value from file, value is a variable though.

11. settimeofday(2) succeeds for microsecond value more than USEC_PER_SEC and for negative value

12. value of a value question...