The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
$random relle Shell Programming and Scripting 6 07-11-2008 06:59 AM
Random command melaz Shell Programming and Scripting 1 07-10-2008 09:24 PM
Random keyvan Shell Programming and Scripting 8 05-24-2007 01:11 PM
Random files do not FTP in the shell script RLatham20 Shell Programming and Scripting 1 06-29-2006 01:07 PM
random in ksh pascalbout AIX 1 01-04-2006 10:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-21-2007
whegra's Avatar
whegra whegra is offline
Registered User
  
 

Join Date: Sep 2002
Posts: 19
FTP random files

Hello everyone,

What I'm trying to do is figure out how to ftp a set of random files each day about 50 or so maybe more or less.

I have a folder on my server that gets about 1000 or so new files each day, each with today's date in the file name. We just want to take a sample each day for quality checks.

eg. testnumberssomethingelse20071121morenumbers

What I need to do is grab 50 random files for today's date and upload them via ftp. I have all the code for ftp and what not, I just don't know how to pick out 50 or so files out of 1000+ and direct them to my ftp process.

Here is my current script without randomization, which I've gather mostly from here.


Code:
#! /bin/ksh
stty -echo
#print -n Enter Password-
#read PASS
print
stty echo
exec 4>&1

HOST=iphere
USER=test123
DESTDIR=/
SOURCEDIR=/something1/something2
PASS=test123

cd $SOURCEDIR

ftp -nv >&4 2>&4 |&
print -p open $HOST
print -p user $USER $PASS
print -p binary
#print -p cd $DESTDIR

ls | while read filename ; do
        [[ -f $filename ]] && print -p put $filename
done
print -p close
print -p bye
wait
exit 0

Any help would be greatly appreciated.

Thank you
  #2 (permalink)  
Old 11-21-2007
bobbygsk bobbygsk is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 90
Use the rand() command

1) List the filenames to a file
2) Use rand(no. of filenames in the file)
3) Use the result of the rand() as the position of the filename list in the file.
4) Get the filename to ftp
  #3 (permalink)  
Old 11-21-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Here is a cool trick that I'm borrowing from cfajohnson... First put a random number in front of each file name:

ls | awk 'BEGIN {srand()} {printf "%.0f %s \n",rand()*99999, $0; }'

then sort it. This will put the lines in a random order. Then just pick the first few lines:

ls | awk 'BEGIN {srand()} {printf "%.0f %s \n",rand()*99999, $0; }' |sort -n | head -2 | awk '{print $2}'
  #4 (permalink)  
Old 11-21-2007
whegra's Avatar
whegra whegra is offline
Registered User
  
 

Join Date: Sep 2002
Posts: 19
Perderabo/cfajohnson,

This works great!

Thank you.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0