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
Antti29
  • 2,953
  • 12
  • 34
  • 36
Sameet
  • 11
  • 2

2 Answers2

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).

user2394284
  • 5,520
  • 4
  • 32
  • 38
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

abhishek phukan
  • 751
  • 1
  • 5
  • 16