You are not logged in.

#1 2018-11-13 12:49:44

sjmulder
Member
Registered: 2018-11-13
Posts: 11

within: run a command in other directories

within

When I finally got my new laptop I sat down and typed "git clone git@.../{foo,bar,baz,bit,bat,boo}", pulling in all my projects. Would they all build on this machine?

$ cd foo; make
$ cd ../bar; make
$ ...

This gets old quickly so I did a quick script:

$ for x in *; do cd $x; make; cd ..; done

Better, but I couldn't tell what projects all these messages belonged to. Enter within:

$ within * -- make
foo: make: Nothing to be done for `all'.
bar: make: Nothing to be done for `all'.
baz: make: Nothing to be done for `all'.
...

It was a good exercise in writing an event driven C program as all child processes (possibly concurrently, there's a -j flag) needed to have their stdout and stderr processed to add the prefix. Initially I got a bit stuck with select() and went with BSD's kqueue but eventually I got it working with select() - and on Linux.

GitHub | AUR

(There is some overlap with xargs but the prefixing is very useful to me.)

Last edited by sjmulder (2018-11-13 12:50:06)

Offline

#2 2018-11-13 13:17:17

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,932
Website

Re: within: run a command in other directories

Why not simply

$ for x in *; do make -C "$x"; done

Online

#3 2018-11-13 13:20:35

sjmulder
Member
Registered: 2018-11-13
Posts: 11

Re: within: run a command in other directories

schard wrote:

Why not simply

$ for x in *; do make -C "$x"; done

That works well for commands that traverse directories themselves (like make) but not things like git status. It's also hard to tell apart the output from the different invocations. 'within' will prefix all output with the directory.

Offline

#4 2018-11-13 13:56:00

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: within: run a command in other directories

Absent the desire to have fun implementing simple concepts in a new programming language like C, I'd just use:

$ for x in *; do echo "$x: "; cd "$x"; make; cd ..; done

echo isn't a hard concept to implement in bash over C. tongue

...

Remains to be seen whether linebreaks between the directory name and multiline stdout is preferred...

Last edited by eschwartz (2018-11-13 13:56:38)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#5 2018-11-13 14:03:27

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,932
Website

Re: within: run a command in other directories

I think the salient point of OP's program is, that it allows to run a command in multiple directories in parallel (or just concurrently, idk) so that if there is more than one line of output of per-directory run program, it prepends the directory name to the respective output line so that one can see, from which "thread" the output is actually coming.

Online

#6 2018-11-13 14:06:28

sjmulder
Member
Registered: 2018-11-13
Posts: 11

Re: within: run a command in other directories

eschwartz wrote:

echo isn't a hard concept to implement in bash over C. tongue

But that's not what it does – it prepends output to every line.

Anyway, just putting it out there for people who might find it useful. I'm well familiar with the Unix swiff army knife but none of the blades scratched this particular itch.

Offline

Board footer

Powered by FluxBB