When Laziness Isn't
I just needed a few rows of UUIDs in a column of a spreadsheet, more for esthetics than anything else. uuidgen
to the rescue.
At the time I didn't realize thatuuidgen
natively supports outputting multiple ids like souuidgen -C 8
The truly lazy path would have been to read the fine uuidgen manual.
Alas, supposing I needed to make multiple calls to uuidgen
, I went with a Perl
one-liner with a loop, as I couldn't recall the Bash
loop syntax.
Here comes the laziness... I I didn't want to write something like this:
perl -e 'print `uuidgen` for @{[1..5]}';
I'm not so found of of perl's de-reference syntax these days, also that array reference/range was giving "the ick" as my kids would say. I needed something lazier, cleaner. I wondered if there were any default/exported arrays available to me that don't have too many elements to them.... Ah, I know!
$ perl -e 'print `uuidgen` for @INC';d2c9c4b9-2126-4eda-ba52-ca30fdc55db0
eac4f86a-04eb-4c1a-aba1-fb1fa5c7dcda
2a2c416c-00bc-46d8-b7ce-c639f73cef26
4cc052cc-6423-4420-bbf5-595a7ad28c51
0bb78a2e-f4e9-44cd-80ae-e463197398f5
37728b6c-69dc-4669-99e7-2814b0d5e2a6
5acf78b2-6938-465b-ad8a-3bf29037e749
87d6d4ef-e85c-40bb-b3c2-acf9dc88f3e1
This is more a case of (ab)using a variable for an unintended purpose, but today it got the job done, even if it wasn't the most lazy approach. Hubris? Maybe.
Just FYI no need for the array dereference in your example, could just do:
Heh, that's pretty clever to get to some number of values. However, you didn't need the reference:
There can even be a parameter in there:
And, I can never remember the bash for loop either, which is how I end up at the same [Stackoverflow answer](https://stackoverflow.com/q/49110/2766176) (where I learned bash has a `help` command for builtins, although the format of `help for` doesn't do itself any favors.
My problem is always the right number and placement of the semicolons on the one-liner:
Crap, this submitted when I was trying to change something. I had an `alias` example right before the `u 14` line.
LarryL and brian, thank for pointing out I could have just used
My perl is rusty to say the least.@briandfoy - My shell now sources a separate zsh_aliases file with many dozen aliases - about half of which I've learned from you, like your set of very handy number conversions. I'm sure others collect yours, too. So it seemed worthwhile to post that this one gives me an error (recent macOS, Perl 5.40.1):
alias u="perl -le 'system q(uuidgen) for 1..$$ARGV[0]'"
Looks like the double '$' is being interpreted as the process ID:
Bareword found where operator expected (Missing operator before "ARGV"?) at -e line 1, near "54994ARGV"
syntax error at -e line 1, near "54994ARGV"
All the best,
This works, getting the value from @ARGV as a slice:
alias un="perl -e 'system q(uuidgen) for 1..@ARGV[0]'"
As does this:
alias u="perl -e 'print qx(uuidgen) for 1..@ARGV[0]'"
But I'm not clear why plain $ARGV[0] doesn't work there. Your suggested '$$ARGV[0]' doesn't mean the same thing as '@ARGV[0]' in perl directly. Is this a shell thing? (I'm using zsh.)
The $$ in $$ARGV[0] is being replaced when the alias is created:
The shell doesn't use @ here so @ARGV[0] works.
The single $ARGV is replaced with nothing, so it also doesn't work:
You can avoid the replacement with bash and other sh-alikes by quoting the $: