[SOLVED] Randomly select a folder using Shell Scripting
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
There are five folders in a folder and I want to select one randomly.
I used this command:
Code:
rand1="${loc[RANDOM % ${#loc[@]}]}"
but it selects files too. I have no idea about this code even. I found it on a forum.
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[@]}
gives the remainder of the division between a random integer and 5, that is a number between 0 and 4. This is used as index to select an element of the array. What do you mean by "it selects files too"? There is no reference to files here, only a reference to the content of the array loc. If it contains only the names of the directories, you will select one of them randomly.
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[@]}
gives the remainder of the division between a random integer and 5, that is a number between 0 and 4. This is used as index to select an element of the array. What do you mean by "it selects files too"? There is no reference to files here, only a reference to the content of the array loc. If it contains only the names of the directories, you will select one of them randomly.
Sorry but 'loc' here is not an array. The code comes like:
This gives me paths of different files/ folders in '/root/shared_storage/' folder. what I need to do is to avoid getting a file path to rand1. I need only a directory.
This gives me paths of different files/ folders in '/root/shared_storage/' folder. what I need to do is to avoid getting a file path to rand1. I need only a directory.
Actually loc is an array, since
Code:
loc=( something )
is the syntax to assign arrays in bash. To retrieve only directories using a wildcard, put a trailing slash:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.