Author |
Topic: Shell Scripting Doubts (Read 22 times) |
|
Maximus Decimus
Plaza Master
    
member is offline

Strength and Honour


Gender: 
Posts: 1366
|
 |
Shell Scripting Doubts
« Thread started on: Jul 29th, 2004, 6:22pm » |
 
|
I was fooling around with a shell script which reads as
Code:for FILE in $HOME/*.pls do cp $FILE ${HOME}/source chmod a+r ${HOME}/source/${FILE} done |
|
The idea is that all *.pls files in home dir are copied to a subdir ~pranand/source and then perms are for all to read.
But what's the deal with the { and } ? When does one use braces while referencing variables? Some rules of thumb please.
PS: The script doesnt work if the second line reads as follows: chmod a+r $HOME/source/$FILE or chmod a+r $HOME/source/${FILE}
but if the first line is cp $FILE $HOME/source
it works fine
I can figure out that if the stmt reads chmod a+r $HOME/source/$FILE it wont work because FILE holds the absolute path which when appended to $HOME/source points to a non existent file.
|
A general who became a slave. A slave who became a gladiator. A gladiator who defied an emperor.
|
|
|
JediMaster
#Global Moderator#
    
member is online

use the force well, you must

Gender: 
Posts: 265
|
 |
Re: Shell Scripting Doubts
« Reply #1 on: Jul 29th, 2004, 9:54pm » |
 
|
on Jul 29th, 2004, 6:22pm, Maximus Decimus wrote:But what's the deal with the { and } ? When does one use braces while referencing variables? Some rules of thumb please. |
|
The {} is used when there could be an ambiguity about how the shell can identify the variable. For example $NUMst can be refering to ${NUM}st (like in 1st if NUM=1) or NUMst variable.
Quote:PS: The script doesnt work if the second line reads as follows: chmod a+r $HOME/source/$FILE or chmod a+r $HOME/source/${FILE}
but if the first line is cp $FILE $HOME/source
it works fine
I can figure out that if the stmt reads chmod a+r $HOME/source/$FILE it wont work because FILE holds the absolute path which when appended to $HOME/source points to a non existent file.
|
|
$HOME/source/$FILE or ${HOME}/source/${FILE} will both expand to a wrong path and the script will not work in any case?? Is it working for you?
|
|
192.91.75.29
|
To see a World in a Grain of Sand
|
|
|
|