LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script Fails (https://www.linuxquestions.org/questions/linux-newbie-8/script-fails-878094/)

maddyfreaks 05-01-2011 12:15 AM

Script Fails
 
Hi, Am new to Linux. Here is what am trying.
#!/usr/bin/bash
export D1_DIR=/my/dir1
export D2_DIR=/my/dir2
for i in 1 2
do
echo $D$i_DIR ### Errors from here
mkdir -p $D$i_DIR ##Errors
ln -s $D$i_DIR L$i #Error
done

--- give a clue or help me in pointing out my mistake. I also tried eval but there is no use.....

David the H. 05-01-2011 01:18 AM

First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

The string $D$i_DIR has two variables, $D and $i. Since you didn't define $D, it fails. I think you want echo "D$i_DIR" instead. (See addendum below)

It's also generally safer to double-quote a variable, as I demonstrated.

Edit: Actually there's a second problem. Since the underscore can also be part of a variable name, the $i variable needs to be differentiated from it also.

So you have to use echo "D${i}_DIR".

By the way, is your bash exec really in /usr/bin/bash? Most systems have it in /bin/bash.

spazticclown 05-01-2011 01:19 AM

Nesting variables appears pretty tricky. You will need to use eval and the back slash to escape the variable.

Something like
Code:

eval echo \$D$i\_DIR
eval echo \$D$i"_DIR"

Either should work

More references can be found at google, using "bash nesting variables"

Hope this helps.

David the H. 05-01-2011 01:30 AM

spazticclown is right. I forgot about the indirect referencing needed to get the actual values.

However you don't really need to use exec here. Bash has an indirect reference pattern. Try this:
Code:


export D1_DIR=/my/dir1
export D2_DIR=/my/dir2

for i in 1 2 ; do

    Dn_DIR="D${i}_DIR"
    echo "${!Dn_DIR}"  #indirect referencing done with ${!var}

done


David the H. 05-01-2011 01:37 AM

One more thing. You can avoid all this indirect stuff completely if you simply use an array variable instead.
Code:

#!/bin/bash

DIR[0]=/my/dir1
DIR[1]=/my/dir2

for i in 0 1 ; do

    echo "${DIR[i]}"

done


corp769 05-01-2011 01:38 AM

Quote:

Originally Posted by David the H. (Post 4342506)
spazticclown is right. I forgot about the indirect referencing needed to get the actual values.

However you don't really need to use exec here. Bash has an indirect reference pattern. Try this:
Code:


export D1_DIR=/my/dir1
export D2_DIR=/my/dir2

for i in 1 2 ; do

    Dn_DIR="D${i}_DIR"
    echo "${!Dn_DIR}"  #indirect referencing done with ${!var}

done


Damn, that's how you do that. It's been a while since I had to do anything like that, and I wanted to reply so bad before, but couldn't think for the life of me. Thanks dude!

maddyfreaks 05-01-2011 11:22 AM

Thanks
 
Thanks a lot spazticclown, David...

hopefully you saved my life and my time.

once Again Thanks

spazticclown 05-02-2011 01:25 PM

I like David's option using the array, very clean and simple... mine is just hobbled together because I was trying to do the same thing and failing to find a good answer, bookmarked this thread.


All times are GMT -5. The time now is 09:43 AM.