0

howdi folks ,

i wrote a script that i am using to copy file to multiple files and generate random names to the copies. this part is working ,however when i am trying to run more sets of commands on the copies created it does not take all the copies but only one copy and run the commands on this one.

this is the script :

#!/bin/bash
## Script for copying a file to multiple files (mass copy)  and generating random name to each file
##
## Main script
# VARS
COUNTER=0

echo "File to copy ? (full name)"
read filename
echo "Please select extension to new files (doc/pdf/exe/etc...)"
read ext
echo "How many copies to make ? (number)"
read numcopies
     while [  $COUNTER -lt $numcopies ]; do
         for i in $( cat /dev/urandom | tr -cd 'a-f0-9' | head -c 64 ); do
             cp $filename $i.$ext
             ls $i.$ext > /tmp/newfiles.txt
         done
     let COUNTER=COUNTER+1
     done
#
## Diff test for files generated on this script only
# VARS
DIFF=$(diff $filename $i.$ext)

if [ "$DIFF" = "" ]
then
    echo "Diff all files succesfully"
fi

#
## md5sum test for files generated on this script only
# VARS
TMP=/tmp/md5sum_test.txt
MD5=$(md5sum $filename $i.$ext > $TMP)

grep $filename $TMP | awk '{print $1}' | while read line;
do
    GREP=$(grep -v $line $TMP)

    if [ "$GREP" == "" ]
    then
      echo "md5sum's are matching"
    else
      echo "operation failed"
    fi
done
exit 0

this is the out put i am getting :

root@myserver:/my/directory # ./copier.sh 
File to copy ? (full name)
oren.doc
Please select extension to new files (doc/pdf/exe/etc...)
doc
How many copies to make ? (number)
20
Diff all files succesfully
md5sum's are matching
root@myserver:/my/directory # cat /tmp/newfiles.txt 
bbfb46e1a0fabeab9343839dd2b8e13a254847f0890f809959decc669abd06fd.doc
root@myserver:/my/directory # cat /tmp/md5sum_test.txt 
cc8bba3d80207cd0b9832eddf7781743  oren.doc
cc8bba3d80207cd0b9832eddf7781743  bbfb46e1a0fabeab9343839dd2b8e13a254847f0890f809959decc669abd06fd.doc

thanks in advance for any feedbacks...

3
  • And you couldn't have cut down the amount of code necessary to reproduce the questionable behaviour? (This more often than not leads to yourself discovering the problem long before you have to ask others.) Commented Oct 1, 2014 at 7:24
  • Use More Quotes™ Commented Oct 1, 2014 at 11:59
  • hi ,i spent a ccpole of good hors on this and on net and when i run the commands out of script it works but i missing the point of redirecting output so i can re-use it on script. Commented Oct 1, 2014 at 17:52

1 Answer 1

1

This:

ls $i.$ext > /tmp/newfiles.txt

Should append, not overwrite:

ls $i.$ext >> /tmp/newfiles.txt
Sign up to request clarification or add additional context in comments.

1 Comment

hi ,thanks for the feed. that solves file listing on script but diff and md5sum are still using just last file from output...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.