Martin Drozdik
Martin Drozdik

Reputation: 13323

How can I substitute a name of a variable into a file name within a command?

I am trying to make a script that runs through all the machines available and saves some data into my home folder.

machines=`cat $OAR_FILE_NODES | uniq`
for machine in ${machines}
do
    echo "connecting to:" ${machine}
    oarsh ${machine} 'tar -zcvf data_${machine}.tar.gz /tmp/data'
done 

The problem is that all data gets saved to data_.tar.gz archive, overwriting it several times.

How can I make the shell substitute the variable machine into the command passed to oarsh?

Upvotes: 0

Views: 57

Answers (1)

fedorqui
fedorqui

Reputation: 290305

As seen on the comments, it was about the variable not expanding because of the single quote '. By changing to double quotes " the variable is expanded properly:

oarsh ${machine} "tar -zcvf data_${machine}.tar.gz /tmp/data"
                 ^                                          ^

Note that when using a single quote, what oarsh would receive is the literal:

tar -zcvf data_${machine}.tar.gz /tmp/data

while using double quotes would provide the $machine value replaced:

tar -zcvf data_THE_MACHINE.tar.gz /tmp/data

Upvotes: 1

Related Questions