I want create a ruby script that I can run on the command line that will:
1. run a command line command like e.g. "ls -l"
2. then I pass an argument for a specific column from the output of #1
3. for each column specified in #2, it will run another command
ls -l was just an example, I want this to be generic.
I’m newish to ruby, still not getting some of this so if you could commend what and how you did it that would be very helpful for me.
I frequently use
|awk '{print $5;}'(replace5with the one-indexed column of your choice) in pipelines. You can get similarly easy one-liners withrubyif you use some funny command-line switches. Compare:vs
ruby -nplaces an implicitwhile gets() do .. endloop around your code, andruby -epasses script elements on the command line. Combine the two and you get some easy one-liner scripting ability, similar toperl -neorawk(1).The manpage mentions another option that looks very useful:
Juan points out how to use it (single quotes prevents my shell,
bash(1), from interpreting the$F[3]as a variable reference).: