It should work fine with:
while [[ "$i" -le "$number" ]]; do
^^ ^^
This requires bash, but since your shebang is #!/bin/bash
, your t.sh
script will run with bash, no matter its '.sh
' extension.
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.
Initializing i
to 0 is better/cleaner, but [[ "" -le "$number" ]]
(on the first loop, still "works" (in that an empty string is consider "lower or equal" to an non-empty "$number
" string, and remains in the loop)
That being said, the more correct form/usage for that script would be (using arithmetic comparison):
#!/bin/bash
range=100
number=$((RANDOM % range))
i=0
while (( i <= number )); do
echo "DATE: ${i}" >> date.txt
i=$((i+1))
done
Or, using a c-style loop
#!/bin/bash
range=100
number=$((RANDOM % range))
for ((i = 0 ; i < number ; i++)); do
echo "DATE: ${i}" >> date.txt
done
All this assumes bash
, but as seen here, since for (( ... ))
syntax isn't POSIX, it would not work with Alpine Linux or other OS where sh
links to ash (Almquist shell) or Dash.
In that latter case:
#!/bin/bash
range=100
number=$((RANDOM % range))
for i in $(seq 0 $number); do
echo "DATE: ${i}" >> date.txt
done