1. 程式人生 > >Linux和Oracle生成隨機字串

Linux和Oracle生成隨機字串

Linux 和Oracle裡面如何生成隨機字串:
Linux 可以利用uuidgen來生成
[[email protected] ~]$ 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
不帶引數的
[
[email protected]
shellscript]# sh random_string.sh debdbbjeba iehicahgca fjdcceaihb jfcjededfa cbhijeefcj jabeaeicja dagbeadejc ifcefaagag fjhjigacdb faeabdbabh [[email protected] 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));
[email protected]
>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; /
[email protected]
>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; / [email protected]>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; / [email protected]>select * from table(rand_str(5)); COLUMN_VALUE ---------------------------------------------------------------------------------------------------- E}T-fXq#.k BozUxu4{v, ZlAtpeUP=M )Au}A,i1DK 9`W}Bb~Kfe