Linux 运行sh脚本时出错
我一直在运行我的脚本时遇到这些错误,我就是无法解决 不断出现的错误是 rm:无法删除~/my documents/article:':是一个目录。它所指的目录是$2…这是我的脚本Linux 运行sh脚本时出错,linux,bash,Linux,Bash,我一直在运行我的脚本时遇到这些错误,我就是无法解决 不断出现的错误是 rm:无法删除~/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" ex
#! /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
#/垃圾箱/垃圾箱
SRC=$1
DES=2美元
如果[$#-ne 2];然后
echo“1.请输入源目录”
echo“2.请输入目标目录”
回声“谢谢”
出口
fi
如果[!-d$1];然后
echo“$1不是目录请输入有效目录”
回声“谢谢”
出口
fi
#向用户发出错误警告,警告源目录无效
如果[-d$2];然后
echo“输出目录存在”
其他的
echo“输出目录不存在,正在创建目录”
mkdir$2
fi
#如果目标目录不存在,则创建目标目录
IFILE=$GETFILES;
FINDFILE=$FINDFILE;
查找$1-名称“*.doc”>FINDFILE
查找$1-名称“*.pdf”>FINDFILE
查找$1-名称“*.PDF”>FINDFILE
#查找文档、pdf和pdf文件,并将数据发送到findfile。
而read-r行;
做
cp$行$2
完成IFILE;
LCOUNT=0
扩散计数=0
找到=0
存档=1
BASE=“${line%.*}”
EXTENSION=“${line##*.}”
COUNT=$COUNT;
ls$2 | grep${line%%.*}\|wc-l
如果[$COUNT-等式0]];
然后
cp$1/$2行;
其他的
echo“输出中已存在文件,因此需要进行比较”
薪酬=$2/$line
fi
而[[$FOUND-eq 0]]&&[$LCOUNT-lt$COUNT]];
做
echo“diffcount是$diffcount”
###将输入目录中的文件与中的文件进行比较
###输出目录
如果[$DIFFCOUNT-等式0];
然后
echo“文件已存档,无需执行任何操作”
FOUND=$FOUND[$FOUND+1]
其他的
LCOUNT=$LCOUNT[$LCOUNT+1]
COMP=“OUT”/“$BASE”\u“$LCOUNT”。$EXTENSION”
echo“下一次比较的行计数为$LCOUNT”
echo“获取下一个要比较的文件”
echo“比较文件现在是$COMP”
fi
如果[$L计数-ne$COUNT];然后
存档=$[$ARCHIVE+1]
其他的
存档=0
fi
如果[$ARCHIVE-等式0];
然后
NEWOUT=“OUT”/“$BASE”\u“$LCOUNT”。$EXTENSION”;
echo“新文件名为$NEWOUT”
cp$1/$LINE$NEWOUT
fi
完成<$IFILE
rm$IFILE
OFILE=$2/DOCFILES;
ls$2 | grep“>doc$OFILE;
而read-r行;
做
基本=${line%.*}
扩展=${line##*.}
NEWEXTENSION=“.pdf”
SEARCHFILE=$BASE$NEWEXTENSION
查找$2-名称“$SEARCHFILE”-exec{}\;
完成
里亚尔
###这将删除所有重复的文件,因此
###将存在单个.doc.pdf文件
对rm
的普通调用只能删除文件,而不能删除目录
您可以通过指定-d
(删除空目录)或-r
(递归删除目录和内容)标志来删除目录:
这在man rm
中有很好的描述
除此之外,你似乎忽略了引用:
$ rm $OFILE
如果文件的值包含空格,则可能会严重中断,请改用引号:
以及:
(例如,如果您的“$2”实际上是“/home/foo/my.doc.files/”,它会将此目录中的所有文件放入$OFILE)
然后迭代这个文件的内容
相反,只需将循环与文件全局绑定一起使用:
for o in "${2}"/*.doc
do
## loop code in here
done
或者只需使用find
进行过滤(不要忘记使用-exex
调用可执行文件):
什么是$GETFILES
?它从哪里获得值?(将set-x
添加到脚本顶部,以查看实际运行的内容,您应该可以看到您的问题。)
$ rm $OFILE
$ rm "${OFILE}"
ls $2 | grep ".doc" > $OFILE
for o in "${2}"/*.doc
do
## loop code in here
done
find "$2" -name "$SEARCHFILE" -mindepth 1 -maxdepth 1 -type f -exec convertfile \{\} \;