I am trying to print a random line from a text file that has 10 lines. The problem that I run into is that it sometimes picks the number "0" which is not a line in the text file. How would I have it pick a random number between 1-10?

#!/bin/bash

RANGE=10
number=$RANDOM
let "number %= $RANGE"
NAME=$(awk 'NR=='$number'{print;exit}' /home/names)
	echo "$NAME"

Thanks to anyone who can help.

The range as it stands will run from 0 to 9
So add 1 to it, to get 1 to 10

Thanks!

#!/bin/bash

RANGE=10
number=$RANDOM
let "number %= $RANGE"
NAME=$(awk 'NR=='$number+1'{print;exit}' /home/eric/names)
	echo "$NAME"

do it using awk's srand() and rand().

awk 'BEGIN{
  srand()
  num=int(rand() * 10) +  1
}
NR==num' file
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.