bash创建许多目录,如果它不存在于ssh上

时间:2013-02-19 18:08:46

标签: bash ssh directory

我想在远程服务器2上创建许多已存在于server1上的目录。 我在server1上的脚本:

LOG_DIR="/opt/server1/service/logs/"
DEST_HOST="server2"
DEST_USER="logger"
DEST_DIR="/volumes/storage/server1"

LISTEN_DIR=$(find $LOG_DIR -type d | tr '\n' ' ')

ssh $DEST_USER@$DEST_HOST " for Y in $LISTEN_DIR
                do
                    if [ ! -d  $DEST_DIR$Y ]; then
                        mkdir -p $DEST_DIR$Y;
                    fi
                done"

但是在目录/ opt / server1 / service / logs /的server1上还有其他几个目录...... 脚本结果:

[20:00 root@server1:~]# bash -x script.sh
+ LOG_DIR=/opt/server1/service/logs/
+ DEST_HOST=server2
+ DEST_USER=logger
+ DEST_DIR=/volumes/storage/server1
++ find /opt/server1/service/logs/ -type d
++ tr '\n' ' '
+ LISTEN_DIR='/opt/server1/service/logs/ /opt/server1/service/logs/org.service-com.settlement.host.ui-1.0.0.SNAPSHOT /opt/server1/service/logs/com.springsource.server.repository.hosted-2.0.0.RELEASE /opt/server1/service/logs/org.service-com.xml2titp.ws-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.mportal.ws.oracle-1.0.0.SNAPSHOT /opt/server1/service/logs/access /opt/server1/service/logs/com.springsource.server.admin.web-2.0.0.RELEASE /opt/server1/service/logs/org.flex.web.plan-2 /opt/server1/service/logs/org.service-com.admin.host-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.tickets.ws-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.apijson.ws-1.0.0.SNAPSHOT /opt/server1/service/logs/apool_xyz.plan-2 /opt/server1/service/logs/org.service-com.json2titp.host.ui-1.0.0.SNAPSHOT /opt/server1/service/logs/server1 /opt/server1/service/logs/org.service-com.apijson.host.ui-1.0.0.SNAPSHOT /opt/server1/service/logs/org.flex.web.plan-2-org.service-com.flex.bundle.main-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.xyzsettlement.ws-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.host-1.0.0.SNAPSHOT /opt/server1/service/logs/wrapper '
+ ssh logger@server2 'IFS_OLD=  

                for Y in /opt/server1/service/logs/ /opt/server1/service/logs/org.service-com.settlement.host.ui-1.0.0.SNAPSHOT /opt/server1/service/logs/com.springsource.server.repository.hosted-2.0.0.RELEASE /opt/server1/service/logs/org.service-com.xml2titp.ws-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.mportal.ws.oracle-1.0.0.SNAPSHOT /opt/server1/service/logs/access /opt/server1/service/logs/com.springsource.server.admin.web-2.0.0.RELEASE /opt/server1/service/logs/org.flex.web.plan-2 /opt/server1/service/logs/org.service-com.admin.host-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.tickets.ws-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.apijson.ws-1.0.0.SNAPSHOT /opt/server1/service/logs/apool_xyz.plan-2 /opt/server1/service/logs/org.service-com.json2titp.host.ui-1.0.0.SNAPSHOT /opt/server1/service/logs/server1 /opt/server1/service/logs/org.service-com.apijson.host.ui-1.0.0.SNAPSHOT /opt/server1/service/logs/org.flex.web.plan-2-org.service-com.flex.bundle.main-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.xyzsettlement.ws-1.0.0.SNAPSHOT /opt/server1/service/logs/org.service-com.host-1.0.0.SNAPSHOT /opt/server1/service/logs/wrapper  
                do
                    if [ ! -d  /volumes/storage/server1 ]
                    then
                        mkdir -p /volumes/storage/server1;
                    fi;
                done;'

1 个答案:

答案 0 :(得分:3)

尝试这样做:

find local_dir -type d -print0 |
ssh remote 'while IFS="" read -r -d "" a; do mkdir -p "remote_dir/$a"; done'

或者如果你喜欢单行:

find local_dir -type d -print0 | ssh remote 'while IFS="" read -r -d "" a; do mkdir -p "remote_dir/$a"; done'

说明

  • find local_dir -type d:从local_dir打印所有目录,以便STDOUT
  • |Unix pipe:将STDOUT的最新命令分支STDIN
  • ssh remote 'remote_command':ssh到远程服务器,然后执行命令。需要单'个引号来阻止shell插入$a变量
  • while read a; do mkdir -p remote_dir/$a; done:远程命令,它在STDIN
  • 上读取
  • mkdir -p,无需检查任何目录的存在