Home > Mobile >  Loop does not work normally in git action
Loop does not work normally in git action

Time:11-07

Loop does not work normally in git action.

My task.sh looks like this:

#!/bin/bash

range=100
number=$((RANDOM % range))

while [ "$i" -le "$number" ]; do
    echo "DATE: $1" >> date.txt
done

The result of the above code is:

./task.sh: 6: [: Illegal number:

The code below works fine.

echo "DATE: $1" >> date.txt

I tried the following, but it also gives an error.

#!/bin/bash

range=500
number=$((RANDOM % range))

for ((run=1; run <= number; run  )); do
    echo 'hello'
done

I'm curious how you can make a command like below work normally.

while (random(1-100)); do
     echo 'hello'
done

Best regards!

CodePudding user response:

It should work fine with:

while [[ "$i" -le "$number" ]]; do
      ^^                    ^^

But in its current form, it would be an infinite loop.

You would need to add

i=$((i 1))

That would increment the $i variable, making "$i" -le "$number" work properly.

CodePudding user response:

You have two separate problems:

  1. You are running your script with sh, not bash
  2. You are not assigning i

For the first one, see Why does my Bash code fail when I run it with 'sh'?

For the second, set i=0 in your script before you try to compare $i as a number.

  • Related