You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
749 B
27 lines
749 B
#!/bin/bash
|
|
|
|
# If no directory given, use the current one.
|
|
dir=""
|
|
if [[ "$1" == "" ]]; then
|
|
dir="."
|
|
else
|
|
dir="$1"
|
|
fi
|
|
|
|
# arrn = number of files in directory + 1 (header line)
|
|
arrn=$(ls "$dir" | wc -l)
|
|
# Add one for sed:
|
|
# sed 11q will print the first 10 lines, but I want the 11th
|
|
# Add one for % modulous:
|
|
# 10 mod 10 == 0, but I need 10 to be a valid random number
|
|
((arrn += 2))
|
|
rand=$(($RANDOM % $arrn))
|
|
# Add to rand until it is at least two:
|
|
# 0 will fail, and 1 will print the header line of ls -l
|
|
while [[ "$rand" -lt 2 ]]; do
|
|
((rand += 1))
|
|
done
|
|
# List the dir again, get the $rand line, grab the tail of that,
|
|
# then print the 9th element of the line (the file/dir name).
|
|
ls -l "$dir" | sed "${rand}q" | tail -n1 | awk '{print $9}'
|