vloubes
vloubes

Reputation: 315

Reading file's lines randomly and logger them

I'm trying to read a text file with a lot of lines and log them by using logger via bash scrip. I achieved to read the file line by line with following code

#!/bin/bash

filename=logger.txt
[[ -f ${filename} ]] || exit 1
x=0
while read -r line; do
            logger "$line";
           sleep 0.1;
    done < $filename
    exit 0

but how to read lines randomly and log them.

I'm trying it with this but without any success

#!/bin/bash

filename=logger.txt
[[ -f ${filename} ]] || exit 1
x=0
while read -r line; do

         logger "%06d %s\n" $RANDOM "$line";
         sleep 0.1;
         done < $filename
         exit 0

How could I implement the RANDOM function into the script or is there another option to achieve it? I tried shuf but it read just one line and then stopped.

Upvotes: 1

Views: 83

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

Given -r flag shuf outputs random lines from input file continuously. So you can do:

while read -r line; do
    logger "$line"
    sleep .1
done < <(shuf -r "$filename")

Upvotes: 1

Related Questions