[GRASS-dev] g.mremove bug?
    Ivan Shmakov 
    ivan at theory.asu.ru
       
    Sun Feb 24 10:47:25 EST 2008
    
    
  
>>>>> Glynn Clements <glynn at gclements.plus.com> writes:
 >>> The more fundamental problem with using xargs is that the GRASS
 >>> parser requires multiple "answers" for any given option to be
 >>> separated by commas, not spaces.
 >> Does it?  IIUC, the following commands are completely equivalent as
 >> long as the GRASS parser is concerned:
 >> $ g.remove rast=foo,bar
 >> $ g.remove rast=foo rast=bar
 > Right; I was thinking specifically about the former case. It's so
 > long since I've seen the latter form used that I had forgotten about
 > it.
	That's strange -- I use the latter form quite often, mostly as:
$ g.mlist ... | sed -e s/^/OPTION=/ | xargs GRASS-COMMAND ...
	or, e. g.:
$ r.patch output=foo $(g.mlist ... | sed -e s/^/input=/)
[...]
 >>> That effectively requires g.mremove to collate names itself.
 >>> Invoking g.remove once per map is simpler, but it's also
 >>> inefficient.
 >> Huh?  Even simpler than the following?
 > Yes.
 > Generating shell commands using sed then executing them is anything
 > but simple;
	But the idea is overly simple: since `g.remove' requires every
	map name to be prefixed by its type (e. g., `rast='), we're
	going exactly this way -- changing every line to begin with
	`TYPE='.
 > particularly when the sed command is itself generated using shell
 > features (variable substitution).
 > "while read map ; do g.remove ... ; done" may be more verbose, but
 > it's much easier to understand.
	That'll be:
foo () {
    while read map ; do g.remove "$1=$map" ; done
}
	if there's a desire to have just one function for every map
	type.  I don't think it's much more complex than:
foo () {
    while read map ; do echo "$1=$map" ; done | xargs g.remove
}
	or, with a more appropriate tool to do the substitution:
foo () {
    sed s/^/"$1"/ | xargs g.remove
}
	For my students, I've explained it roughly as follows:
	* there're some commands that read a file with a list of objects
	  (files) to perform the operation on (e. g., $ wget -i);
	  standard input is a particular case of a file ($ wget -i -);
	* there're much more commands that obtain the list from the
	  command line (e. g., $ ls, $ chmod, etc.);
	* there're commands which can do both ($ wget URL, or $ echo URL
	  | wget -i -);
	* for these commands that cannot read the list from a file,
	  there's the `xargs' command, which ``transforms'' the contents
	  of the given file into the command line arguments for a
	  command; then, there're OS limits on the command line length.
	Anyway, could you please comment on my patch [1]?  I've tested
	it a bit, but the `g.mremove' command wouldn't probably be the
	one I'll use myself much.
[1] nntp://news.gmane.org/gmane.comp.gis.grass.devel/25056
    
    
More information about the grass-dev
mailing list