2

I have a script that generates a .log file when the job finishes. I do not want it to move on until the number of .log files equals the number of .com files. Currently, if I execute this script it will start the loop and then immediately skip actually looping and just continue on with the script.

By what I have gleaned from reading, there is no reason this script shouldn't work. It should be caught on the loop until those two values equal to one another.

$direct is the working directory

 COM="find $direct -maxdepth 1 -type f -name *.com" 
 log="find $direct -maxdepth 1 -type f -name *.log" 
 until [[ $($COM | wc -l) = $($log | wc -l) ]];
 do
    sleep 10
 done 

 echo name EE E+ZPE E+U E+H G | cat > allRE 
 for i in R[R,S]*.out
     do echo $i | cat > $i.energies && 
     #cat temp.txt > $i.energies
     #echo $i is finished
 done 

What I get is that it submits the jobs (which is coded into a part of the script not shown), starts the loop (I can see the sleep command when I look at the processes list), then tries to execute the shown part of the script only to find there are no .out files (which are created once the job has started running on the supercomputer I use), and then continues down the script.

I was asked to run it with -x. I don't know what I am looking for, so here is the section involving the loop:

 ++ COM='find /ddn/home6/r2536/G09Input/test2/low/com -maxdepth 1 -type f -name *.com'
 ++ log='find /ddn/home6/r2536/G09Input/test2/low/com -maxdepth 1 -type f -name *.log'
 +++ find /ddn/home6/r2536/G09Input/test2/low/com -maxdepth 1 -type f -name RR1.com RR2.com RR3.com RR4.com RR5.com RR6.com SS1.com SS2.com SS3.com SS4.com SS5.com SS6.com
 +++ wc -l
 find: paths must precede expression: RR2.com
 Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
 +++ find /ddn/home6/r2536/G09Input/test2/low/com -maxdepth 1 -type f -name '*.log'
 +++ wc -l
 ++ [[ 0 = 0 ]] #if this is saying that there are no .com files, I don't get it, because the script just made and submitted 12 .com files.
 ++ echo name EE E+ZPE E+U E+H G
 ++ cat
 ++ for i in 'R[R,S]*.out'
 ++ echo 'R[R,S]*.out'
 ++ cat
 ++ grep 'SCF D' 'R[R,S]*.out'
 ++ tail -1
 grep: R[R,S]*.out: No such file or directory
  • 1
    There are multiple reasons why this could fail. Please run it with `-x` and log output and errors – that other guy Aug 27 '19 at 20:27
  • Is there a reason you are double bracketed on your `until`? .. This *should* work with the outer brackets removed .. – Zak Aug 27 '19 at 20:28
  • @thatotherguy There are no errors displayed, it just continues on down the script. – Pyrodancer123 Aug 27 '19 at 20:32
  • @Pyrodancer123 Please run it with `-x`. For example, put `set -x` as the second line of the script (after the shebang), and then `exec > /tmp/mylog 2>&1` as the third to log to file (if you are not able to see the output while it's running). `-x` *will* show verbose output even if your loop doesn't do anything. If you don't see it, you should go find it – that other guy Aug 27 '19 at 20:36
  • 2
    One thing I see going wrong: the unquoted expansions `$COM` and `$log` result in `find ... -name *.com` instead of `find ... -name '*.com'`; that is subject to filename expansion, which you don't want there. But really, you want to look at [BashFAQ/004](https://mywiki.wooledge.org/BashFAQ/004) to see how to count files. – Benjamin W. Aug 27 '19 at 20:38
  • @BenjaminW. guessed it. Please rewrite your code to use functions instead of putting commands in string variables, then quote it as per the duplicate. Do not attempt to just quote it in your string, it will not work. (Also note that this error was emitted all along, with or without `-x`: it's a good idea to always listen to what the script is trying to tell you) – that other guy Aug 27 '19 at 20:48
  • @thatotherguy Maybe you should answer the question, because no offense, but I find the "use functions" very vague. – Pyrodancer123 Aug 27 '19 at 20:55
  • Just `findCOMs() { find "$direct" -maxdepth 1 -type f -name "*.com"; }` ... `$(findCOMs | wc -l)` like you would in any other language, instead of using a string variable to store code – that other guy Aug 27 '19 at 20:56
  • @thatotherguy I've never dealt with making functions. I find it kind of rude that you just assumed I had. I'm a chemist by training, not a programmer, but I'm trying to learn. This entire script is me teaching myself. – Pyrodancer123 Aug 27 '19 at 20:58
  • My bad! You were already familiar with flow control and variables, so I figured you were also familiar with other basics. Will be more explicit in the future – that other guy Aug 27 '19 at 21:08
  • I've added another duplicate about counting files. Ignore the accepted answer, the answer by Mat is the way to go. – Benjamin W. Aug 27 '19 at 22:34

0 Answers0