大数据库入门之脚本批处理

1、执行批处理命令的脚本文件 all.sh

mkdir -p /usr/shell/
vim all.sh
chmod 777 all.sh
#!/bin/bash
echo "------start execyting the shell script------"
# awk 读取hosts文件的第一部分ip地址
hosts=$(cat /etc/hosts | tail -n +3 | awk '{print $1}')
# 传递的指令(参数)
cmd=$*
# for循环读取文件
for host in  ${hosts[*]}
do
    exec="ssh $host $cmd"
    echo $exec
    if eval $exec; then
        echo 'success'
    else
        echo "fail"
    fi
done
# cat file | while read line 读取文件
#cat /etc/hosts | while read line
#do
# echo $line
#done
# awk读取文件
# cat /etc/hosts | tail -n +3 | awk '{print $2}'
echo "----end th executing of the shell script------"

2、执行批处理分发文件的脚本 scp.sh

cd /usr/shell/
vim scp.sh
chmod 777 scp.sh
#!/bin/bash
echo "------start execyting the shell script------"
# awk 读取hosts文件的第一部分ip地址
hosts=$(cat /etc/hosts | tail -n +3 | awk '{print $1}')
# 传递的第一个参数,文件名称
file=$1
# 传递的第二个参数,目录路径
dir=$2
# for循环读取文件
for host in  ${hosts[*]}
do
    exec="scp $file $host:$dir"
    echo $exec
    if eval $exec; then
        echo 'success'
    else
        echo "fail"
    fi
done
# cat file | while read line 读取文件
#cat /etc/hosts | while read line
#do
# echo $line
#done
# awk读取文件
# cat /etc/hosts | tail -n +3 | awk '{print $2}'
echo "----end th executing of the shell script------"
发布了151 篇原创文章 · 获赞 44 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq262593421/article/details/105352639