Giter VIP home page Giter VIP logo

Comments (15)

madeddie avatar madeddie commented on August 11, 2024

Could you elaborate what you mean with "speak"?

from loop.

Miserlou avatar Miserlou commented on August 11, 2024

Just read from stdin as you'd expect the tool to.

The complications there are how this actually gets used by the program, as I can imagine two use cases:

The simpler, but less useful, making stdin available in the command context:

$ echo "hello" | loop "echo $STDIN" --num 3
hello
hello
hello

The more complicated-but-useful, making it available in the command context:

$ cat my-list-of-files-to-create.txt
hello.jpg
goodbye.jpg
$ cat my-list-of-files-to-create.txt | loop "touch $ITEM" --for-stdin
$ ls
hello.jpg
goodbye.jpg

Or something like that. Does that make sense?

from loop.

madeddie avatar madeddie commented on August 11, 2024

Yeah, makes sense, although that's very much already available in the --for flag. Having the option to pipe things does make things easier.

On Unix this could also be accomplished with:
cat my-list-of-files-to-create.txt | paste -sd',' - | xargs loop 'echo $ITEM' --for

  • cat my-list-of-files-to-create.txt could be any command
  • paste -sd',' pastes all lines together into one string with a comma as separator
  • xargs executes the command with stdin added at the end

Could you explain what you have in mind for stdout? Let Loop reuse the output of the command it executes?

from loop.

Miserlou avatar Miserlou commented on August 11, 2024

This could be accomplished like you described, but I find that to be a pretty hideous solution. The goal of the tool is to make it easy and intuitive to write loops for common scenarios, so it should be able to read from stdin directly without the need for any more tools (die, xargs, die!)

stdout is probably more complicated and I probably don't know enough to get it right on the first try. I'm imagining something like..

$ loop "echo my-cool-filename-$COUNT.jpg" --num 3 | upload_file.sh 
uploading my-cool-filename-1.jpg..
uploading my-cool-filename-2.jpg..
uploading my-cool-filename-3.jpg..

I don't know if the pipeing could happen upon every iteration of the loop, or if it would have to be buffered and then all sent out at once over newlines. I imagine the latter, unfortunately.

from loop.

Miserlou avatar Miserlou commented on August 11, 2024

Gah, there are even more problems with that.. ex, does it execute

upload_file.sh uploading my-cool-filename-1.jpg my-cool-filename-2.jpg my-cool-filename-3.jpg

or

upload_file.sh my-cool-filename-1.jpg
upload_file.sh my-cool-filename-2.jpg
upload_file.sh my-cool-filename-3.jpg

Even more annoying, the whole thing is made easier simply by loop "upload_file.sh my-cool-filename-$COUNT.jpg" --num 3, so perhaps that's not a great example!

from loop.

madeddie avatar madeddie commented on August 11, 2024

Yeah, I agree about stdin and the one-liner, it's also not very platform agnostic.

About the stdout, piping doesn't work like that, you can't spawn multiple processes on a single pipe like that (unless you use something like xargs). There might be a really advanced idea I'm not familiar with out there, but I think that's a dead end.

Something like a specific flag for an output using command could work though, like the find command:
loop "echo my-cool-filename-$COUNT.jpg" --num 3 --exec upload_file.sh

from loop.

madeddie avatar madeddie commented on August 11, 2024

Ah yes, so you noticed the problem, indeed like you said, the command piped to would get all output in one execution.

For loops where the command generates output that you want to use, instead of being able to use the $ITEM or $COUNT value directly in the loop command, the --exec might still be worthwhile.

Mind you, on a unix platform, I would personally still tell people to use xargs, just because I'm old-skool like that; do one thing and do it well :D

from loop.

Miserlou avatar Miserlou commented on August 11, 2024

For the purposes of this ticket, I think that stdin is a lot more important. "Do a thing for thing in list of things" is going to be one of the most common use cases, so that's really the one I want to design around.

I think stdout doesn't need to be too fancy. It's okay to a) grow a big-ass list of command responses and pipe them out all at once after the loop finishes or b) ignore it entirely for now.

I don't think I really see much benefit to --exec since there's already a command string that gets looped, so loop "echo my-cool-filename-$COUNT.jpg" --num 3 --exec upload_file.sh simply becomes loop "upload_file.sh my-cool-filename-$COUNT.jpg" --num 3, which is the whole point of the the thing to begin with!

from loop.

Miserlou avatar Miserlou commented on August 11, 2024

I'm thinking more about that --exec use case and I think I get what you're saying, but wouldn't it still be more intuitive to loop 'calc $COUNT*2 | cat' than to loop 'calc $COUNT*2' --exec "cat"?

from loop.

madeddie avatar madeddie commented on August 11, 2024

Yeah, true. Although that might get just as ugly as my paste -sd example :)
I'll see about other use-cases, but for now, I think you're right.

from loop.

Miserlou avatar Miserlou commented on August 11, 2024

I implemented some versions of the stdin stuff. Not sure if I'm happy with it yet.

Examples:

$ cat README.md | loop 'echo $ITEM' -n 5
![Logo, with help from Linn Kristiansen](https://i.imgur.com/TQp8nu3.png)

# loop [![Build Status](https://travis-ci.org/Miserlou/Loop.svg)](https://travis-ci.org/Miserlou/Loop) [![crates.io](https://img.shields.io/crates/v/loop-rs.svg)](https://crates.io/crates/loop-rs)

UNIX's missing `loop` command.

and:

$ cat my-list-of-files-to-create.txt | loop 'touch $ITEM'
$ ls
hello.jpg 
goodbye.jpg

It can now also read directly from the keyboard with -i:

$ loop 'echo $ITEM | tr a-z A-Z' -i
hello
world^D
HELLO
WORLD

from loop.

Miserlou avatar Miserlou commented on August 11, 2024

The problem as it stands is that it can't support the trivial use case:

$ echo "hello" | loop "echo $ITEM" --num 3
hello
hello
hello

since "hello" is only once, it'll only execute one time.

from loop.

madeddie avatar madeddie commented on August 11, 2024

That depends on if you'd like this to happen:

$ cat my-list-of-files-to-create.txt | loop 'echo $STDIN' --num 3
hello.jpg 
goodbye.jpg
hello.jpg 
goodbye.jpg
hello.jpg 
goodbye.jpg

from loop.

Miserlou avatar Miserlou commented on August 11, 2024

I'm not sure. Does that seem like the right behavior to you?

Perhaps that should be defined with an additional argument, ex:

$ echo "hello" | loop 'echo $STDIN' --num=3 --continue
hello
hello
hello

Not sure about that. Need a better concept for it maybe.

from loop.

benjibee avatar benjibee commented on August 11, 2024

Just going to add to this (and #20) as I installed loop because it looked like the perfect solution to testing the HTTP status code of a page while I work on it. Basically the CLI equivalent of mashing reload:

loop --every 10s -- curl -sI https://www.wikipedia.org | head -n 1

Which returns:

HTTP/2 200

Once and then errors out with the same thread 'main' panicked at 'failed printing to stdout

from loop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.