在出现文件时逐个遍历目录中的文件,并在屏幕上显示对其执行命令的结果



我有一个目录,其中新文件以一定的间隔(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: %sn" "$prev_file";
    fi
    if [[ -n "$file" ]]; then
      printf "Command on last file: %sn" "$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,将其用于下一个循环周期中的比较。

相关内容

  • 没有找到相关文章