For loop executes twice, not sure how to fix
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?
作者: Sameet 的来源 发布者： 2017 年 9 月 15 日
#!/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
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 发布者: 2017 年 9 月 15 日
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 发布者: 2017 年 9 月 19 日