Hacker News new | past | comments | ask | show | jobs | submit login

It is a guilty pleasure but I like writing awk scripts that write shell scripts that get piped into sh, for example

   ls | awk '{if (length($1)==7) {print "cat " $1 }}' | sh
it is something you really aren't supposed to do because bad inputs could be executed by the shell. Personally the control structures for bash never stick in my mind because they are so unlike conventional programming languages (and I only write shell scripts sporadically) so I have to look them up in the info pages each time. I could do something like the above with xargs but same thing, I find it painful to look at the man page for xargs.

When I show this trick to younger people it seems most of them aren't familiar with awk at all.

For me the shell is mostly displaced by "single file Python" where I stick to the standard library and don't pip anything, for simple scripting it can be a little more code than bash but there is no cliff where things get more difficult and I code Python almost every day so I know where to find everything in the manual that isn't on my fingertips.




Do You ever use awk's "system" command?

I run stuff like You mentioned (piping to a shell) and also system() frequently. It depends on many factors which one I'll choose.

(FWIW, I'm also quite decent in many shell flavors on many Unix/Linux variants, so that is another determinant)

Eg, the Busybox ash(1) that I frequently work with does not support arrays, but its awk(1) does...


That’s not so terrible if you at least verify the output before the final “| sh”.

Though you’d have to be confident that running it twice is going to give the same results. If it’s remote data that could change then weird/bad/nasty things could happen.

For anything non trivial, best to separate those steps and generate a temp script to execute.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: