View previous topic :: View next topic |
Author |
Message |
shanenin Guru


Joined: 28 Nov 2003 Posts: 578 Location: Rochester, MN U.S.A
|
Posted: Tue May 03, 2005 5:37 pm Post subject: brackets used in bash |
|
|
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 |
|
 |
gentsquash l33t

Joined: 03 Nov 2004 Posts: 753 Location: Still a Gentoo beginner.
|
Posted: Tue May 03, 2005 6:48 pm Post subject: |
|
|
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 |
|
 |
IvanYosifov l33t


Joined: 15 Oct 2004 Posts: 778 Location: Bulgaria
|
Posted: Tue May 03, 2005 6:51 pm Post subject: |
|
|
$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 |
|
 |
tomk Bodhisattva


Joined: 23 Sep 2003 Posts: 7221 Location: Sat in front of my computer
|
Posted: Tue May 03, 2005 7:13 pm Post subject: |
|
|
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 |
|
 |
pjp Administrator


Joined: 16 Apr 2002 Posts: 20609
|
Posted: Tue May 03, 2005 7:20 pm Post subject: |
|
|
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 |
|
 |
shanenin Guru


Joined: 28 Nov 2003 Posts: 578 Location: Rochester, MN U.S.A
|
|
Back to top |
|
 |
armstrtw n00b

Joined: 09 Oct 2003 Posts: 18
|
Posted: Tue May 17, 2005 6:15 pm Post subject: |
|
|
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 |
|
 |
tomk Bodhisattva


Joined: 23 Sep 2003 Posts: 7221 Location: Sat in front of my computer
|
Posted: Tue May 17, 2005 6:40 pm Post subject: |
|
|
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 |
|
 |
TrueDFX Retired Dev

Joined: 02 Jun 2004 Posts: 1348
|
Posted: Tue May 17, 2005 7:33 pm Post subject: |
|
|
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 |
|
 |
armstrtw n00b

Joined: 09 Oct 2003 Posts: 18
|
Posted: Tue May 17, 2005 7:54 pm Post subject: |
|
|
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 |
|
 |
|