1

bashスクリプトの行番号をマークして、保存した行番号でそのスクリプトを再起動できるかどうかを知る必要があります。

コード:

#!/bin/bash
while read -r line; do #I'm reading from a big wordlist
command1 using $line
command2 using $line
done

具体的には、スクリプトが指定された行番号から開始するように、スクリプトの現在の$行番号を別のテキストファイルに自動的に書き込む方法があります。これにより、万が一の場合にすべてを最初から開始する必要がなくなります。スクリプトを停止する必要がありますか?

それは意味がありますか?

どうもありがとうございます !

4

3 に答える 3

2

これは役立つかもしれません:

#!/bin/bash

TMP_FILE="/tmp/currentLineNumber"                         # a constant

current_line_count=0                                      # track the current line number

processed_lines_count=0

# Verify if we have already processed some stuff.
if [ -r "${TMP_FILE}" ]; then
  processed_lines_count=$(cat ${TMP_FILE})
fi

while read -r line; do                                    # I 'm reading from a big wordlist

    # Skip processing till we reach the line that needs to be processed.

    if [ $current_line_count -le $processed_line_count ]; then

      # do nothing as this line has already been processed
      current_line_count=$((current_line_count+1))        # increment the counter
      continue

    fi

    current_line_count=$((current_line_count+1))
    echo $current_line_count > ${TMP_FILE}                # cache the line number

    # perform your operations
    command1 using $line
    command2 using $line

done
于 2012-06-08T16:02:16.547 に答える
1

これは機能するはずです:

    #!/bin/bash
    I=`cat lastline`;
    A=0;

    while read -r line; do
           if [$A>=$I]; then
               command1 using $line
               command2 using $line
               (( I++ ))
               echo "$I" > "lastline";
           fi;
           (( A++ ))
    done

再起動する場合は、lastlineを削除する必要があることを忘れないでください。:-)

于 2012-06-08T15:50:06.887 に答える
1

bashのみのソリューションは優れていますが、他のツールを使用して再起動を合理化することで、パフォーマンスが向上する場合があります。あなたの質問のスクリプトのように、以下はstdinのワードリストを取ります。

#!/bin/sh

# Get the current position, or 0 if we haven't run before
if [ -f /tmp/processed ]; then
  read processed < /tmp/processed
else
  processed=0
fi

# Skip up to the current position
awk -v processed="$processed" 'NR > processed' | while read -r line; do

  # Run your commands
  command1 using $line
  command2 using $line

  # Record our new position
  processed=$((processed + 1))
  echo $processed > /tmp/processed

done

ああ、そして私がこれを書いた方法では、それはBourneシェルと互換性があるので、bashを必要としません。

于 2012-06-09T03:10:20.303 に答える