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..

1
  • 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. Commented Nov 15, 2014 at 16:21

1 Answer 1

2

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. 
Sign up to request clarification or add additional context in comments.

2 Comments

It'd be simpler to run the loop read over FD #3: while read -u3 x; do ... done 3<~/file, and leave stdin alone.
@GordonDavisson: True, fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.