Skip to content

Instantly share code, notes, and snippets.

@ozuma
Created November 30, 2021 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozuma/cc5e933b7dbbfc5f9186930ae0d229ec to your computer and use it in GitHub Desktop.
Save ozuma/cc5e933b7dbbfc5f9186930ae0d229ec to your computer and use it in GitHub Desktop.
シェルスクリプトでランダム値を生成する

乱数は擬似乱数で十分なので、seedは /dev/urandom を用いる。

  • 文字種はtrで制御
  • 文字数はfoldコマンドで
  • 行数はheadで
$ cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 8 
Qz5D4Ddy0CUTk7mU
FLbxZE5pls5tLCaJ
nAy4ApBXZGGEX0qQ
q9HRZk9panIlwS4V
C0YUOnYaA7CSwCil
BZ8KdSfs8sCKcYrr
ZDQTPabwm2c4m06D
dhHKEwCcL3JbyaKJ

大文字だけ欲しい場合は、 tr -dc 'A-Z0-9' のように調節する。

$ cat /dev/urandom | tr -dc 'A-Z0-9' | fold -w 16 | head -n 8 
04ES0JNSY6YFWWPR
EXT2R89QY5PZRISD
LA455ECHNW0ZH4H4
VVJ20UIXN7H8Y0W6
1M1JSGJOHB8IP0SN
F6ZHYDBX70THR1R9
67LJQHUXK31H0OKF
SIGHPVOQVORPAKDO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment