1

I am trying to iterate nested for each loop in a shell script which is giving the expected results but along with extra output.

Below is the code.

 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

What output I am getting is.

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

What I am expecting is

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

Can anyone please help?

1 Answer 1

1

If I correctly understand your question, you don't need two nested loops...
Just get the container name from the bkup variable...

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.