0
#!/bin/bash
a=10
b=5
p=60
while [[ $p -ne 0 ]];
do 
  t=$(date)
  if [[ $a -ne $b ]]
  then
      c=$((c+1))
  fi
  p=$((p-1))
  sleep 1
done
if [[ $c -ne 0 ]];
then
    echo "running in $t"
    c=0
fi

I have tried adding space and all. Still can't figure out.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Just
  • 11
  • 1
  • What does _"I have tried adding space and all"_ mean? What gave you the idea to _"adding space and all"_? What was that supposed to fix? Any hints may give us information that can solve your problem. – Ted Lyngmo Jan 28 '23 at 01:55
  • Fyi: I ran the script for a minute and it printed `running in lör 28 jan 2023 02:55:04 CET` which was about what I expected it to print. – Ted Lyngmo Jan 28 '23 at 01:57
  • 1
    [Shellcheck](https://www.shellcheck.net/) finds no problems with the code in the question, and it successfully prints a date when I run it. Best guess is that your code has invisible characters in it that got lost when you copied it into the question. Try copying the code from the question into another file and running that. Also see [How can I debug a Bash script?](https://stackoverflow.com/q/951336/4154375). – pjh Jan 28 '23 at 02:04
  • 1
    You can also try printing the script with `LC_ALL=C cat -v scriptname`, which will convert many normally-invisible characters to visible representations. – Gordon Davisson Jan 28 '23 at 02:38
  • I actually need to run a different condition that will matches the current time and an error word in a logfile. I have replaced the condition segment with a and b comparison. – Just Jan 28 '23 at 11:45

1 Answers1

0

First off, you have some syntax errors.

c=$((c+1))

should be

c=$((c+=1))

Similarly,

p=$((p-1))

should be

p=$((p-=1))

Next, "a" and "b" never change, so they will never be equal !!! That guarantees always working thru 60 values for "p", but that can be achieved without the test comparing "a to "b" (that code segment is superfluous).

The echo stating "running in ..." seems arbitrary and detached from the fact that there is nothing following that which will actually restart the script!

I can only deduce that you did not fully understand what you were trying to do or how to go about it.

So ... I suggest you consider the following guidance.

If you start by writing the code logic

  • in pseudo-code,
  • fine-grained for each task that you are trying to accomplish,
  • in the correct sequence and
  • in the correct context,

then having that worded so that it does exactly what you want it to do ... will, almost explicitly, tell you WHAT you need to code for each of those, not the HOW. The HOW is the nitty gritty of coding.

What you have provided above in your question does not reflect the necessary separation of logical elements or details to address the required task breakdown.

If you give that a try, the solution will almost pop out of the page at you.

Eric Marceau
  • 1,601
  • 1
  • 8
  • 11
  • Those are NOT _syntax error_, It is the POSIX equivalent for incrementing by one e.g. `((c++))` at least in the bash shell. If it is relevant to the OP's code/algorithm, is a different story.. – Jetchisel Jan 28 '23 at 04:52
  • c=$(( c+1 )) Worked. – Just Jan 28 '23 at 11:53
  • Can you please explain the difference between `c=$((c+1))` and `c=$((c+=1))`. The first expression returns `c+1` and the second one increments `c` by `1` and returns `c`. Because in both variants the result is assigned to `c`, there is no difference here. – Wiimm Jan 28 '23 at 15:02
  • When I tried c=$((c+1)) on my Ubuntu MATE 20.04, GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu), it did not work for me! – Eric Marceau Jan 28 '23 at 16:11
  • @EricMarceau How exactly did you test it, and how did it fail? I don't have that exact OS or bash version, but `c=$((c+1))` works for me on a bunch of OSes and bash versions from 3.2.57 to 5.1.16. I also don't see any way it can reasonably fail (assuming `$c` is an integer, it doesn't overflow, etc). – Gordon Davisson Feb 01 '23 at 01:47