This bash script extracts a random line from a given (text) file. This is a cheap "fortune" replacement. Goes well with a funny quotes file in you .bashrc :)
-
#!/bin/bash
-
-
# randomline : extracts a random line from a text file
-
# Ozh, ozh@planetquake.com
-
-
if [ -z $1 ] ; then
-
echo "Usage : $0 <file>"
-
exit 0
-
fi
-
-
NB_LINES=$(expr $(wc -l $1 | sed -e 's/ *//' | cut -f1 -d " "))
-
-
NB_RAND=0
-
while [ "$NB_RAND" -eq 0 ]
-
do
-
NB_RAND=$(expr $RANDOM \% $NB_LINES)
-
done
-
-
sed -n "${NB_RAND}p;${NB_RAND}q" $1
Related posts
Metastuff
No Comment yet
No comments yet. You could get "First Post!" which would bring you fame, luxury cars and chicks.
This entry "Random Line" was posted on 25/01/2004 at 10:12 pm and is tagged with Bash, Code, Linux
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site