Randomly select a folder using Shell Scripting
Hi,
There are five folders in a folder and I want to select one randomly. I used this command: Code:
rand1="${loc[RANDOM % ${#loc[@]}]}" Can anybody help me to select only a directory, not files? Thanks in advance! |
RANDOM is a built-in variable that returns an integer between 0 and 32767 every time it is referenced. If you have the names of the folders in the array loc, the expression
Code:
$RANDOM % ${#loc[@]} |
Quote:
Code:
loc=(/root/shared_storage/*) |
I am not much familiar with this command.. But wrote a script for you. See if it useful for you.
Quote:
Verified ======== Quote:
|
Quote:
Code:
loc=($(find . -maxdepth 1 -type d)) |
Thanks ssrameez! But I don't know to embed perl with my script. :(
Anyway Code:
if [ -f $rand1 ]; then Thanks again you two who tried to help! :) |
Quote:
Code:
loc=( something ) Code:
loc=(/root/shared_storage/*/) |
Thank you colucix. Didn't know the facts you mentioned :)
|
All times are GMT -5. The time now is 01:27 AM. |