LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-17-2011, 03:08 AM   #1
kapz_unlocked
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS 5.5
Posts: 11

Rep: Reputation: Disabled
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!
 
Old 06-17-2011, 03:17 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 06-17-2011, 03:28 AM   #3
kapz_unlocked
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS 5.5
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
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.
 
Old 06-17-2011, 03:36 AM   #4
ssrameez
Member
 
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
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
 
Old 06-17-2011, 03:45 AM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,327

Rep: Reputation: 254Reputation: 254Reputation: 254
Quote:
Originally Posted by kapz_unlocked View Post
Code:
loc=(/root/shared_storage/*)
rand1="${loc[RANDOM % ${#loc[@]}]}"
Maybe you can use find:
Code:
loc=($(find . -maxdepth 1 -type d))
 
Old 06-17-2011, 03:54 AM   #6
kapz_unlocked
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS 5.5
Posts: 11

Original Poster
Rep: Reputation: Disabled
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!
 
Old 06-17-2011, 04:34 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by kapz_unlocked View Post
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[@]}]}"
 
Old 06-21-2011, 01:02 AM   #8
kapz_unlocked
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS 5.5
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thank you colucix. Didn't know the facts you mentioned
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Shell Scripting -- foreach file in folder??? AC97Conquerer Programming 11 07-06-2011 01:25 AM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-27-2008 12:50 AM
BASH Scripting - printing select lines in a file bullfrog1870 Linux - Newbie 16 11-08-2006 10:33 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
randomly select file mindcry Linux - Software 2 03-06-2004 09:05 AM


All times are GMT -5. The time now is 04:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration