Random Filename Chooser
This is a rather peculiar little script with a purpose that may not be immediately apparent. The script will look for files in the specified folder; it will then pick one at random from the list of matches and create a soft link to it. How is this useful, you ask? There are situations where you want to access the same file name but get something different once in a while.
Imagine you access “default.htm”, which is a dynamically updated link to a random page from a given set. Or, perhaps, you have an application use a particular file for input and you want to rotate the input sources at random, selecting from a specific set of choices.
#!/bin/bash INPUT_DIR=/opt/application/input FILENAME=$(ls $INPUT_DIR | grep ".xml$" | while read FILE do echo "`expr $RANDOM % 1000`:$FILE" done | sort -n| sed 's/[0-9]*://' | head -1) if [ -f ${INPUT_DIR}/default.xml ] then rm ${INPUT_DIR}/default.xml fi ln -s ${INPUT_DIR}/${FILENAME} ${INPUT_DIR}/default.xml
Popularity: 5% [?]
No related posts.


