首先 cd ~
vim xsync
然後寫入內容
#!/bin/bash
#1. 判斷參數個數
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍歷所有目錄,挨個發送
for file in $@
do
#4.5 判斷文件是否存在
if [ -e $file ]
then
#3. 獲取父目錄
pdir=$(cd -P $(dirname $file); pwd)
#4. 獲取當前文件的名稱
fname=$(basename $file)
#5. 遍歷集羣所有機器,拷貝
for host in hadoop102 hadoop103 hadoop104
do
echo ==================== $host ============= =======
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $USER@$host:$pdir
done
else
echo $file does not exists!
fi
done
修改文件權限
chmod +x xsync
將配置好的文件拷貝到/bin下,這樣哪個用戶都可以使用
sudo cp xsync /bin