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..... |
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. |
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 More references can be found at google, using "bash nesting variables" Hope this helps. |
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:
|
One more thing. You can avoid all this indirect stuff completely if you simply use an array variable instead.
Code:
#!/bin/bash |
Quote:
|
Thanks
Thanks a lot spazticclown, David...
hopefully you saved my life and my time. once Again Thanks |
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. |