天天看點

shell readarray指令

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,如需轉載請自行聯系原作者