evry icon indicating copy to clipboard operation
evry copied to clipboard

discussion: extended looping features

Open c-nv-s opened this issue 1 year ago • 5 comments

would there be any interest in extending the features of this cli tool to match loop-rs I was searching for the closest thing written in Go and this seems to be it.

Loops in bash are surprisingly complicated and fickle! I wanted a simple and intuitive way to write controllable loops that:

Run on controllable timers! $ loop 'ls' --every 10s

Have custom counters! $ loop 'touch $COUNT.txt' --count-by 5

Loop until output matches a condition! $ loop --until-contains 200 -- ./get_response_code.sh --site mysite.biz

Loop until a certain time! $ loop './poke_server' --for-duration 8h

Loop until a program succeeds (or fails!) $ loop './poke_server' --until-success

Iterate over the standard input! $ cat files_to_create.txt | loop 'touch $ITEM'

Get a summary of the runs! $ loop 'ls' --for-duration 10min --summary

Run until output changes or stays the same between invocations! $ loop --until-changes date +%s $ loop --until-same date +%s

c-nv-s avatar Oct 09 '24 14:10 c-nv-s

@c-nv-s Thank you for your comment.

I think loop-rs is very interesting. However, the concept seems to be different from evry. The main function of loop-rs is to loop the execution of commands, while evry's main function is to split STDIN.

k1LoW avatar Oct 09 '24 15:10 k1LoW

ok. I just thought that evry's features seem to currently be a subset of what loop-rs is i.e. controllable counters, and iterate over stdin.

If you ever feel inspiration to do a go implementation of loop-rs then I think you are already part way there.

c-nv-s avatar Oct 09 '24 15:10 c-nv-s

This is a simple question, but can loop-rs split STDIN into 5-line segments and process them, or count the number of lines every 10 seconds? (This is the main feature of evry)

k1LoW avatar Oct 10 '24 00:10 k1LoW

You can see that it holds each line of input from stdin in a variable called $ITEM, so I guess you would have to use a combination of the counter $COUNT to determine how many lines you want to process. i.e. append each $ITEM to $ITEMS and if wc -l of $ITEMS is 5 then do something and reset.

c-nv-s avatar Oct 10 '24 10:10 c-nv-s

How would you write the following command in loop-rs?

$ tail -F access.log | evry -l 1000 -c 'somecommand'

This command passes the access log to somecommand as STDIN in 1,000-line segments.

k1LoW avatar Oct 11 '24 03:10 k1LoW

Very sorry for the late response. I didn't have access to loop when you asked and then life took over for a while.

yes the evry expression of that command will be more terse

cat access.log | loop 'cumulativeString="${cumulativeString} $ITEM" ; if [ $(( $COUNT % 1000 )) -eq 0 ] ;then  somecommand "${cumulativeString}" ; fi'

as you can see loop can read from stdin but it just doesn't follow stdin like evry does

if you wanted to track changes to the access log you would have to workaround with inotify-tools or something.

 loop 'inotifywait --event modify ./access.log && if [ $(( $COUNT % 1000 )) -eq 0 ]; then tail -n 1000 ./access.log | xargs -I "{}" somecommand {}; fi ' --until-fail 

c-nv-s avatar Jan 02 '25 22:01 c-nv-s

happy new year by the way

c-nv-s avatar Jan 02 '25 22:01 c-nv-s