开发者

Bash List Files after a specific file

开发者 https://www.devze.com 2023-02-12 03:55 出处:网络
I want to run my program on all the files in a directory after a specific file using a bash script. If I have a direc开发者_开发百科tory like:

I want to run my program on all the files in a directory after a specific file using a bash script.

If I have a direc开发者_开发百科tory like:

fileA
fileB
fileC
fileD

I want to run ./prog <file> for all files after fileC. How would I write a bash script to do this?

I currently have

for FILE in ./tests/*; do
  ./prog $FILE
  if [ $? -eq 0 ]; then
    echo "success: $FILE"
  else
    echo "**FAILURE: $FILE"
    exit 1
  fi
done

But, I want to start at a specific file in that directory. It doesn't necessarily need to be sorted since ls list files in a specific order which is the same each time.

I typically just run my script, and when it fails, I fix it for that specific file, but then I'd want to resume from that file, and not restart from the beginning.


If the files are sorted, then you can use '<' and '>' operators to do a stringwise compare of two variables:

startfile=$1
for FILE in ./tests/*; do
    if ! [ "$FILE" '<' "$startfile" ] ; then
        echo doing something with $FILE
    else
        echo Skipping $FILE
    fi
done


for file in `ls tests | grep -A 999999 "^tests/startfile$" | tail -n +2`
do
  whatever
done

grep -A 999999 lists tests/startfile (^ and $ to avoid partial match) and the 999999 next entries.

tail -n +2 lists everything starting from line 2 (1-based)

0

精彩评论

暂无评论...
验证码 换一张
取 消