0

i am trying to write a script to delete those files who's last modification falls into a time interval... my script is..

echo "enter time interval(HH:MM - HH:MM):"    
read t1    
t1=$( echo "$t1" | awk -F: '{ print ($1 * 3600) + ($2 * 60)}')    
read t2    
t2=$( echo "$t2" | awk -F: '{ print ($1 * 3600) + ($2 * 60)}')

ls -l |awk '{ print $8" "$9 }' > r    
while read line    
do   
 t=$(echo $line | awk '{print $1}' | awk -F: '{ print ($1 * 3600) + ($2 * 60)}')    
 f=$(echo $line | awk '{print $2}')         
 if [ $t -ge $t1 ]    
 then    
    if [ $t -le $t2 ]    
    then
        count=0    
        while read line1    
        do    
         if [ $count -le 10 ]    
         then    
         echo "$line1"    
         count=`expr $count + 1`    
         fi    
        done < $f    
        echo "do you want to delete this file "    
        read yn             
        case $yn in    
        'Yes' ) rm "$f";;    
        'No' ) exit;;    
    esac                
    fi    
fi    
done <r

but read command (after "echo "do you want to delete this file "" ) is not working..... please help..

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • `IFS=: read h m; t1=$(( h*3600 + m*60 ))` is more efficient than calling out to `awk` to parse and compute the time in seconds. – chepner Nov 15 '14 at 16:21

1 Answers1

1

The whole top-level while loop has the input redirected because of done <r. So, when you try read yn, it reads form r again.

Possible solution: before entering the loop, redirect the standard input to some other file descriptor, and use read -u to read from it:

#! /bin/bash
while read -u3 x ; do           # Read from the file descriptor 3.
    echo $'\t'"$x"
    if [[ $x == *echo* ]] ; then
        echo Do you see echo\?
        read y                  # True user input.
        echo Answer: "$y"
    fi
done 3< ~/file

read x                          # No redirection in effect, user input again.
# read -u3 y                    # This would cause the "Bad file descriptor" error.
choroba
  • 231,213
  • 25
  • 204
  • 289