You are not logged in.

#1 2012-07-03 19:36:42

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

Help with a construct like this: find | parallel function [SOLVED]

I'd like to use a find | parallel function combo to:

1) Find all directories
2) Enter the directories and run a predefined function


#!/bin/bash

bcue() {
  shntool cue *.flac > foo.cue
  shntool join *.flac
}

find -type d | parallel cd {} ; bcue

Running this doesn't work as expected.  It simply runs the bcue function from the dir from which I run the script.  How can I call the function after I cd into the directory using parallel?

Last edited by graysky (2012-07-03 23:32:34)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2012-07-03 19:39:49

Cloudef
Member
Registered: 2010-10-12
Posts: 636

Re: Help with a construct like this: find | parallel function [SOLVED]

Edit: Err.. Nevermind.

Last edited by Cloudef (2012-07-03 19:51:17)

Offline

#3 2012-07-03 19:42:03

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: Help with a construct like this: find | parallel function [SOLVED]

I've never used parallel, but it seems you'd want to put {} around everything right of the pipe.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#4 2012-07-03 20:43:01

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Help with a construct like this: find | parallel function [SOLVED]

Because it's being interpreted as two commands:

parallel cd {};
bcue

You need to invoke a shell in order to change directories before executing something. Something like (quoting problems notwithstanding)

parallel bash -c "cd {}; bcue"

This still won't actually work because the inner invocation of bash doesn't inherit the definition of bcue, but it's a step in the right direction. Consider embedding the two shntool invocations directly, or maybe using a different approach altogether. I'd use Perl personally, but that's probably at least a little bit because my bash sucks.

Offline

#5 2012-07-03 21:01:23

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,094

Re: Help with a construct like this: find | parallel function [SOLVED]

You might want to look at the -execdir function that find has too.


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#6 2012-07-03 21:51:01

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Help with a construct like this: find | parallel function [SOLVED]

Yes, but -execdir won't do the operations in parallel...

However, you could instead do something like

find -type d -execdir sh -c "cd {}; bcue &" \;

which will start the operations represented by 'bcue' in the background, which may or may not be exactly the same thing as the original version with parallel. But you still can't use it as written because the new "inner" shell doesn't know anything about the bcue function. Consider making a script out of it. Better yet, consider making it a script that takes a directory name as an argument; then you could just do

find -type d | parallel $script {}

(making some guesses about the correct invocation of parallel based on what you wrote; I don't use it myself.)

Offline

#7 2012-07-03 23:32:15

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

Re: Help with a construct like this: find | parallel function [SOLVED]

Nice discussion, guys.  Solution:

$ cat ~/bin/bincue 
#!/bin/bash
[[ -z "$1" ]] && echo "Must give a dir!" && exit 1
cd "$1"
shntool cue *.flac > "$1".cue
shntool join *.flac
$ cd /mnt/music
$ find . -type d | parallel bincue {}

Last edited by graysky (2012-07-03 23:37:57)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

Board footer

Powered by FluxBB