在出现的目录中逐个循环浏览文件,并在屏幕上显示对它们执行的命令的结果

时间:2016-05-22 07:04:06

标签: linux bash shell unix

我有一个目录,其中新文件以特定间隔(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输出重复值,有没有办法在目录中出现新文件时逐行显示输出。

3 个答案:

答案 0 :(得分:1)

决定如何查看已处理的文件: 您可以将已处理文件的文件名写入日志文件,或将它们移动到另一个目录(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

答案 1 :(得分:0)

我建议您使用 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

答案 2 :(得分:0)

  

但它会输出重复值......

您将获得同一文件的重复输出,因为您具有设置

FFID=$(ls -lrt "$FILES" | grep -i ffid | tail -1)
在您的外部while [ true ]循环中,将旧的FFID值(与现有的FFID值进行比较以检查是否出现了新的FFID值)的

,从而重复获得相同的“旧”值。而是在外部循环之前移动该行,并将循环中的变量FFID设置为新的FFID,并将其用于下一个循环周期中的比较。