BASH仅读取文件N行,睡眠N次,然后从中断处继续 [英] BASH read file for only N rows, sleep for N time and continue where left off

查看:56
本文介绍了BASH仅读取文件N行,睡眠N次,然后从中断处继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有这个脚本-

#!/bin/bash
INPUT=/cygdrive/c/apps/dcm4che-2.0.25-bin/SUID_TEST
BASEDIR=/cygdrive/c/apps/dcm4che-2.0.25-bin/bin
TWIDDLE=/cygdrive/c/apps/dcm4chee-2.17.2-mssql/bin/twiddle.sh
#Administrator login and password
LOGIN=mylogin
PASSWORD=mypass
count=0
exec 3<&0 # Save stdin to file descriptor 3.
exec 0<$INPUT # Redirect standard input.
while read line
do
input1=$(echo $line | awk '{ print $1}')
echo "Deleting :" ${input1}
$TWIDDLE -u ${LOGIN} -p ${PASSWORD} -s jnp://192.168.50.51:1099 invoke "dcm4chee.archive:service=ContentEditService" purgeStudy ${input1}
count=$( expr $count + 1 )
sleep 30
done
exec 0<&3 # Restore old stdin.
echo "Counter:" $count # Show deleted items

输入文件"SUID_TEST"将包含100,000行,每行看起来像"1.2.124.113532.192.168.50.52.20070330.204624.1634650".

the input file 'SUID_TEST' will have over 100,000 lines in it, with each line looking like '1.2.124.113532.192.168.50.52.20070330.204624.1634650'.

我想做的是让脚本只读取50行,睡眠10分钟,然后继续读取另外50行,睡眠10分钟,直到到达输入文件的末尾.

What I am trying to do is have the script read only 50 lines, sleep for 10min and continue reading another 50 lines, sleep for 10min, until it reaches the end of the input file.

现在,它一直运行到到达文件末尾为止.

Right now, it just runs until it gets to the end of the file.

有人可以帮我解决这个问题吗?

Could someone help me out with how I could get this going?

谢谢

-仅在此处放置一些代码-我正在使用的新脚本:

-- just placing some code here - new script I'm using:

#!/bin/bash
INPUT=/cygdrive/c/apps/dcm4che-2.0.25-bin/SUID_TEST
BASEDIR=/cygdrive/c/apps/dcm4che-2.0.25-bin/BIN
TWIDDLE=/cygdrive/c/apps/dcm4chee-2.17.2-mssql/bin/twiddle.sh
LOGIN=mylogin
PASSWORD=mypass
cnt=0
count=0
exec 3<&0 #Save stdin to file descriptor 3.
exec 0<$INPUT #redirect standard input
while read input1 rest  # Let read split the line instead of running awk
do
  echo "Row $count"
  ((cnt++))
  echo "Deleting :" ${input1}
  $TWIDDLE -u ${LOGIN} -p ${PASSWORD} -s jnp://192.168.50.51:1099 invoke "dcm4chee.archive:service=ContentEditService" purgeStudy ${input1}
  ((count++))
  if (( cnt == 50 )); then
      echo "Sleeping for 10 minutes on" | date
      sleep 600
      cnt=0
  fi
done
exec 0<$3
echo "Counter:" $cnt #show deleted items

推荐答案

只需计算循环迭代次数,在计数器达到50时休眠并重置计数器即可.

Just count your loop iterations, sleeping and resetting the counter when it hits 50.

cnt=0
count=0
while read input1 rest  # Let read split the line instead of running awk
do
  echo "Row $count"
  ((cnt++))
  echo "Deleting :" ${input1}
  $TWIDDLE -u ${LOGIN} -p ${PASSWORD} -s jnp://192.168.50.51:1099 invoke "dcm4chee.archive:service=ContentEditService" purgeStudy ${input1}
  ((count++))
  if (( cnt == 50 )); then
      date
      sleep 600
      cnt=0
  fi
done

要消除对exec语句的需要,只需从不同的文件描述符(习惯上为3)读取read命令,然后将输入文件重定向到while循环的文件描述符3.

To remove the need for the exec statements, you can simply have the read command read from a different file descriptor (3 is customary), then redirect your input file to the while loop's file descriptor 3.

while read -u3 input1 rest; do
    ...
done 3< "$INPUT"

这篇关于BASH仅读取文件N行,睡眠N次,然后从中断处继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆