删除文本文件的脚本

删除文本文件的脚本

我试图制作一个脚本来搜索您指出的目录中的所有 ASCII 文本文件,然后将每个文件的路径写入文本文件。之后它应该一一显示每个文件的头部并询问您是否要删除该文件。如果您说“是”,它会删除该文件,如果您说其他内容,它只会向您显示下一个文件,依此类推,直到您检查每个文件。所以我最终得到的是:

#!/bin/bash
echo "Give directory to search for ASCII text files"
read direct
find $direct -type f -exec file {} + | grep ASCII > ztextz
sed "s/ .*//" ztextz > ztextz1
sed "s/://g" ztextz1 > ztextz2
#until here everything works fine
#it creates the file that contains paths of all ASCII text files in the directory given by you
#After that it is a mess..it needs to check the head of every file one by one
#and let me decide if I want to delete it or not
files=ztextz2
while IFS= read -r name;
do
head "$name";

echo "Do you want to delete this file?"
read want
case $want in
YES)
rm $name;;
Yes)
rm $name;;
yes)

rm $name;;
Y)
rm $name;;
y)
rm $name;;
esac
done < "$files"

因此,作为输出,我想看到目录中第一个文本文件的 10 行,并询问是否要删除该文件。在我回答“是”后,它应该删除该文件并显示下一个文件中的 10 行等。

我现在得到的输出是 2 个文件的前 10 行(我的目录中有 4 个文件)和问题“你想删除这个文件吗?”然后脚本就中断了。

似乎它没有按预期工作。有人可以解释一下我的错误在哪里吗?


好的,因为我没有足够的声誉来自己回答,所以我把它放在这里。


是的,所以我坚持使用我的旧脚本,在将两个 $$ 像 $$numb 放在一起时遇到问题,并将它们替换为 ${!numb} 它开始工作......所以现在我可以从文本中清理我的目录该脚本不需要的文件=)

   #!/bin/bash
echo "Give directory to search for ASCII text files"
read direct
find $direct -type f -exec file {} + | grep ASCII > ztextz
sed "s/ .*//" ztextz > ztextz1
sed "s/://g" ztextz1 > ztextz2
set `less ztextz2`
numb=$#
rm ztextz*
while [ $numb -gt 0 ]
do head ${!numb}
echo "Do you want to delete ${!numb} file?"
read want
case $want in
yes)
rm ${!numb}
echo "File is removed"
sleep 1;;
Yes)
rm ${!numb}
echo "File is removed"
sleep 1;;
Y)
rm ${!numb}
echo "File is removed"
sleep 1;;
y)
rm ${!numb}
echo "File is removed"
sleep 1;;
YES)
rm ${!numb}
echo "File is removed"
sleep 1;;
esac
numb=$[$numb-1]
done

附言。使其更快地删除sleep 1=)

答案1

问题是你正在用实例read- 一个用于读取文件名,另一个用于获取答案。因此,read等待答案会占用第二个文件名,并且您应该希望文件列表看起来不像:

some_throuwaway_stuff
foo
very_important_file.txt
Yes
some_throuwaway_stuff
foo

您的脚本可能应该类似于:

#!/bin/bash
echo "Give directory to search for ASCII text files"
read direct
exec 3<&0
find "$direct" -type f |
while IFS= read -r name; do
    echo "========================================"
    if file "$name" | grep ASCII; then
        echo "----------------------------------------"
        head "$name";
        echo "----------------------------------------"
        echo "Do you want to delete this file?"
        read -u 3 want
        case $want in
            YES|Yes|yes|Y|y)
                rm "$name"
                ;;
            *)
                ;;
        esac
    fi
done

请注意,重定向exec 3<&0将定期提供stdin给文件描述符 3,然后在循环中检查该文件描述符以获取用户的答复。

也就是说,rm -i这可能是一个更好的选择。

答案2

您可以更换您的块

while [ $numb -gt 0 ]
    do head ${!numb}
    echo "Do you want to delete ${!numb} file?"
    read want
    case $want in
        yes)
            rm ${!numb}
            echo "File is removed"
            sleep 1;;
        Yes)
            rm ${!numb}
            echo "File is removed"
            sleep 1;;
        Y)
            rm ${!numb}
            echo "File is removed"
            sleep 1;;
        y)
            rm ${!numb}
            echo "File is removed"
            sleep 1;;
        YES)
            rm ${!numb}
            echo "File is removed"
            sleep 1;;
    esac
    numb=$[$numb-1]
done

经过

while [ $numb -gt 0 ]
do
    head ${!numb}
    rm -i ${!numb}
    numb=$[$numb-1]
done

相关内容