使用带参shell 脚本 把本地文件批量同步远程服务器
基础位置参数
-
特殊变量:
-
$0
:脚本名称。 -
$1
,$2
, ...:第1、2个位置参数。 -
$@
:所有参数(保留分隔)。 -
$*
:所有参数(合并为单个字符串)。 -
$#
:参数个数。
-
#!/bin/bash
echo "脚本名称: $0"
echo "第一个参数: $1"
echo "第二个参数: $2"
echo "所有参数: $@"
echo "参数个数: $#"
编写带参数的Shell脚本
vim sync_files_parameters.sh
#判断参数个数
if [ $# -lt 2 ]; thenecho "错误:需要至少2个参数!"exit 1
fi
echo "===========同步文件================="
for host_name in bigdata113 bigdata114 bigdata112
do
local_file="$1"
target_dir="$2"
target_user="root"
scp $local_file $target_user@$host_name:${target_dir}
echo "=========scp==$local_file $target_user@$host_name:${target_dir}================="
done
执行shell 脚本
sh sync_files_parameters.sh "/root/training/zookeeper-3.7.2/conf/zoo.cfg" "/root/training/zookeeper-3.7.2/conf/zoo.cfg"