-2

I have a following problem I tried to solve but since my knowledge of Bash is limited I ask for help. I have a set of files with inp extension (e.g. name1.inp, name2.inp etc.). These are located in some folder and submitted into queue. At some point computations starts and produces output file (e.g. name1.log, name2.log etc.). Once the computation is finished the output file contains as the last like string "Normal termination". I would like to write a script that does check the status of such computations, that is prints out information if the computation started, is in progress or finished. I know how to check the finished computation (tail command), but unsure how to loop over all inp files in the folder to check if they are log files, and if these are present just to check if "Normal termination" string is present or not. If for inp file the log file is not present the script would also say that the computation is not started yet.

With many thanks in solving this simple problem.

rk85
  • 57
  • 2
  • 6
  • Does this answer your question? [Loop through all the files with a specific extension](https://stackoverflow.com/questions/14505047/loop-through-all-the-files-with-a-specific-extension), [Extract filename and extension in Bash](https://stackoverflow.com/q/965053/4518341), and [How do I tell if a regular file does not exist in Bash?](https://stackoverflow.com/q/638975/4518341) – wjandrea Aug 03 '20 at 16:21
  • You can loop `for inp in path/to/*.inp; do` and then trim the directory (e.g. `inpfile="${inp##*/}"`) now trim the extension (e.g. `inpname="${inpfile%.inp}"`) then just check if that exists in the log dir, (e.g. `[ -e /path/to/log/${inpname}.log ]`) Then just use `if grep -q 'Normal termination'; then ## handle done ...` – David C. Rankin Aug 03 '20 at 16:21
  • 1
    This is not a simple problem, it's at least 3 different problems. I posted some links above to help you out, but please be aware that SO is not a code-writing service, and questions should be about one problem only. Try writing the script yourself, but if you hit a roadblock, *then* ask a question, about that specific problem. See [ask] for advice. – wjandrea Aug 03 '20 at 16:33

1 Answers1

2

Thanks to many useful remarks the script looks as follows, and works very well. Many thanks to ones who commented the previous post.

for inp in *.inp; do

  inpfile="${inp##*/}"
  inpname="${inpfile%.inp}"
  outfile="${inpname}.log"

  if test -f "$outfile"; then
    # echo "$outfile exists."

    if grep -q 'Normal termination' $outfile; then
      echo ">> $outfile FINISHED."
    else
      echo "-- $outfile IN PROGRESS."
    fi

  else
    #echo "$outfile does not exist."
    echo "XX $outfile NOT STARTED."
  fi

done
rk85
  • 57
  • 2
  • 6