0

I have written a shell script. The for loop runs twice. Not sure how to fix this.

Can any one please assist me in resolving the issue?

Code:

#!/bin/sh
mkdir -p C:/Users/spatro/Desktop/attachment_backup
feed=`sqlplus -s <<EOF
$1/$2
whenever sqlerror exit sql.sqlcode rollback
SET SERVEROUTPUT ON
SET VERIFY OFF
SET FEEDBACK OFF
SET HEADING OFF
SET PAGESIZE 0
select attach_sub_folder from DC_Purge_Files_log where entity_type = 'Attachment' and delete_complete = 'N';
/
exit
EOF`
for counter in $feed
do
mkdir -p C:/Users/spatro/Desktop/attachment_backup/$counter
echo "Sub folder created "$counter
done

2 Answers 2

0

Rule number one of safe shellscripting: Always use quotes

Your loop would have executed once, had the command substitution (the stuff in backticks) and $feed variable been quoted. As it stands, those are asking for trouble! Specifically, word splitting and glob expansion.

If you would like some tooling:

Shellcheck would answer your question, and Naziquote would fix it (see this question).

0

If you want the for loop to execute just once, use a break statement before "done"

for counter in $feed do mkdir -p C:/Users/spatro/Desktop/attachment_backup/$counter echo "Sub folder created "$counter break done

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.