2013-05-04 126 views
5

有时在bash脚本中,我需要生成新的GUID(Global Unique Identifier)Linux命令生成新的GUID?

我已经做了,通过生成一个新的GUID一个简单的Python脚本:看here

#! /usr/bin/env python 
import uuid 
print str(uuid.uuid1()) 

但我需要这个脚本复制到任何一个我工作在新的系统。

我的问题是:任何人都可以引入包含类似命令的命令或包?

+1

[命令行GUID for Unix和Windows?]的可能重复(http://stackoverflow.com/questions/569858/command-line-guid-for -unix-and-windows) – hek2mgl 2013-05-04 08:56:28

+1

不错。你怎么能不能谷歌的Linux命令来生成新的GUID?' :D – hek2mgl 2013-05-04 08:57:30

+0

@ hek2mgl:你是对的 – pylover 2013-05-04 09:01:00

回答

10

假设你没有uuidgen,你并不需要的脚本:

$ python -c 'import uuid; print str(uuid.uuid1())' 
b7fedc9e-7f96-11e3-b431-f0def1223c18 
+2

不错,但它似乎并不完美随机:每次通话只能更改8个首字符。 – krookedking 2014-12-03 11:02:58

+3

它不应该是* random *,它应该是* unique *。如果你想随机使用,请根据文档使用'uuid.uuid4()'。 – MikeyB 2014-12-03 21:47:42

3

既然你想要一个随机 UUID,你想用的,而不是1 4型:

python -c 'import uuid; print str(uuid.uuid4())' 

这个Wikipedia article解释了不同类型的UUID。你想要“类型4(随机)”。

我用Python生成类型的任意数量的4 UUID的散装写了一个小bash函数:

# uuid [count] 
# 
# Generate type 4 (random) UUID, or [count] type 4 UUIDs. 
function uuid() 
{ 
    local count=1 
    if [[ ! -z "$1" ]]; then 
     if [[ "$1" =~ [^0-9] ]]; then 
      echo "Usage: $FUNCNAME [count]" >&2 
      return 1 
     fi 

     count="$1" 
    fi 

    python -c 'import uuid; print("\n".join([str(uuid.uuid4()).upper() for x in range('"$count"')]))' 
} 

如果你喜欢小写,变化:

python -c 'import uuid; print("\n".join([str(uuid.uuid4()).upper() for x in range('"$count"')]))' 

要:

python -c 'import uuid; print("\n".join([str(uuid.uuid4()) for x in range('"$count"')]))'