LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-01-2011, 01:15 AM   #1
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Rep: Reputation: 0
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.....

Last edited by maddyfreaks; 05-01-2011 at 01:26 AM.
 
Old 05-01-2011, 02:18 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.

Last edited by David the H.; 05-01-2011 at 02:24 AM. Reason: as stated
 
Old 05-01-2011, 02:19 AM   #3
spazticclown
Member
 
Registered: Sep 2010
Distribution: Fedora, Android, CentOS
Posts: 91
Blog Entries: 2

Rep: Reputation: 21
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.
 
Old 05-01-2011, 02:30 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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

Last edited by David the H.; 05-01-2011 at 02:31 AM.
 
1 members found this post helpful.
Old 05-01-2011, 02:37 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
 
Old 05-01-2011, 02:38 AM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by David the H. View Post
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!
 
Old 05-01-2011, 12:22 PM   #7
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Original Poster
Rep: Reputation: 0
Thanks

Thanks a lot spazticclown, David...

hopefully you saved my life and my time.

once Again Thanks
 
Old 05-02-2011, 02:25 PM   #8
spazticclown
Member
 
Registered: Sep 2010
Distribution: Fedora, Android, CentOS
Posts: 91
Blog Entries: 2

Rep: Reputation: 21
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script question: Shell script in kde to log in on a server with ssh c4719929 Programming 18 01-31-2011 10:26 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 10:56 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 10:10 PM


All times are GMT -5. The time now is 11:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration