且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

shell readarray命令

更新时间:2022-09-16 17:11:47

declare -a hosts 声明数组
declare -i elements i  声明2个整形变量

readarray -t删除文本结尾的换行符

crontab条目如下:

10 * * * * /root/detection_tool/sync_time.sh /root/detection_tool/time_sync_hosts 

我们用来多台服务器时间同步shell scripts如下:

#!/bin/bash

declare -a hosts
declare -i elements i


readarray -t hosts < $1
elements=${#hosts[@]}

for ((i=0;i<$elements;i++)); do
        echo "${hosts[${i}]}:"
        ssh -i /root/xxx.key root@${hosts[${i}]} 'ntpdate -u time.rightscale.com'
        ssh -i /root/xxx.key root@${hosts[${i}]} 'service ntpd restart'
done

exit 0













本文转自chenzudao51CTO博客,原文链接:http://blog.51cto.com/victor2016/1873620,如需转载请自行联系原作者