命令行基础知识:生成UUID

Universally unique identifiers (UUIDs) are 128-bit numbers that are accepted as being unique on the local system they are created on as well as among the UUIDs created on other systems in the past as well as the future. Because of their uniqueness, they can be created on both the client and server and come in really handy in situations where an auto incremented primary key can fall short.

通用唯一标识符(UUID)是128位数字,在创建它们的本地系统以及过去和将来在其他系统上创建的UUID中,它们被认为是唯一的。 由于它们的独特性,它们可以在客户端和服务器上创建,并且在自动递增主键可能不足的情况下非常有用。

Because of their uniqueness, UUIDs are well suited for generating test data. Need a random string? A UUID is fine. What about an email? UUID@UUID.com is great. Need a bunch of random string? UUIDs will be unique, making them easy to track down as they move through a system.

由于其独特性,UUID非常适合生成测试数据。 需要一个随机字符串吗? UUID可以。 电子邮件呢? UUID@UUID.com很棒。 需要一堆随机字符串吗? UUID将是唯一的,从而使它们在系统中移动时易于跟踪。

入门 (Getting started)

To generate universally unique identifiers from the command-line you will need the uuidgen utility.

要从命令行生成通用的唯一标识符,您将需要uuidgen实用程序。

Fortunately, the command is pretty standard issue on Unix-like operating systems like Linux and macOS.

幸运的是,在像Unix之类的操作系统(例如Linux和macOS)上,该命令是非常标准的问题。

If you don’t happen to have the command available (try running it to see) please consult with your system’s package manager and see if it’s available.

如果您碰巧没有该命令可用(请尝试运行该命令以查看),请咨询系统的程序包管理器并查看其是否可用。

Also worth noting, the macOS version of uuidgen does function a bit differently than that of the Linux version in that is returns UUIDs in all capital letters.

同样值得注意的是, uuidgen的macOS版本与Linux版本的功能确实有所不同,因为它以所有大写字母返回UUID。

For the sake of example, this shouldn’t matter much.

举个例子,这没什么大不了的。

生成UUID (Generating a UUID)

The easiest way to generate a UUID is to run uuidgen without any arguments. Out of the box, uuidgen will generate a random UUID assuming you have a high-quality random number generator available:

生成UUID的最简单方法是运行不带任何参数的uuidgen 。 开箱即用, uuidgen将生成一个随机UUID, uuidgen是您有一个可用的高质量随机数生成器:

$ uuidgen
a522f494-92ce-44e9-b1a3-f891baed8d60

You can also generate time-based and hash-based UUIDs but generally speaking, the random values are probably sufficient.

您还可以生成基于时间的UUID和基于哈希的UUID,但通常来说,随机值可能就足够了。

生成一堆UUID (Generating a bunch of UUIDs)

Generating a single UUID is pretty easy. To generate a bunch of them, we will need to leverage a small bit of shell scripting.

生成单个UUID非常容易。 要生成一堆,我们将需要利用少量的Shell脚本。

Let’s say we wanted to generate 10 UUIDs, we could write a short loop:

假设我们要生成10个UUID,可以编写一个短循环:

$ for i in {1..10}; do uuidgen; done
834efdb6-6044-4b44-8fcb-560710936f37
e8fa8d54-641a-4d7b-9422-91474d713c62
dff59ac0-4d80-4b96-85c4-14f3a118e7fe
511fea83-9f5f-4606-85ec-3d769da4bf63
3bc82ef7-1138-4f97-945a-08626a42a648
a33abc11-264e-4bbb-82e8-b87226bb4383
2a38839e-3b0d-47f0-9e60-d6b19c0978ad
74dca5e8-c702-4e70-ad16-0a16a64d55fa
cd13d088-21cf-4286-ae61-0643d321dd9e
9aec3d5a-a339-4f24-b5a3-8419ac8542f2

You can swap the 10 out for whatever number you’d like (the system’s math co-processor permitting).

您可以将10换成您想要的任何数字(系统的数学协处理器允许)。

If you wanted to generate a list of comma-separated values (CSV) with 2 UUIDs per line, you simply echo out multiple UUIDs during each iteration of our for loop:

如果要生成每行2个UUID的逗号分隔值(CSV)列表,则只需在我们的for循环的每次迭代期间echo多个UUID:

$ for i in {1..10}; do echo `uuidgen`,`uuidgen`; done
63b1146f-9e7c-4e1f-82eb-3fe378e203df,ed9d6201-e5b2-4410-9ab1-35c8ca037994
8d3981b6-f112-4f21-ac4b-44791e279b2a,eb63310e-d436-44fa-80c6-65721a300a2b
0eddfe24-1c2e-43a1-b2c2-9d3af6bad837,62ef1782-76a2-4b3c-ac69-1c2d02f65789
29f18766-fc9d-46a4-a1d0-e112738edb30,b6bd303d-1148-4f46-bec7-d7e4cb6e4f03
865bcf30-6a8b-49d6-8b27-8dc51620adf7,972b0959-4270-4683-b19b-360b2605f2d0
0d82d54b-566a-45d1-b3a8-5da1a88bceb3,1c67a802-9647-46b1-bde4-3053699b27f9
778b5415-3e1f-4bc5-a349-499459ac4ab7,7e1a2081-c742-4882-9154-e5d2a4af630c
e6cc95bd-3ee1-43cb-bea1-51783de5fc57,5088d3a3-ab67-4684-8761-e48bb14596ec
a7453bc0-b5e5-41a3-9ed4-cf4d8e0908a2,957ef50f-7889-4335-9f40-17878e3d20fe
3689362d-588a-409e-bd2c-d6fdaa361574,9ffe7c8d-9afb-4b24-a5b7-b29a06f6fac7

Based on the unique nature of UUIDs, we don’t have to worry about any duplicates in our generated data!

基于UUID的独特性质,我们不必担心生成的数据中有任何重复项!

Remember how I mentioned generating email addresses? A small tweak to our echo statement, and we can generate a list of email-looking data:

还记得我提到过生成电子邮件地址吗? 对我们的echo语句进行一些细微调整,我们可以生成电子邮件外观数据的列表:

$ for i in {1..10}; do echo `uuidgen`@`uuidgen`.com; done
7dd44050-9df4-43aa-b3b4-3b47eff8fc31@3052e93c-95d1-40f5-b468-3d4e06dd208b.com
cca71187-f666-46ff-81c6-eb3b72ff6972@30f4c9a8-712e-4f4c-ad3a-4b55ef85eee0.com
6ff086ad-493d-4b3a-8ed1-970239d7125b@8302d772-4deb-43d1-8901-0a3b4f747b55.com
f9813daa-6a8e-4543-8708-d42cefdda20a@d586854c-7df9-4046-89f8-51f960973afb.com
a7e9e43b-d2b1-4415-b73d-ff72b713e45f@a7c56c2c-df25-44bc-872d-a893f750b54d.com
0d1d13fe-777d-44d8-b1b2-302ca1e48aa1@7c2d8e6a-fa8b-4fa3-a0ef-8360aa42e730.com
f85d0772-22d2-43d0-8d71-4e6714c2bb20@fb4f74fe-f9f9-4e86-b31d-f148344a97e0.com
f46eb868-0a99-4291-98f2-19d95f1e9fbb@37ef072d-c515-4145-8b8a-edf32ec18bd2.com
eaa4a63e-2646-427a-a892-f8027c2791ed@33daf102-2b5b-4070-88c5-261fe5d96cfa.com
d75f6720-b249-4395-bcc7-9ffe2b67cabb@457b04b4-3c15-4b77-aae2-9afd6803bcfe.com

These are all well and good, but they aren’t real email addresses that we can easily check.

这些都很好,但它们不是我们可以轻松检查的真实电子邮件地址。

If we were to tweak the output one more time, and swap the second uuidgen for a disposable email address domain, like mailinator.com we will not only have a list of email-looking data, but it will be a list email addresses we could actually monitor!

如果我们要再次调整输出,然后将第二个uuidgen交换为一个可使用的电子邮件地址域,例如mailinator.com我们不仅将拥有一个看起来像电子邮件的数据列表,而且还将是一个列表电子邮件地址,我们可以实际监控!

$ for i in {1..10}; do echo `uuidgen`@mailinator.com; done
4ba50929-520b-49f7-996d-e369be5d6232@mailinator.com
16deaeae-64bd-45f0-9f73-b32d41ca1bfb@mailinator.com
743701e8-0dc5-4851-8fc4-24d155755bdc@mailinator.com
adff0015-c535-431a-970f-98ffd1fc21eb@mailinator.com
6516fcb3-e54f-4800-a6cc-11d50d756f28@mailinator.com
8a9c5252-bd0c-4c3b-a7c9-4b60ebcc4294@mailinator.com
eed94fd6-b075-493c-8d8e-3acae90d5629@mailinator.com
f4ab80d2-85ca-4722-a260-0f84c37051fd@mailinator.com
53ead1d0-cc70-410f-a91a-4a79b339fba2@mailinator.com
b208e103-d7f1-4f6d-838d-530d6339dce7@mailinator.com

And for good measure, if you wanted to save the output of any of the previous examples to a file, you can append > /path/to/some/file to pipe the output:

并且,如果想将前面任何示例的输出保存到文件中,则可以很好地实现,可以添加> /path/to/some/file来管道输出:

$ for i in {1..10}; do echo `uuidgen`@mailinator.com; done > /tmp/emails.txt

$ cat /tmp/emails.txt
826119d2-f590-4fa3-ba7e-0717869d40b1@mailinator.com
795fec1a-76fe-4fed-8a06-ed517c1a5e7d@mailinator.com
14a502ad-0aa9-40e5-a46f-5806264b5316@mailinator.com
c6c2a588-7cce-4675-a490-0101d7bcc614@mailinator.com
7346c15b-0c92-44c4-a854-5de18c0c202d@mailinator.com
c67a535a-e28d-43b1-b553-c203bc22a821@mailinator.com
76d22d18-0f09-405d-9903-eb44ec93b605@mailinator.com
2b631756-21e6-4d95-873b-3245797f9028@mailinator.com
aab686e8-540e-43e9-9e24-ca04fbf4d414@mailinator.com
a577e9c9-0ad1-4934-b5f1-17b68938fff8@mailinator.com

结论 (Conclusion)

Universally unique identifiers are like a better version of a random number.

普遍唯一的标识符就像一个更好的随机数版本。

Their uniqueness makes them quite powerful and combined with some light shell scripting on the command-line, we’re able to generate a substantial volume of data. All without needing to load up our favorite language’s package repository.

它们的独特性使它们非常强大,并结合了命令行上的一些轻量级shell脚本,我们能够生成大量数据。 所有这些都无需加载我们喜欢的语言的软件包存储库。

Next time you’re in need of a UUID, save yourself the search for “online UUID generator” and take care of your system’s command-line interface!

下次您需要UUID时,请省去对“在线UUID生成器”的搜索,并照顾好系统的命令行界面!

Want to learn more about your system’s particular implementation of uuidgen? Simply type man uuidgen in your favorite terminal and go beyond the basics covered here.

想更多地了解您的系统对uuidgen的特定实现? 只需在您最喜欢的终端中输入man uuidgen ,并且超出了此处介绍的基础知识。

翻译自: https://www.digitalocean.com/community/tutorials/workflow-command-line-basics-generating-uuids

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值