You copy the files to your current directory, whatever
that is at the moment. You probably should use
"/bin/cp -f $dirname/$file $dirname/malign.top".
daniel. | [reply] |
system() has a return code. You should always test it after a command.
--Bob Niederman, http://bob-n.comAll code given here is UNTESTED unless otherwise stated.
| [reply] |
You could try the following shell script:
#!/bin/bash
do_modeling()
{
FILES=`ls ./`;
for f in $FILES;; do
echo $f
cp -f $f malign.top
cp -f $f get-model.top
/usr/local/modeller6v2/bin/mod6v2 malign.top
/usr/local/modeller6v2/bin/mod6v2 get-model.top
done
}
do_modeling
do_modeling
| [reply] [d/l] |
hola.
i know why your script does not work.
it is not "system", not cp, not the other program that is used. tho the advice pertaining to these is good, they are not the culprit. the culprit is readdir
here is why.
the first two 'files' returned by readdir are "." and "..".
when you use readdir, you should probably do something like: @fils = grep { -f $dirname . $_ } readdir DIR;
as it is, you start with .
you increment m after a failed cp:
you go on to the second file, .., and increment m again
and before you know it, your counter has reached the limit
at which your do loop is broken out of.
if you use the construct above, all will be well.
best wishes,
...wufnik
-- in the world of the mules there are no rules --
| [reply] [d/l] |
I think what you meant to suggest was:
@fils = grep { -f "$dirname/$_" } readdir DIR;
(I suspect that the value of $dirname does not end in a slash, so just concatenating it with the file name would be wrong.) | [reply] [d/l] |
You should only use the single-argument form of system if you have complete control over the strings that are interpolated in the system call. On the practical side, your current script won't work if there are spaces, quotation marks, or other special characters in the filenames.
But maybe more importantly, passing unchecked data directly to the shell is a security no-no. In your case, anyone who can create a file within the working directories of this script can execute any command as the user executing the script. So for example, if you plan to use this script from root's crontab and parse through some data files in joe's home directory, joe could very quickly do some nasty things (i.e, touch "; cat /etc/shadow | mail joe ;")
OK, maybe this example is a little far fetched for this innocuous script, but it's a good habit to avoid single-argument system... if only for the reason that spaces and quotation marks in filenames won't Just Work.
Better to use the multi-argument form which bypasses the shell, solving all of these problems:
system("/bin/cp", "-f", "$dirname/$file", "malign.top");
system("/bin/cp", "-f", "$dirname/$file", "get-model.top");
system("/usr/local/modeller6v2/bin/mod6v2", "$dirname/malign.top");
system("/usr/local/modeller6v2/bin/mod6v2", "$dirname/get-model.top");
Now the arguments get directly passed via exec, and not through the shell, so the filenames can contain any stuff at all (including spaces & quotes) and the script still works as you'd expect (without doing anything unexpected/insecure).
blokhead | [reply] [d/l] |
You say you "want the whole process to occur twice", which I assume means that you want two iterations over this sequence:
- open the directory
- make two copies of every file
- run "mod6v2" on each copy
That actually looks like you're running the one process four times on each file (each set of data) in $dirname. Okay, whatever.
But if this is really what you want, you should move the "$m++" so that it is outside the while loop. Also, it would probably look more sensible if you moved the "closedir(DIR)" so that it is inside the do ... until loop.
(update: Forgot to mention: please use proper indentation -- it really does help.) | [reply] |
First I would suggest that instead of using the
system call that you use File::Copy instead
to copy the files.
Secondly as pointed out elsewhere in the thread check
the return code generated by system where you are executing
/usr/local/modeller6v2/bin/mod6v2 for errors.
Peter L. Berghold -- Unix Professional Peter at Berghold dot Net |
| |
Dog trainer, dog agility exhibitor, brewer of
fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and
a good Belgian ale in your chalice. |
| [reply] |