bash - Shell Script /dev/urandom -


i want make bash script sends stdout file image containing ascii writable characters. script should receive , accept 1 argument containing number of octets should read /dev/urandom. so, need read given number of octets /dev/urandom create file image send stdout. have this:

!/bin/bash

x=1

if [ $x -eq 0 ] echo "error: argument needed"

else

strings /dev/urandom
echo result

fi

basically i'm checking if there's argument and, if there is, read dev/urandom. of course sketch.

i told there command called strings reads , extracts sequences of characters file, i've checked on internet , can't find info it.

so question is: how read number of octets given in arguments /dev/random can put them in stdout (i know how put on stdout :) )

strings not want. if want random characters restricted particular set, filter out not want. $num alphanumeric chars /dev/urandom, can do:

tr -dc a-za-z0-9 < /dev/urandom | dd bs=$num count=1 2> /dev/null 

or

tr -dc '[:alnum:]' < /dev/urandom ... 

note not strictly portable. although standard tr states should work on input file, version throwing error 'illegal byte sequence'. option is:

perl -pe 's/[^a-za-z0-9]//g' /dev/urandom | dd bs=$num count=1 2> /dev/null 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -