Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [tlug] random in bash [Kmail Notification]



OK, I wrote a bash script that will print one randomly chosen line from a
specified file. 

Eric wanted Kmail to play a randomly picked soundfile every time a mail
arrived. So, if there's a text file listing all the sound files to choose
from (filelist.txt), you could call this script 'randline' and do 

>$ artsplay `randline filelist.txt`

----------------------------------------
#!/bin/bash
#prints one randomly chosen line from a specified file

INFILE=$1

#all that piping is because wc -l prints formatted output instead 
#  just a single number
LINECOUNT=`wc -l $INFILE | tr -s ' ' | cut -d ' ' -f 2`

# RANDOM returns 0 - 32767, so normalize it to 1 - LINECOUNT
let LINENUM=(1+($RANDOM%$LINECOUNT))

head -n $LINENUM $INFILE | tail -n 1
# end of script
----------------------------------------

Paul Richter
RichterPN@example.com

Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links