Reputation: 107
I have a directory in which new files appear at a certain interval (10-15 seconds), all the files are in the same format (.ffid) and also numbered in a regular manner (1.ffid 2.ffid 3.ffid)
, I wand to execute some commands to extract information from n and (n-1) file and perform calculation on that information and display it on screen while the script runs.
This is the code that I have right now :
#!/bin/bash
#bash-hexdump
# Quick script to check delay of the shotpoints
echo "please enter the complete line name as mentioned in the RAID2"
read line
cd /argus/raid2/$line
dir=/argus/raid2/$line/
FILES="$dir"
while [ true ]
do
FFID=$(ls -lrt "$FILES" | grep -i ffid | tail -1)
echo "FFID Value is : "$FFID""
while [ $FFID = $(ls -lrt "$FILES" | grep -i ffid | tail -1) ]
do
sleep 0.5
done
ls -lrt "$dir" | awk '{print " "$9}' | awk 'NR>1' | head -n -2 > /d/home/adira0151/Desktop/tmp/list_a
ls -lrt "$dir" | awk '{print " "$9}' | awk 'NR>2' | head -n -2 > /d/home/adira0151/Desktop/tmp/list_b
paste /d/home/adira0151/Desktop/tmp/list_a /d/home/adira0151/Desktop/tmp/list_b > /d/home/adira0151/Desktop/tmp/list_c
rm /d/home/adira0151/Desktop/tmp/list_a /d/home/adira0151/Desktop/tmp/list_b
let ofst1=1840
let ofst2=1974
let ofst3=1798
while read cffid nffid
do
wd=$(hexdump -s $ofst1 -n 6 -e "64 \"%_p\" \"\\n\"" "$FILES""$cffid")
sp=$(hexdump -s $ofst3 -n 4 -e "64 \"%_p\" \"\\n\"" "$FILES""$cffid")
ct=$(hexdump -s $ofst2 -n 8 -e "64 \"%_p\" \"\\n\"" "$FILES""$cffid" | awk -F: '{print ($1 *3600) + ($2 * 60) + $3}')
nt=$(hexdump -s $ofst2 -n 8 -e "64 \"%_p\" \"\\n\"" "$FILES""$nffid" | awk -F: '{print ($1 *3600) + ($2 * 60) + $3}')
wbt=$(echo "$wd" | awk '{print (($1) *1.33)/1000}' | cut -c 1-3)
spint=$(echo "$nt" "$ct" | awk '{print $1 - $2}')
tot=$(echo "$wbt" "$spint" | awk '{print $1 + $2}' |cut -c 1-2)
echo " " " SP_NO " " " "W_d" " " " Current_SP_Time " " " " Next_SP_Time " " W_B_T " " SP_Int " " " " Total_time "
echo " "
echo " " " " " $sp " " $wd " "" " " " " " $ct " " " " $nt " " " " " " $wbt " " $spint " " " " " " $tot " ""
echo " "
echo " "
if [ $tot -lt 12 ]
then
paste /d/home/adira0151/Desktop/tmp/slow_down.txt
echo please slow down
fi
done < /d/home/adira0151/Desktop/tmp/list_c
done
But it outputs repeated values from list_c , is there any way to just display the output line by line when new files appear in the directory.
Upvotes: 0
Views: 114
Reputation: 19375
But it outputs repeated values …
You get repeated output for the same file because you have the setting
FFID=$(ls -lrt "$FILES" | grep -i ffid | tail -1)
of the old FFID value (to be compared with present ones to check if a new one has appeared) inside your outer while [ true ]
loop, thus getting the same "old" value repeatedly. Rather move that line before the outer loop and set the variable FFID
in the loop to the new FFID, using that for the comparison in the next loop cycle.
Upvotes: 0
Reputation: 19982
Decide how you want to see which files have been processed:
You can write the filenames of the processed files to a logfile or move them to another dir (mkdir -p processed; mv ${file} processed
).
Process the new files and sleep a short time:
function process_file {
your_logic_with $1
}
while [ x ]; do
for file in *.ffid; do
# grep "^${file}$" logfile | continue
process_file "${file}"
# mv "${file}" processed
# Or
# echo "${file}" >> processed
done
sleep 1
done
Upvotes: 1
Reputation: 15461
I suggest you use inotify to monitor newly created files.
In your case, to output (or do whatever you want) with newly created .ffid
files :
DIR="/tmp" # set DIR with your dir to monitor
inotifywait -m -e create $DIR | while read file
do
if [[ ${file##*.} == "ffid" ]]; then
if [[ -n "$prev_file" ]]; then
printf "Command on previous file: %s\n" "$prev_file";
fi
if [[ -n "$file" ]]; then
printf "Command on last file: %s\n" "$file";
fi
prev_file=$file # stores the penultimate created file
fi
done
Upvotes: 0