D
D
dimjkee902020-12-12 01:42:02
bash
dimjkee90, 2020-12-12 01:42:02

How to repeat a loop in bash?

Good afternoon!
Tell me, please, what to read and how can it be implemented and is it possible to do this in bash?
You need to constantly check the variables, if the variables are equal to
$1 = $2 ,
then execute the command I need and continue comparing the variables.
If they are equal, then continue comparing them and do nothing until they are not equal,
if they are not equal to
$1 != $2
Then repeat everything from the beginning

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hint000, 2020-12-12
@dimjkee90

Based on the comments, something like this

#!/bin/bash
flag=0
while true; do
  date=`date |awk'{print $3, $4}'|sed's/.\{3\}$//'`
  file=`date -r /var/log/nginx/access.log |awk'{print $3, $4}'|sed's/.\{3\}$//'`

  if [ "$date"="$file" ];
  then
      if [ "$flag"="0" ];
      then 
        flag=1
        command_name
        echo "Сервис перезапущен"
      fi
  else
    flag=0
    echo "Сервис не перезапущен"
  fi
done

PS I almost missed the jamb: square brackets must be separated by spaces (in your example they are not separated).
And, for sure, you need to add some kind of pause between checks (a second, a minute, ..), otherwise the check will be done many times per second, out of the blue creating a load.

S
Saboteur, 2020-12-12
@saboteur_kiev

It is necessary to clarify the condition when exactly and how the variables change

You need to constantly check the variables, if the variables are equal to
$1 = $2 ,
then execute the command I need and continue comparing the variables.

At the next comparison, the variables will be equal again - run the desired command again? How many times will the command you need be executed before the variables become unequal?
Describe an example of the desired script in any fictional syntax to make it clearer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question