0

So I have this shell script that I think should run a given number of times, sleep then resume, and output the results to a log file

#!/bin/bash

log=/path/to/file/info.log
a=$(COMMAND1 | cut -d : -f 2)
b=$(COMMAND2 | grep VALUE| cut -c 7,8)

for i in {1..4}
 do
         echo "Test" $i  >> $log
         date >> $log
         echo $a >> $log
         echo "$((-113 + (($b * 2)))) VALUE" >> $log
         sleep 60
 done

When I run ps -ef | grep scriptname.sh it seems the script does run. Executes once then the PID is gone as if the run has completed.

I have tested the script and know that it is running and capturing the data I want. But I do not understand why its not incrementing and not sure why its ending earlier than expected. Thanks for any input

info.log output sample

Test {1..4}
DATE IN UTC
EXPECTED VALUE OF a
EXPECTED VALUE OF b
mcv110
  • 13
  • 4
  • 1
    Btw.: Please fix your shebang. – Cyrus Aug 27 '21 at 19:48
  • Show how to start the script. – Cyrus Aug 27 '21 at 19:50
  • fixed thanks. from script path> ./scriptname.sh & – mcv110 Aug 27 '21 at 19:52
  • Please add content of /path/to/file/info.log to your question (no comment). – Cyrus Aug 27 '21 at 19:57
  • you mean what gets output into the log? that looks like this....never Test 1, Test 2, Test 3....always Test {1..4}...added as edit to original post – mcv110 Aug 27 '21 at 20:06
  • Then you're running your script with `sh` instead of `bash`. Running `sh yourscript` makes it be interpreted as a `sh` script (ignoring the `#!/bin/bash` shebang), so `{1..4}` is just the string `"{1..4}"` instead of expanding to `"1" "2" "3" "4"`. – Charles Duffy Aug 27 '21 at 20:19
  • Just run your script with `/path/to/yourscript`, not `sh yourscript` (and [don't use `.sh` extensions on executable scripts at all](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/), and _especially_ not on things that are bash scripts instead of sh scripts; filename extensions should be used for shell _libraries_, not shell _scripts_). – Charles Duffy Aug 27 '21 at 20:20
  • Please add output of `echo "$BASH_VERSION"` and `echo "$-"` to your question. – Cyrus Aug 27 '21 at 20:54
  • As a note -- it's more efficient to put `>>"$log"` immediately after `done` (and thus open the output file just once and keep it open for the whole loop) instead of `>>"$log"` at the end of each command inside the loop (opening it, writing one command's output, closing it, then re-opening it again on the next line with `>>"$log"` on it, as the current code does). – Charles Duffy Aug 27 '21 at 23:20

0 Answers0