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 thatuuidgennatively 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.
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:
$ for i in $(seq 1 5); do uuidgen; done 33E5B2E9-4212-4997-8F5E-B812AE9FBFBD CE775C34-7AC6-4C39-85F4-E03FC218D510 60964665-84E6-476D-A31D-9EE70A2EB079 EE4BFA26-3613-44E3-ADD8-8C2B81D4CA0E 4244A1F1-9EF6-4263-8F79-8B0109D1AB00 $ for i in {1..5}; do uuidgen; done D0C753DE-034A-41F9-A846-109A96891F37 993AA245-C215-4781-AD52-6F6226DAD4DB EAF31617-FC9E-4636-9530-750DF7EA41AF 5DCE95BB-22CE-4018-9178-C6083FEDF134 E129283C-C377-499D-B0A4-5926913F943ECrap, this submitted when I was trying to change something. I had an `alias` example right before the `u 14` line.
@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.)