LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Randomly select a folder using Shell Scripting (https://www.linuxquestions.org/questions/linux-newbie-8/randomly-select-a-folder-using-shell-scripting-886823/)

kapz_unlocked 06-17-2011 02:08 AM

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[@]}]}"
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!

colucix 06-17-2011 02:17 AM

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.

kapz_unlocked 06-17-2011 02:28 AM

Quote:

Originally Posted by colucix (Post 4388252)
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:
Code:

loc=(/root/shared_storage/*)
rand1="${loc[RANDOM % ${#loc[@]}]}"

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.

ssrameez 06-17-2011 02:36 AM

I am not much familiar with this command.. But wrote a script for you. See if it useful for you.

Quote:

#!/usr/bin/perl

use strict;
use warnings;

my $dir = $ENV{'PWD'};
opendir(DIR,"$dir") or die "Unable to open dir $dir:$!\n";

my @dirs = grep {!/^\./ && -d "$dir/$_" } readdir(DIR);

my $range = $#dirs;

my $random_number = int(rand($range));

print "Random number $random_number\n";
print "My Random Directory is $dirs[$random_number]\n";

Verified
========

Quote:

./rand_dir.pl
Random number 0
My Random Directory is test1

./rand_dir.pl
Random number 2
My Random Directory is test3

Reuti 06-17-2011 02:45 AM

Quote:

Originally Posted by kapz_unlocked (Post 4388258)
Code:

loc=(/root/shared_storage/*)
rand1="${loc[RANDOM % ${#loc[@]}]}"


Maybe you can use find:
Code:

loc=($(find . -maxdepth 1 -type d))

kapz_unlocked 06-17-2011 02:54 AM

Thanks ssrameez! But I don't know to embed perl with my script. :(
Anyway
Code:

if [ -f $rand1 ]; then
                        rand1="${loc[RANDOM % ${#loc[@]}]}"

within a loop solved the problem!!

Thanks again you two who tried to help! :)

colucix 06-17-2011 03:34 AM

Quote:

Originally Posted by kapz_unlocked (Post 4388258)
Sorry but 'loc' here is not an array. The code comes like:
Code:

loc=(/root/shared_storage/*)
rand1="${loc[RANDOM % ${#loc[@]}]}"

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:
Code:

loc=(/root/shared_storage/*/)
rand1="${loc[RANDOM % ${#loc[@]}]}"


kapz_unlocked 06-21-2011 12:02 AM

Thank you colucix. Didn't know the facts you mentioned :)


All times are GMT -5. The time now is 01:27 AM.