0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シェルで大きさを指定してランダムな文字列を生成する

Last updated at Posted at 2017-11-10
$ dd if=/dev/urandom of=/dev/stdout bs=1k count=600 2>/dev/null | base64 | tr -d '\n'
  • 単にランダムなデータのストリームは/dev/randomからddすれば取得できる
  • ASCII文字に変換するためにbase64エンコードすることにした
  • base64エンコードで133%の大きさになるので、目標サイズの100/133の大きさのデータが出てくるようにddのbscountを設定する
  • 以下の例だと目標サイズは800kbyte(819200byte)なので、ddで書き出すサイズは600kbyte
  • tr -d '\n'でbase64の改行を削除する
  • base64はPOSIXコマンドではないが、オプションなしだとGNUとBSDの違いは気にならないので使うことにした

wcで生成された文字列のサイズを確認してみる

$ dd if=/dev/urandom of=/dev/stdout bs=1k count=600 2>/dev/null | base64 | tr -d '\n' | wc -c
819200
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?