I've been trying to process the output of find
with parallel
, which in turn invoked a shell (some textual substitutions were needed). I observed some strange behaviour, which I cannot really explain to myself.
In each directory there are a bunch of files, call them file1.xtc
, file2.xtc
. Some of them have names such as file1.part0002.xtc
, etc. If the file passed from find
had the *.part000x.*
name, I need to remove the *.part000x.*
bit, such that the resultant command is comething like
command -f file1.part0001.xtc -s file1.tpr
I used find
and parallel
to that effect but parallel
's substitutions (in particular, the {.}
bit) are not quite sufficient (they remove the .xtc
extension, leaving the .part0001
alone), so here's a command I used to check my output:
find 1st 2nd 3rd -name '*.xtc' -print0 | parallel -0 sh -c 'name=""; name="{.}"; echo {.} ${name%.*}.tpr'
If I use the above command, first declaring name
and assigning an empty string to it (or anything else for that matter), the result is
file1.part0001 file1.tpr
as required (those are the names I need to use for my command). If, however, I run this
find 1st 2nd 3rd -name '*.xtc' -print0 | parallel -0 sh -c 'name="{.}"; echo {.} ${name%.*}.tpr'
the result is:
file1.part0001 .tpr
or it behaves as if $name
didn't exist.
So the my questions are:
-what is the reason for this behaviour?
-what would be the preferred way of dealing with it?
The first question is more important here, as the method I used above is a workaround, which, while not pretty, works. It is not the first time I needed to do a textual substitution like that and this behaviour continues to baffle me.
Output of sh --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
output of a newer version of bash
that I installed and used instead of sh
in the above command (to the same effect) (/usr/local/bin/bash --version
)
GNU bash, version 4.2.0(1)-release (i386-apple-darwin11.4.2)