OK, I am stumpped. I have this shell Script that I want to randomly select a file with the extention of .sct. Then using a portion of its file name select the six related .mot files. Then move them all to another folder. I also need a user input form for the number of .SCT files to randomly select and move.
So I have a file structure like this:
Need to randomly select the file .sct and move it and its related .mot files to another directory. Hopefully I have explained this good enough.
Thanks for the help. I could do this in VB but this UNIX thing has me stumpped. Right now we do it manualy through thousands of files.
So here is the script I want to use. It isn't working I can't get past the
if [ ! -d "$dir" ]; then echo "$0: $dir: no such directory"; exit; fi;
#! /usr/bin/env bash
dir="$1"
count="$2"
[ "$dir" ] && [ $count -gt 0 ] && {
if [ ! -d "$dir" ]; then echo "$0: $dir: no such directory"; exit; fi;
RANDOM=$$$(date +%s) #init random seed
for (( c=0; c < $count; c++ )); do
files=(*.sct) #creates array of sct files
ct=${#files[@]} #computes array length
if [ $ct -eq 0 ]; then break; fi
sct=${files[$[($RANDOM % $ct)]]} #pick random file
prefix=$(echo $sct | sed 's:\(.*-\).*:\1:')
mot_files=($(ls $prefix*.mot 2> /dev/null))
mv $sct $dir
if [ ${#mot_files[@]} -gt 0 ]; then
mv ${mot_files[@]} $dir #
fi
done
} || echo "usage: $0 <dir> <num of files>"
At first glance this scripts looks like working one. Try to simplify it (check for args at first and do the work at second) and debug it with bashdb. I'm sure if you can write this one by yourself you can easily debug it and find a mistake.
I appoligize. I reread my post and I want to make it clear that I didn't write this code. I got it from another site. I have been trying to make it work all day and I can't. I am very new to Shell Scripting and I am trying to use this to automate a very time consuming process. Sorry for the confusion. It has been a long day out here.
Thanks for any help you can provide. It appears that the code will not create the directory.
So the UNIX machine isn�t connected to an internet connection so I had to write it down and type it in to the windows machine. Thanks for the help.
Here is the output:
++ dir=$�\r�
++ count=$�\r�
++ �[� $ �\r� �]�
++ �[� �gto �]�
./random_select.sh: line 4: [:-gt:unary operator expected
++ �[� �!� �d$ �\r� �]�
: no such directory �lect.sh
:no such directory
++ exit
Are you using PuTTY to connect to that machine? If so, anything you highlight using the mouse is automatically copied to the clipboard.
Am I right in assuming that you called this script without parameters? And that you wrote it using Notepad? Because I'm seeing an awful lot of carriage returns (\r) here. On the command prompt, run
tr -d '\r' random_select.sh
to clear them out. How are you transferring the file? If by FTP, change the transfer type to ASCII to have the system do that automatically.
Thanks for the reply. I tried to send you a PM explaining the situation. Unfortunately I could not.
So:
Are you using PuTTY to connect to that machine?
No, i can not connect to that machine from my unclassified computer. I am doing the pen and paper drill.
Am I right in assuming that you called this script without parameters?
yes, just ./
And that you wrote it using Notepad?
Yes, i wrote it in notepad on the unclass machine and brought it over to the other machine.
How are you transferring the file?
They are for this conversation just in the file location on the UNIX machine.
Thank you for your continued support. When i get back in I will try the code you mentioned. Thanks