Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer in shell script?

Tags:

bash

shell

I am trying to print a filename which contains decimal point numbers... say

 L2.3stop.txt

I have variables defined as :

 z1=2.3
 z2=3.4
 z3=7.8
 z4=8.9

and so on

In a for loop i runs from 1 to 5

Inside the loop if I do

 temp=`echo z$i`

and then I print the file name using

 echo L${temp}stop.txt

it just prints

 Lz1stop.txt
 Lz2stop.txt

etc..

How can I print the desired filename....

I also tried using

 echo L$((z$i))stop.txt

but this only works when z1, z2, z3 etc are integers and not floating numbers....

like image 962
PyariBilli Avatar asked Jul 30 '26 22:07

PyariBilli


2 Answers

I hope this helps:

z1=2.3
z2=3.4
z3=7.8
z4=8.9

for i in {1..4};do
        echo L$(eval "echo \$z"$i)stop.txt
done

or this should work too:

for i in {1..4};do
    echo L$(echo $(echo "\$z$i"))stop.txt
done

outputs:

L2.3stop.txt
L3.4stop.txt
L7.8stop.txt
L8.9stop.txt
like image 136
Kent Avatar answered Aug 01 '26 11:08

Kent


This is not portable, but in bash you can do:

name=z$i
echo ${!name}
like image 28
William Pursell Avatar answered Aug 01 '26 12:08

William Pursell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!