[英]Loop through the files one by one in a directory as they appear and display result of command executed on them on the screen
我有一個目錄,在該目錄中,新文件以一定的間隔(10-15秒)出現,所有文件的格式相同(.ffid),並且也以常規方式編號(1.ffid 2.ffid 3.ffid)
,我想執行一些命令從n和(n-1)文件中提取信息,並對這些信息進行計算,並在腳本運行時將其顯示在屏幕上。
這是我現在擁有的代碼:
#!/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
但是它從list_c輸出重復的值,當目錄中出現新文件時,有什么方法可以僅逐行顯示輸出。
決定要如何查看已處理的文件:您可以將已處理文件的文件名寫入日志文件或將它們移動到另一個目錄( mkdir -p processed; mv ${file} processed
)。 處理新文件並短暫睡眠:
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
我建議您使用inotify監視新創建的文件。
在您的情況下,要輸出(或做任何您想做的)新創建的.ffid
文件:
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
但是它輸出重復的值……
您獲得了相同文件的重復輸出,因為有了該設置
FFID=$(ls -lrt "$FILES" | grep -i ffid | tail -1)
while [ true ]
循環內部的舊FFID值(與當前值進行比較以檢查是否出現了新值),從而反復獲得相同的“舊”值。 而是將該行移到外部循環之前,然后將循環中的變量FFID
設置為新的FFID,並將其用於下一個循環中的比較。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.