开发者

How to exit a bash loop using ls?

开发者 https://www.devze.com 2022-12-24 08:50 出处:网络
I wan开发者_开发技巧t to loop over a series of files in a directory in batches and then exit when the directory is empty.

I wan开发者_开发技巧t to loop over a series of files in a directory in batches and then exit when the directory is empty.

At $work 'myprog' is actually a program which processes (and archives) incoming email in a Maildir in batches of 100.

I am after something simple I can put into cron.

#!/bin/bash

# Setup
mkdir -p foo && touch foo/file_{1,2,3,4}.txt
alias myprog='f=`ls foo/file_*.txt | head -n1`; rm -v $f'

# Loop and then exit ?!
# This line to go into cron.
while (ls foo); do ls foo/ | wc -l; myprog; sleep 1; done

Any ideas?


I suppose you can do:

#!/bin/bash
# ...
while (ls foo/* &> /dev/null); do myprog; sleep 1; done

If nothing matches foo/* (if no visible files are in directory foo), ls will fail. &> /dev/null keeps ls quiet.

0

精彩评论

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