 |
|
 |
|
|
 |
Autocreate Directory with a new name |
 |
 |
|
|
05-24-05 11:04 PM
How do I create a directory, and if it fails, if a directory with that
name already exists there, retry with a different name?
I'm a bit confused with how to handle the error resulting from the
"Mkdir" failing, and put that inside the loop, so it knows it failed,
and then retries.
Is a while loop the best thing to use in this case?
Thanks very much
The Nomad.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Autocreate Directory with a new name |
 |
 |
|
|
05-24-05 11:04 PM
On 24 May 2005 10:56:43 -0700, binary-nomad@hotmail.com <binary-nomad@hotmail.com> wrote:[vb
col=seagreen]
> How do I create a directory, and if it fails, if a directory with that
> name already exists there, retry with a different name?[/vbcol]
You could count the ones with similar names - do a
ls /path/to/dir | grep name | wc -l
to see how many directories with 'name' as part of their name exist
in /path/to/dir. Add one to that, (or make the first one without
a number) and mkdir with whatever name and the number that wc gave
you.
> I'm a bit confused with how to handle the error resulting from the
> "Mkdir" failing, and put that inside the loop, so it knows it failed,
> and then retries.
Or, you could make them with a time/date stamp, down to the second if
that's fast enough to not have 2 during the same second...or serialize
during a second if it's more than 1 a second.
> Is a while loop the best thing to use in this case?
Nope. Direct measurement is always preferable to trial and error.
The real question might be "what are you trying to do that you feel
this is necessary".
Dave Hinz
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Autocreate Directory with a new name |
 |
 |
|
|
05-24-05 11:04 PM
binary-nomad@hotmail.com wrote:
> How do I create a directory, and if it fails, if a directory with that
> name already exists there, retry with a different name?
>
> I'm a bit confused with how to handle the error resulting from the
> "Mkdir" failing, and put that inside the loop, so it knows it failed,
> and then retries.
>
> Is a while loop the best thing to use in this case?
>
>
>
> Thanks very much
>
>
>
> The Nomad.
You could use a loop. Set a variable like
TRUE=false
set up a while loop that will run until TRUE == TRUE
When you determain that the directory has been successfully written, change
TRUE to TRUE and it will exit the loop.
Each loop would increase the counter by one. But I would not try to write
the file first. I would want to test if the file was there first using
something like the -f option. Something like
if [ ! -f /path/to/file/<file_name>.$counter ] ; then
note: so I don't get flamed over file names with spaces, something I don't
use, I'll leave it to you to figure out how to format the statement if you
do use filenames with spaces.
Of course there are different reasons for not writing the file. You would
not want to try file names forever so you would need to check if the actual
write was successful. I would suggest you check the return code ${?} to
see
if it failed or not. If you have written your script right and the return
code shows that the write has failed, it is probably for a reason that
trying with a different name would not solve and you should exit the script
with an error message. If the return code indicates the write was
successful, then set TRUE to TRUE.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Autocreate Directory with a new name |
 |
 |
|
|
05-24-05 11:04 PM
Dave Hinz wrote:
> On 24 May 2005 10:56:43 -0700, binary-nomad@hotmail.com
> <binary-nomad@hotmail.com> wrote:
>
> You could count the ones with similar names - do a
> ls /path/to/dir | grep name | wc -l
> to see how many directories with 'name' as part of their name exist
> in /path/to/dir. Add one to that, (or make the first one without
> a number) and mkdir with whatever name and the number that wc gave
> you.
>
Just counting the directories that are similar and adding one has a problem.
Let's say you have 3 directories:
dir_1
dir_2
dir_4
you count to see how many dir* directories you have and get 3. Add one and
try to write the dir dir4...
If you wanted to use this approach, I would suggest pulling out just the
number part of the name, doing a sort and taking the hightest number and
adding 1...
Just a thought.
>
> Or, you could make them with a time/date stamp, down to the second if
> that's fast enough to not have 2 during the same second...or serialize
> during a second if it's more than 1 a second.
This would be the way I would approach the problem depending on the
requirements of the job. I can't think of a reason why I would not find
this acceptable, but I have seen pointy haired bosses that want things done
in particular ( in the most negative connotation of "particular") ways.
>
>
> Nope. Direct measurement is always preferable to trial and error.
> The real question might be "what are you trying to do that you feel
> this is necessary".
Agreed, as long as direct mesurement gives you something reliable to get the
job done.!
>
> Dave Hinz
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Autocreate Directory with a new name |
 |
 |
|
|
05-24-05 11:04 PM
On Tue, 24 May 2005 at 17:56 GMT, binary-nomad@hotmail.com wrote:
> How do I create a directory, and if it fails, if a directory with that
> name already exists there, retry with a different name?
>
> I'm a bit confused with how to handle the error resulting from the
> "Mkdir" failing, and put that inside the loop, so it knows it failed,
> and then retries.
>
> Is a while loop the best thing to use in this case?
mkdir "$DirName" 2>/dev/null || {
base=$DirName
n=1
while :
do
DirName=$base-$n
mkdir "$DirName" && break
n=$(( $n + 1 ))
done
}
This could be made somewhat faster by using a test for the
existence of the directory (or a file of the same name).
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Autocreate Directory with a new name |
 |
 |
|
|
05-24-05 11:04 PM
In comp.unix.shell binary-nomad@hotmail.com:
> How do I create a directory, and if it fails, if a directory with that
> name already exists there, retry with a different name?
> I'm a bit confused with how to handle the error resulting from the
> "Mkdir" failing, and put that inside the loop, so it knows it failed,
> and then retries.
> Is a while loop the best thing to use in this case?
mkdir dir_$((`ls -d dir_* | tail -1 | sed 's/dir_//'`+1))
Sounds difficult at first, but presuming there's at least "dir_1"
available, there's no need to test at all.;) Should work at least
in bash/ksh.
Good luck
--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 82: Yeah, yo mama dresses you funny and you need
a mouse to delete files.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Autocreate Directory with a new name |
 |
 |
|
|
05-24-05 11:04 PM
On 24 May 2005 10:56:43 -0700, binary-nomad@hotmail.com wrote:
> How do I create a directory, and if it fails, if a directory with that
> name already exists there, retry with a different name?
>
> I'm a bit confused with how to handle the error resulting from the
> "Mkdir" failing,
> and put that inside the loop, so it knows it failed,
> and then retries.
Why retry, except for hardware failure, it is not going to heal it's
self unless it is remoted mounted.
> Is a while loop the best thing to use in this case?
I would. You can always test the return code $? for success 0 or
failure -ne 0.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Autocreate Directory with a new name |
 |
 |
|
|
05-25-05 01:48 AM
On Tue, 24 May 2005 at 22:23 GMT, Michael Heiming wrote:
> In comp.unix.shell binary-nomad@hotmail.com:
>
>
>
>
> mkdir dir_$((`ls -d dir_* | tail -1 | sed 's/dir_//'`+1))
>
> Sounds difficult at first, but presuming there's at least "dir_1"
> available, there's no need to test at all.;) Should work at least
> in bash/ksh.
Or, without using ls, tail and sed, in any POSIX shell:
set -- dir_[0-9]*
eval last=\${$#}
mkdir dir_$(( ${last#dir_} + 1 ))
Both of these assume that there is a file or directory called
dir_NUM, where NUM is an integer.
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Autocreate Directory with a new name |
 |
 |
|
|
05-25-05 01:48 AM
Michael Heiming wrote:
> In comp.unix.shell binary-nomad@hotmail.com:
>
>
>
>
> mkdir dir_$((`ls -d dir_* | tail -1 | sed 's/dir_//'`+1))
>
> Sounds difficult at first, but presuming there's at least "dir_1"
> available, there's no need to test at all.;) Should work at least
> in bash/ksh.
>
> Good luck
>
There is a little problem with this aproach. First, let's look at the
directories I have set up for the demo:
$ ls -d dir_*
dir_1/ dir_10/ dir_2/ dir_3/ dir_9/
Notice that I used the same ls format and notice the location of dir_10.
Now the tail part:
$ ls -d dir_* | tail -1
dir_9/
Are we starting to see the problem yet?
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Autocreate Directory with a new name |
 |
 |
|
|
05-25-05 07:48 AM
In comp.unix.shell matt_left_coast <Not@given.org>:
> Michael Heiming wrote:
[..][vbcol=seagreen]
> There is a little problem with this aproach. First, let's look at the
> directories I have set up for the demo:
[..]
> $ ls -d dir_* | tail -1
> dir_9/
> Are we starting to see the problem yet?
mkdir dir_$((`ls dir_* | awk 'NF>0'|sed 's/dir_//'| sed 's/://'| sort -rn |
head -1`+1))
Not really, only your missing imagination fixing the minor problem.;)
(Left beautifying for someone else, but you should get the idea)
--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 334: 50% of the manual is in .pdf readme files
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 09:47 PM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|