gpt4 book ai didi

bash - 如果不存在,bash会创建许多目录

转载 作者:行者123 更新时间:2023-12-02 14:15:26 26 4
gpt4 key购买 nike

我想在远程server2上创建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"

但是在server1的/ opt / server1 / service / logs /目录中,还有其他几个目录...
脚本结果:
[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;'

最佳答案

尝试这样做:

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
  • ,无需检查是否存在任何目录
  • 关于bash - 如果不存在,bash会创建许多目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14964422/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com