LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 11-22-2012, 02:05 AM   #1
tramni1980
Member
 
Registered: Jul 2006
Location: Tübingen, Germany
Distribution: Slackware64-13.37
Posts: 737

Rep: Reputation: 37
bash while loop does not execute in succession?


Dear Slackers,
I ran the following code, containing a while loop:
Code:
i=1989;
while [ $i -le 2009 ]
do
mkdir -p /local/directory/$i
cd /local/directory/$i
ftp -n host port <<EOF
user username password
prompt off
cd /remote/directory_$i
mget *
bye
EOF
((i++))
done
and I noticed something strange: when I checked the local directory about half an hour after the execution of the script had started, I noticed that the folders 1989, 1990, 1991, 1992 and 1999, 2000, 2001 existed. Nevertheless, some hours later when the script finished, the local directory contained all the folders it should, and it seems that everything is ok. But how is that possible? Isn't the bash loop executed in succession, I mean, I did not expect to see some years skipped at some point during the execution of the script, as was the case with the years from 1993 to 1998?

Any suggestions will be appreciated.
Best regards,
Martin
 
Old 11-22-2012, 03:14 AM   #2
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Rep: Reputation: 16
You can check if directories created successfully and exit if not.
Code:
 mkdir -p /local/directory/$i || exit 1
 
Old 11-22-2012, 03:38 AM   #3
tramni1980
Member
 
Registered: Jul 2006
Location: Tübingen, Germany
Distribution: Slackware64-13.37
Posts: 737

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by scoban View Post
You can check if directories created successfully and exit if not.
Code:
 mkdir -p /local/directory/$i || exit 1
Yes, I could. But what is interesting, is that the directories that one could think were not created successfully,
were actually created later.
 
Old 11-22-2012, 04:39 AM   #4
wildwizard
Member
 
Registered: Apr 2009
Location: Oz
Distribution: slackware64-14.0
Posts: 755

Rep: Reputation: 226Reputation: 226Reputation: 226
Just out of curiosity were you using a GUI file manager to check this or the output from ls?
 
Old 11-22-2012, 05:22 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by scoban View Post
You can check if directories created successfully and exit if not.
Code:
 mkdir -p /local/directory/$i || exit 1
Yes and no. When the -p option is used, it is not an error if the directory cannot be created because it already exists:
Code:
c@CW9:~$ mkdir /tmp/tmp
c@CW9:~$ mkdir /tmp/tmp
mkdir: cannot create directory `/tmp/tmp': File exists
c@CW9:~$ mkdir -p /tmp/tmp && echo OK || echo not OK
OK
 
Old 11-22-2012, 05:39 AM   #6
tramni1980
Member
 
Registered: Jul 2006
Location: Tübingen, Germany
Distribution: Slackware64-13.37
Posts: 737

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by wildwizard View Post
Just out of curiosity were you using a GUI file manager to check this or the output from ls?
I used ls. The script is executed on a server, where I log in with ssh.
 
Old 11-22-2012, 05:53 AM   #7
GazL
Senior Member
 
Registered: May 2008
Posts: 3,425

Rep: Reputation: 937Reputation: 937Reputation: 937Reputation: 937Reputation: 937Reputation: 937Reputation: 937Reputation: 937
Are you sure that 1999,2000,2001 weren't left behind by a previous run of your while loop? mkdir is an atomic filesystem operation. There is no way that they should show up out of sequence.
 
Old 11-22-2012, 06:45 AM   #8
tramni1980
Member
 
Registered: Jul 2006
Location: Tübingen, Germany
Distribution: Slackware64-13.37
Posts: 737

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by GazL View Post
Are you sure that 1999,2000,2001 weren't left behind by a previous run of your while loop? mkdir is an atomic filesystem operation. There is no way that they should show up out of sequence.
Yes, I am sure that there were no left overs, as I either delete everything or nothing. But, as I come to think about it,
I fiddled with screen a bit, and it is possible that the later directories were created by an older process I had not terminated.
Thank You for your suggestion, for me it important to know that it is not possible for a while loop in bash to run out of succession.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Execute a loop for only 1 minute! sowpra Programming 12 04-25-2012 09:44 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
[SOLVED] Not able to execute FOR loop in shell bkcreddy17 Programming 14 04-19-2011 07:06 PM
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 09:18 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM


All times are GMT -5. The time now is 03:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration