Linux和Oracle生成随机字符串

Linux 和Oracle里面如何生成随机字符串:
Linux 可以利用uuidgen来生成
[oracle@oracle ~]$ uuidgen
f239007b-14a8-4a38-9925-794f5aad7740
我们要做的就是把里面的"-"去掉,数字替换为字母,然后截取想要的位数

vi random_string.sh
#!/bin/bash
count=10
help(){
       echo "请输入你想生成的随机字符串的数量,默认10个."
       usage:sh "$0" arguments
      }

if [[ -z "$1" ]];then
   count=10
else
   count="$1"
fi


for i in `seq $count`
do 
    str=$(uuidgen |sed 's/-//g'|tr [0-9] [a-z]|cut -c 1-10)
    echo "$str"
done
exit 1
不带参数的
[root@oracle shellscript]# sh random_string.sh
debdbbjeba
iehicahgca
fjdcceaihb
jfcjededfa
cbhijeefcj
jabeaeicja
dagbeadejc
ifcefaagag
fjhjigacdb
faeabdbabh
[root@oracle shellscript]# sh random_string.sh 5
efjfcdieab
ddccgfdccj
gcicaagifd
aicbgificj
bfddicbbad


######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################


Oracle:
oracle提供了dbms_random.string

create or replace type str is table of varchar(1000);
/


create or replace function rand_str(count_str number)
	return str
	authid current_user
  is
   strings str:=str();
   begin 
       strings.extend(count_str);
        for i in 1..count_str
         loop
          strings(i):=dbms_random.string('u',10);
          dbms_output.put_line(strings(i));
         end loop;
    return strings;
   end;
   /

select * from table(rand_str(5));
SYS@PROD>select * from table(rand_str(5));

COLUMN_VALUE
----------------------------------------------------------------------
WPLJRTZVHH
LGUITJGZQD
PHYVRBBMFM
WYWQUEMKHP
JVRXAKFLKB



关于DBMS_RANDOM.STRING 在官方文档中有描述
dbms_random.string(opt,len);
opt:   'u', 'U' - Returning string is in uppercase alpha characters.

       'l', 'L' - Returning string is in lowercase alpha characters.

       'a', 'A' - Returning string is in mixed-case alpha characters.

       'x', 'X' - Returning string is in uppercase alpha-numeric characters.

       'p', 'P' - Returning string is in any printable characters.


######################################################################
######################################################################
当opt为a的时候,是大小写混合
create or replace function rand_str(count_str number)
	return str
	authid current_user
  is
   strings str:=str();
   begin 
       strings.extend(count_str);
        for i in 1..count_str
         loop
          strings(i):=dbms_random.string('a',10);
          dbms_output.put_line(strings(i));
         end loop;
    return strings;
   end;
   /

SYS@PROD>select * from table(rand_str(5));

COLUMN_VALUE
----------------------------------------------------------------------------------------------------
FqDDQbhZss
MsoIxMWNlC
xhrQChlIND
FIPNdJZZGi
AlOEmQEFIn


######################################################################
######################################################################
当opt为x的时候,是大写字母和数字混合
create or replace function rand_str(count_str number)
	return str
	authid current_user
  is
   strings str:=str();
   begin 
       strings.extend(count_str);
        for i in 1..count_str
         loop
          strings(i):=dbms_random.string('x',10);
          dbms_output.put_line(strings(i));
         end loop;
    return strings;
   end;
   /
SYS@PROD>select * from table(rand_str(5));

COLUMN_VALUE
----------------------------------------------------------------------------------------------------
G538D5GR8K
LQN53T19N2
WFZZ4PYWF7
D6BCAGBR1S
R38RT4JBCE

######################################################################
######################################################################
当opt为p的时候,是任何可以打印出来的字符混合
create or replace function rand_str(count_str number)
	return str
	authid current_user
  is
   strings str:=str();
   begin 
       strings.extend(count_str);
        for i in 1..count_str
         loop
          strings(i):=dbms_random.string('p',10);
          dbms_output.put_line(strings(i));
         end loop;
    return strings;
   end;
   /

SYS@PROD>select * from table(rand_str(5));

COLUMN_VALUE
----------------------------------------------------------------------------------------------------
E}T-fXq#.k
BozUxu4{v,
ZlAtpeUP=M
)Au}A,i1DK
9`W}Bb~Kfe

猜你喜欢

转载自blog.csdn.net/lv941002/article/details/83047564
今日推荐