0

I want to run command one by one, for change the names of the directories on the server.

When I run script, directories renamed in server 1. But, directories are not found in server 2.

What the error could be in the script?

Script;

#!/bin/bash

mach_directory=/home/user/example
erase_dir1=cache
erase_dir2=tmp


for i in {0..10}
do
    user=user
    server=$(ssh $user@server$i hostname)

        ssh $user@$server find $mach_directory -type d -name $erase_dir1 ! -path "*Admin/$erase_dir1*" -print0 | while IFS= read -r -d '' file ; do mv "$file" "${file}_$(date +%d%m%Y)"; done

        ssh $user@$server find $mach_directory -type d -name $erase_dir2 ! -path "*Admin/$erase_dir2*" -print0 | while IFS= read -r -d '' file ; do mv "$file" "${file}_$(date +%d%m%Y)"; done
done
psce
  • 1
  • 1
  • You definitely want to look up into ansible project or maybe python fabric module. This products will make easy some of tasks like one you have mentioned. It's not actually answer to this one specific question, but i think it would help you. – Navern Aug 23 '14 at 22:44

2 Answers2

0

You either need to do A LOT of escaping in there as much of your code is being executed locally, on the machine where you run the script, either put all that script after "ssh $user@$server" in a file, copy it to all severs and execute it.

Also, you may want to take a look at parallel ssh.

EDIT:

Everything that has a $ in this case, as well as the pipes and equals are going to be executed locally on the machine where you run the script. This means that it will execute only the find command on the remote computer and the rest of the script locally.

Florin Asăvoaie
  • 7,057
  • 23
  • 35
0

EDIT: I've swapped the quotes.

Quote the command after the ssh arguments

ssh $user@$server "find $mach_directory -type d -name $erase_dir1 ! -path '*Admin/$erase_dir1*' -print0 | while IFS= read -r -d '' file ; do mv '$file' '${file}_$(date +%d%m%Y)'; done"

(and i changed the quotes from double to single, inside the string.)

thelogix
  • 389
  • 1
  • 7