On 17.10.2014 08:58, Kenny McCormack wrote:
> [...]
>
> P.S. Another poster has given a shell-only solution, but I think that's
> ugly and limited in scope (and off-topic here).
I deliberately omitted the [OT] flag since all solutions that primarily use
"system()" or "| sh" are depending on specific shell features and syntax;
the key functionality (modulo the awk wrapper glue) in all posted solutions
is OT here.
WRT "limited in scope" I'm not sure what you mean to say. The OP's requested
function is shell (and [non-awk-]tool) functionality. You don't gain much if
using awk just for the implicit loop that shells also support. WRT "uglyness"
I think that unnecessarily spreading functionality between tools is ugly.
YMMV.
Moreover, the awk solutions are also inferior because they are not correct
in the general case; you need to generate quotes around your shell entities.
This quoting will need escaping, and all this will make the _awk_ solution
even uglier:
print "echo \"" $0 "\"\ndiff",$0,"\"../v28/"gensub(/\.DAT$/,".m\"",1) "\necho"
(Hope I have located the stuff in the right place in that quoting mess.[*])
> I like the idiom of using
> 'gawk' to generate shell commands and then piping the output to 'sh'.
I like it too; I use it whereever appropriate. (In the given case I don't
think you gain anything, rather make a simple function less legible. YMMV.)
> It's
> quite useful. Note that this method is also more efficient than using
> system() internally to the script, since the shell only gets spawned once
> (rather than once per command executed).
Yes.
Janis
[*] Note to the OP: Using printf will typically result in better readable
code, IMO, in cases you have many arguments. Here the unquoted form...
printf "\necho %s\ndiff %s %s\necho \n",
$0, $0, "../v28/"gensub(/\.DAT$/,".m",1)
(Though, because of readability, I'd prefer using mutiple prints, one for
each to generate shell command...
printf "echo %s\n", ...
printf "diff %s %s\n", ...
)
Or quoted...
printf "\necho %s\ndiff \"%s\" \"%s\"\n echo\n",
$0, $0, "../v28/"gensub(/\.DAT$/,".m",1)