Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was trying to make a script that searches for all ASCII text files in the directory that you point out and after that writes the path of every file to a text file. After that it's supposed to show the head of each file one by one and ask if you want to delete the file. If you say yes it removes that file, if you say something else it just shows you the next file and etc. until you check every file. So I've ended up with:

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

So as the output I suppose to see 10 lines from first text file in my dir and question if I want to remove that file. After I respond with "Yes" it is supposed to remove that file and show me the 10 lines from next file and etc.

The output that I get now is the first 10 lines from 2 files (I have 4 files in my dir) and the question "Do you want to delete this file?" and then the script breaks.

Seems like its not working as expected. Could someone please explain me where my mistake is?


OK since I don't have enough reputation to answer myself I put it here.


Yeah so I've sticked with my old script where I got problem with putting two $$ together like $$numb and replaced them with ${!numb} it started to work...so now I can clean my directories from the text files that I don't need with that script=)

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

PS. to make it faster remove sleep 1 =)

share|improve this question
    
Please explain how it is not working. I can see some problems immediately but it is much easier to answer if you clearly explain what the expected behavior is and how the actual behavior is different. –  terdon Oct 13 '13 at 19:50
    
Added some information. Hope it is a bit more clear now –  Denis Oct 13 '13 at 20:21
2  
Why don't you use rm -i instead of asking all those question? –  unxnut Oct 13 '13 at 21:16
    
@unxnut why not adding it as an answer - I'd happily upvote it. :) –  peterph Jun 18 at 10:11
    
Thanks @peterph. I have added it below. –  unxnut Jun 18 at 15:35

2 Answers 2

up vote 4 down vote accepted

The problem is that you are reading the file with two instances of read - one for reading the filename the other one for getting the answer. Hence the read waiting for the answer eats every second filename, and you should hope the filelist doesn't look like:

some_throuwaway_stuff
foo
very_important_file.txt
Yes
some_throuwaway_stuff
foo

Your script should probably look something like:

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

Notice the redirection exec 3<&0 which will feed regular stdin to file descriptor 3, which is then checked in the cycle for answer from the user.

That said, rm -i is likely a better option.

share|improve this answer
    
better is the change rm to rm -f –  Babyy Jun 17 at 14:36
    
Depends on circumstances. rm has many useful options that can be either beneficial or wreak havoc on your data depending on what you are doing. –  peterph Jun 18 at 10:08

You can replace your block

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

by

while [ $numb -gt 0 ]
do
    head ${!numb}
    rm -i ${!numb}
    numb=$[$numb-1]
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.