0

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?

5
  • What if you use double quotes in the oarsh line? This way, $machine will get expanded. Commented Feb 28, 2014 at 14:09
  • Like this oarsh ${machine} tar -zcvf "data_${machine}.tar.gz" /tmp/data ? Commented Feb 28, 2014 at 14:13
  • Like oarsh ${machine} "tar -zcvf data_${machine}.tar.gz /tmp/data". Commented Feb 28, 2014 at 14:13
  • 1
    @fedorqui Thank you! I tested it and it works! Commented Feb 28, 2014 at 14:21
  • Nice to read that! I posted as an answer to make it clear for future people entering in your question. Mark as accepted if you want. Commented Feb 28, 2014 at 14:28

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.