我正在尝试迭代shell脚本中每个循环的嵌套,该脚本给出了预期的结果,但同时提供了额外的输出。
下面是代码。
for container in ${DB_1} ${DB_2}
do
for container_dump in ${DB_1_bkup} ${db_2bkup}
do
echo "${container} and backups/${container_dump}_dump_`date +%Y-%m-%d"_"%H_%M_%S`.dump"
done
done
我得到的输出是
DB_1 and backups/DB_1_bkup_dump_`date +%Y-%m-%d"_"%H_%M_%S`.dump
DB_1 and backups/DB_2_bkup_dump_`date +%Y-%m-%d"_"%H_%M_%S`.dump
DB_2 and backups/DB_1_bkup_dump_`date +%Y-%m-%d"_"%H_%M_%S`.dump
DB_2 and backups/DB_2_bkup_dump_`date +%Y-%m-%d"_"%H_%M_%S`.dump
我期望的是
DB_1 and backups/DB_1_bkup_dump_`date +%Y-%m-%d"_"%H_%M_%S`.dump
DB_2 and backups/DB_2_bkup_dump_`date +%Y-%m-%d"_"%H_%M_%S`.dump
有人可以帮忙吗?
答案 0 :(得分:1)
如果我正确理解了您的问题,则不需要两个嵌套循环...
只需从bkup变量获取容器名称...
for container_dump in ${DB_1_bkup} ${db_2bkup}
do
if [ "${container_dump}" = "${DB_1_bkup}" ]; then
container=${DB_1}
else
container=${DB_2}
fi
echo "${container} and backups/${container_dump}_dump_`date +%Y-%m-%d"_"%H_%M_%S`.dump"
done