dwiextract and use of foreach

Dear mrtrix3 experts,

Hello, I have a question about dwiextract before doing dwipreproc. I have about 80 subjects, so I want to use foreach bash for time saving, but I dont know how to apply it for dwiextract.
I used bash for dwidenoise and degibbs, and ready to proceed to further steps : motion and distortion correction.
I am currently following the BATMAN tutorial using my own data.

I got some error note, and tried bunch of different things, but I cannot find the solution.

$ foreach gibbs/.mif : dwiextract IN gibbs/PRE.mif - -bzero | mrmath - mean mean_b0_AP.mif -axis 3
dwiextract: [ERROR] Expected exactly 2 arguments (3 supplied)
dwiextract: [ERROR] Usage: dwiextract input output
dwiextract: [ERROR] Yours: dwiextract gibbs/
.mif gibbs/*.mif -
mrmath: [ERROR] no filename supplied to standard input (broken pipe?)
mrmath: [ERROR] error opening image “-”

Thank you.!

Try the following:

forach gibbs/*.mif : dwiextract IN - -bzero | mrmath - mean mean_b0_AP_PRE.mif -axis 3

This assumes that the dwi files corresponding to the 80 subjects are inside the gibbs folder.
Note that:

IN gives the path matched by the pattern
NAME gives file name including extension
PRE gives file name without extension

You could therefore save the generated mean_b0 files in a new folder by using the command

mrmath - mean mean_b0_AP/NAME -axis 3

instead of the one above.

Hope this helps.

Just to note that the issue here is likely to be that you need the pipe symbol | to be escaped for this to work - i.e. replace the | with \|. Otherwise the shell will try to pipe the output of the whole of the first foreach loop through to the single second command.