我有以下脚本,我认为它应该遍历我读取的文件内容,但由于某种原因它在第一次迭代后退出。
#!/usr/bin/bash
scriptDir=/soft/automation/scripts
cd $scriptDir
#LOOP THROUGH THE LIST OF HOSTS AND TEARDOWN/REBUILD EACH
IFS=$IFS,
while read -r name ip vlan image; do
echo "$(date) : Beginning teardown of host ${name}_${vlan}..."
echo "$(date) : Executing command: ./deploy_VM_PureFlex_Nstar.sh -d -n ${name}_${vlan}"
./deploy_VM_PureFlex_Nstar.sh -d -n ${name}_${vlan}
exitCode=$?
if [[ $exitCode -eq 0 ]]; then
echo "$(date) : Teardown of host ${name}_${vlan} completed successfully (exit code: $exitCode). Sleeping for 60 seconds..."
else
echo "$(date) : Teardown of host ${name}_${vlan} completed with errors (exit code: $exitCode). Sleeping for 60 seconds..."
fi
sleep 60
echo "$(date) : Beginning rebuild of vm for host $name"
echo "$(date) : Executing command: ./deploy_VM_PureFlex_Nstar.sh -a -n ${name} -i ${ip} -v ${vlan} -r ${image} -p normal"
./deploy_VM_PureFlex_Nstar.sh -a -n ${name} -i ${ip} -v ${vlan} -r ${image} -p normal
exitCode=$?
if [[ $exitCode -eq 0 ]]; then
echo "$(date) : Rebuild of vm for host ${name}_${vlan} completed successfully (exit code: $exitCode). Sleeping for 60 seconds..."
else
echo "$(date) : Rebuild of vm for host ${name}_${vlan} completed with errors (exit code: $exitCode). Sleeping for 60 seconds..."
fi
sleep 60
done < ${scriptDir}/hosts.txt
hosts.txt文件的格式。
host01,192.168.1.1,5,BaseImg_DB_20150528
host02,192.168.1.2,5,BaseImg_APP_20150528
我觉得我错过了一些愚蠢的东西。问题是如果我注释掉它调用另一个脚本的2行它就像我期望的那样循环。调用脚本是个问题吗?
答案 0 :(得分:0)
您的hosts.txt
文件不符合POSIX标准,并且文件末尾不包含newline
。你需要:
while read -r name ip vlan image || [ -n "$image" ]; do
为了阅读最后一行。见Why should files end with a newline?