2015-11-09 118 views
1

我一直收到這些錯誤,運行我的腳本,我只是不能解決它...運行sh腳本的錯誤

不斷出現的錯誤是;

rm:can not remove〜/ my-documents/article:':是一個目錄。它指的目錄是$ 2 ...這是我的腳本。

#! /bin/sh 

SRC=$1 
DES=$2 

if [ $# -ne 2 ]; then 
echo "1. Please enter the source directory" 
echo "2. Please enter the destination directory" 
echo "thankyou" 
exit 
fi 


if [ ! -d $1 ]; then 
echo "$1 is not a directory please enter a valid directory" 
echo "thankyou" 
exit 
fi 
#gives the user a error warning the source directory is invalid 


if [ -d $2 ]; then 
echo "output directory exists" 
else 
echo "Output directory does not exist, creating directory" 
mkdir $2 
fi 
#creates the destination directory if one doesn't exist 


IFILE=$GETFILES; 
FINDFILE=$FINDFILE; 


find $1 -name "*.doc" > FINDFILE 
find $1 -name "*.pdf" > FINDFILE 
find $1 -name "*.PDF" > FINDFILE 
#finds doc, pdf & PDF files and sends data to findfile. 

while read -r line; 
do 
cp $line $2 
done < FINDFILE 
#files read and copied to destination directory 

IFILE=$2/$GETFILES; 
ls -R $1 | egrep -i ".doc | .pdf" > IFILE; 

LCOUNT=0 
DIFFCOUNT=0 
FOUND=0 
ARCHIVE=1 
BASE="${line%.*}" 
EXTENSION="${line##*.}" 

COUNT=$COUNT; 

ls $2 | grep ${line%%.*} \; | wc -l 

if [[ $COUNT -eq 0 ]]; 
then 
cp $1/$line $2; 
else 
echo "there is already a file in the output so need to compare" 
COMP=$2/$line 
fi 

while [[ $FOUND -eq 0 ]] && [[ $LCOUNT -lt $COUNT ]]; 
do 
echo "diffcount is $DIFFCOUNT" 

###compares the file from the input directory to the file in 
###the output directory 

if [ $DIFFCOUNT -eq 0 ]; 
then 
echo "file has already been archived no action required" 
FOUND=$FOUND [ $FOUND+1 ] 
else 
LCOUNT=$LCOUNT [ $LCOUNT+1 ] 
COMP="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION" 
echo "line count for next compare is $LCOUNT" 
echo "get the next file to compare" 
echo "the comparison file is now $COMP" 
fi 
    if [ $LCOUNT -ne $COUNT ]; then 
    ARCHIVE=$ [ $ARCHIVE+1 ] 
    else 
    ARCHIVE=0 
    fi 

    if [ $ARCHIVE -eq 0 ]; 
    then 
    NEWOUT="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION"; 
    echo "newfile name is $NEWOUT" 
    cp $1/$LINE $NEWOUT 
    fi 

done < $IFILE 
rm $IFILE 

OFILE=$2/DOCFILES; 
ls $2 | grep ".doc" > $OFILE; 

while read -r line; 
do 

BASE=${line%.*} 
EXTENSION=${line##*.} 
NEWEXTENSION=".pdf" 
SEARCHFILE=$BASE$NEWEXTENSION 

find $2 -name "$SEARCHFILE" -exec {} \; 
done < $OFILE 
rm $OFILE 

### this will then remove any duplicate files so only 
### individual .doc .pdf files will exist 
+0

什麼是'$ GETFILES'?它從哪裏獲得價值? (將'set -x'添加到腳本的頂部以查看實際正在運行的內容,並且您應該看到您的問題。) –

回答

2

rm一個普通的電話只能刪除文件,不目錄

$ mkdir /tmp/mydir 
$ rm -r /tmp/mydir 
$ 

這在充分描述:

$ touch /tmp/myfile 
$ rm /tmp/myfile 

$ mkdir /tmp/mydir 
$ rm /tmp/mydir 
rm: cannot remove ‘/tmp/mydir/’: Is a directory 

可以通過指定的-d(刪除目錄)或-r(刪除目錄和內容遞歸地)標誌除去目錄man rm

除此之外,你似乎忽略了報價:

$ rm $OFILE 

可能爆發嚴重如果OFILE值包含空格,使用報價代替:

$ rm "${OFILE}" 

never parse the output of ls

ls $2 | grep ".doc" > $OFILE 

(例如如果你的「$ 2」實際上是「/home/foo/my.doc.files/」,它將把這個目錄下的所有文件放到$ OFILE中)。

然後你迭代這個文件的內容?

代替,只要使用循環使用文件通配符:

for o in "${2}"/*.doc 
do 
    ## loop code in here 
done 

或者只是做find過濾(不要忘了叫可執行-exex):

find "$2" -name "$SEARCHFILE" -mindepth 1 -maxdepth 1 -type f -exec convertfile \{\} \;