Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
brackets used in bash
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
shanenin
Guru
Guru


Joined: 28 Nov 2003
Posts: 578
Location: Rochester, MN U.S.A

PostPosted: Tue May 03, 2005 5:37 pm    Post subject: brackets used in bash Reply with quote

I copied thsi script out of a shell scripting book(mildly changed)

Code:

for FILE in $HOME/*
do
    cp $FILE ${HOME}/new/directory
    chmod a+r ${HOME}/new/directory/${FILE}
done


is there any logical reason the first variable HOME is not in brackets, but the next two varible HOMEs are in brackets?
_________________
http://brighteyedcomputer.com
Back to top
View user's profile Send private message
gentsquash
l33t
l33t


Joined: 03 Nov 2004
Posts: 753
Location: Still a Gentoo beginner.

PostPosted: Tue May 03, 2005 6:48 pm    Post subject: Reply with quote

ASIDE: The symbols { } are braces. Brackets are [ ] ;
parentheses are ( ) . One can use "generalized parentheses" for
all these and several others.

For your question, I am not sure. I *think*, in this instance,
that the braces do not make a difference. In other contexts,
they can. If HOME expanded to several words, some of which could
be interpreted as Bash commands, then they would be interpreted
as such in $HOME.

E.g, if $HOME started a command, and it expanded to "if ..."

I *think*, but am not sure, that -in contrast- the contents
of ${HOME} are not interpreted as part of the Bash command
language.

Lets hope that a cognoscento can comment on this.
_________________
Your thread resolved? Putting [SOLVED] in its title helps all Gentooers. (Button "edit" , first post)
Prof. Jonathan LF King, Mathematics dept., University of Florida
Back to top
View user's profile Send private message
IvanYosifov
l33t
l33t


Joined: 15 Oct 2004
Posts: 778
Location: Bulgaria

PostPosted: Tue May 03, 2005 6:51 pm    Post subject: Reply with quote

$VAR and ${VAR} do exactly the same thing. The difference is that if VAR ( the name of the var ) contains underscores ( maybe other things ) you can't use the first form.
Back to top
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Tue May 03, 2005 7:13 pm    Post subject: Reply with quote

They are used to distinguish a variable when it could be ambiguous, take this example:

Code:
foo=foo
foobar=stuff

echo $foobar
echo ${foo}bar


The first echo prints 'stuff', the second one prints 'foobar'.
_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20609

PostPosted: Tue May 03, 2005 7:20 pm    Post subject: Reply with quote

An example is probably the best. Consider $var the lazy method, where ${var} is the "safe" method.

Assume you have a list of files a_file, b_file, c_file, ...
This wouldn't work:
Code:
for f in a b c; do echo $f_file; done

This will:
Code:
for f in a b c; do echo ${f}_file; done
Back to top
View user's profile Send private message
shanenin
Guru
Guru


Joined: 28 Nov 2003
Posts: 578
Location: Rochester, MN U.S.A

PostPosted: Tue May 03, 2005 7:25 pm    Post subject: Reply with quote

thanks for the explanations
_________________
http://brighteyedcomputer.com
Back to top
View user's profile Send private message
armstrtw
n00b
n00b


Joined: 09 Oct 2003
Posts: 18

PostPosted: Tue May 17, 2005 6:15 pm    Post subject: Reply with quote

Can someone help me with a similar bash script question?

I have a list of old filenames and a list of new filenames. I'd like to loop through the list renameing filename[i] to newname[i]. I haven't been able to track down an example of using arrays like this in bash.

Thanks,
Whit

Here's a sketch of the program:

oldnames = { oldname1 oldname2 oldname3}
newnames = {newname1 newname2 newname3}

for(i=0; i < length(oldnames); i++) {
mv oldname newname
}
Back to top
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Tue May 17, 2005 6:40 pm    Post subject: Reply with quote

armstrtw wrote:
Here's a sketch of the program:

oldnames = { oldname1 oldname2 oldname3}
newnames = {newname1 newname2 newname3}

for(i=0; i < length(oldnames); i++) {
mv oldname newname
}


To do it with arrays you'll want something like this:

Code:

oldnames[0]="oldname1"
oldnames[1]="oldname2"
oldnames[2]="oldname3"
newnames[0]="newname1"
newnames[1]="newname2"
newnames[2]="newname3"

for ((i=0; i<${#oldnames[*]}; i++)); do
  mv ${oldname[${i}]} ${newnames[${i}]}
done

_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
TrueDFX
Retired Dev
Retired Dev


Joined: 02 Jun 2004
Posts: 1348

PostPosted: Tue May 17, 2005 7:33 pm    Post subject: Reply with quote

The first part of that can also be written like this in bash:
Code:
oldnames=("oldname1" "oldname2" "oldname3")
newnames=("newname1" "newname2" "newname3")
Back to top
View user's profile Send private message
armstrtw
n00b
n00b


Joined: 09 Oct 2003
Posts: 18

PostPosted: Tue May 17, 2005 7:54 pm    Post subject: Reply with quote

Perfect!

I also found an example of static initialization here:
http://www.tldp.org/LDP/abs/html/arrays.html

array=( zero one two three four five )
# Element 0 1 2 3 4 5

Thanks for your help. I'm off and running now.

Cheers,
Whit
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum