Shell脚本:分发文件到各个集群节点

找一个全局目录/root/bin 写脚本

javascript 复制代码
touch xsync
chmod 777 xsync
javascript 复制代码
#!/bin/bash

#作者:ldj
#时间:2024-10-15
#描述:拷贝文件

#1. 判断参数个数
if [ $# -lt 1 ]
    then
       echo "Error: Not Enough Argument!"
       exit
fi

#2.遍历集群所有机器
space=">>>>>>>  "
slash="/"
host_arr=(hadoop10 hadoop11 hadoop12)

for host in ${host_arr[@]}
do	
	#3.获取参数列表(文件)
	for file in $@
	do
	    #4.文件是否存在
	    if [ -e $file ]
		    then
		        #5.当前文件父se目录
		        pdir=$(cd -P $(dirname $file); pwd)
			    #6.当前文件名称
			    fname=$(basename $file)
			    echo $pdir$slash$fname$space$host
				
			    ssh $host "mkdir -p $pdir"
		        rsync -av $pdir/$fname $host:$pdir
		    else
		        echo $file does not exists!
	    fi
	done
done

1.从Window 直接拷贝到Linux可能会报错:/bin/bash^M: bad interpreter: No such file or directory

执行脚本前先 执行

javascript 复制代码
sed -i 's/\r$//' xsync

2.还有一个注意配置域名解析 (重启生效)

相关推荐
zzzzzz31021 小时前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode21 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220702 天前
如何搭建本地yum源(上)
运维
A小辣椒3 天前
TShark:Wireshark CLI 功能
linux
A小辣椒3 天前
TShark:基础知识
linux
AlfredZhao3 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao4 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334664 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪4 天前
linux 拷贝文件或目录到指定的位置
linux
大树885 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai