将脚本输出用于其他命令

时间:2014-10-01 07:19:24

标签: bash shell output-redirect

你好吗,

我编写了一个脚本,用于将文件复制到多个文件并为副本生成随机名称。 这部分是有效的,但是当我试图在创建的副本上运行更多命令集时,它不会占用所有副本,而只需要一个副本并在此副本上运行命令。

这是剧本:

#!/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

这是我得到的输出:

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

提前感谢任何反馈...

1 个答案:

答案 0 :(得分:1)

此:

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

应该追加,而不是覆盖:

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