Julien Bensliman
Julien Bensliman

Reputation: 5

Filling limit of a file in bash

I'm trying to get my "loto" file filling up automatically but I'd like to have only 20 lines, so i try this code :

>loto
if [ -f loto ]
then
    nbligne=$(cat loto |wc -l)
    while [ $nbligne -lt 20 ]
    do
        echo $((RANDOM%1000)) >> loto
    done
    nbligne+=$(cat loto |wc -l)
fi

but the loop does not stop and the file is filled indefinitely

Upvotes: 0

Views: 57

Answers (1)

choroba
choroba

Reputation: 241878

You aren't changing the variable inside the loop, so the condition can never be false.

Maybe you just want to output 20 numbers?

#! /bin/bash
for i in {1..20} ; do
    echo $(( RANDOM % 1000 ))
done > loto

Upvotes: 4

Related Questions