{"id":52,"date":"2010-03-20T21:16:02","date_gmt":"2010-03-21T04:16:02","guid":{"rendered":"http:\/\/www.boxheap.net\/ddaniels\/blog\/?p=52"},"modified":"2010-03-20T21:20:34","modified_gmt":"2010-03-21T04:20:34","slug":"random-words-definition-shell-script","status":"publish","type":"post","link":"http:\/\/www.boxheap.net\/ddaniels\/blog\/2010\/03\/random-words-definition-shell-script\/","title":{"rendered":"Random word&#8217;s definition shell script"},"content":{"rendered":"<p><code>dict \"$(head -n $(($(od -N 2 \/dev\/urandom|cut -d' ' -f2 -s)%98326)) \/usr\/share\/dict\/british-english| tail -n1)\" |less<\/code><\/p>\n<p>More people may prefer:<\/p>\n<p><code>dict \"$(head -n $(($(od -N 2 \/dev\/urandom|cut -d' ' -f2 -s)%$(wc -l \/usr\/share\/dict\/words|cut -d' ' -f1))) \/usr\/share\/dict\/words| tail -n1)\"|more<\/code><\/p>\n<ul>\n<li>I still want to check for bashisms<\/li>\n<li>98326 is the output of <code>wc -l \/usr\/share\/dict\/british-english<\/code>. I put it inline for speed, and didn&#8217;t bother with a variable since I wanted a one line script.<\/li>\n<li>If the dictionary is too big then a larger random number would be needed.<\/li>\n<li>\/usr\/share\/dict\/british-english isn&#8217;t installed on many systems, but words is.<\/li>\n<li>&#8220;more&#8221; is lighter weight than &#8220;less&#8221;, and is installed on more systems. It lacks the ability to go backwards.<\/li>\n<\/ul>\n<p>To get a random number I used:<\/p>\n<p><code>$(od -N 2 \/dev\/urandom|cut -d' ' -f2 -s)<\/code><\/p>\n<ul>\n<li>od converts to decimal.<\/li>\n<li>-N 2 gets two bytes<\/li>\n<li>\/dev\/urandom is pseudorandom bytes from the kernel. There might be a more cross platform alternative like maybe $RANDOM for bash.<\/li>\n<li><code>cut -d' ' -f2 -s<\/code> gets only the second column. Often <code>awk '{print $2}'<\/code> is used instead. Awk can be very big. gawk is said to be big, and mawk is said to be minimal. cut seems more portable and smaller yet to me. -d sets the delimiter, -f2 is field two, -s is only print lines with the delimiter.<\/li>\n<\/ul>\n<p>Try &#8220;set -x&#8221; before the command to see the different levels of shell script in the one line, do &#8220;set +x&#8221; after to get things back to normal.<\/p>\n<p>Drew Daniels&#8217; resume: <a title=\"http:\/\/www.boxheap.net\/ddaniels\/resume.html\" href=\"http:\/\/www.boxheap.net\/ddaniels\/resume.html\">http:\/\/www.boxheap.net\/ddaniels\/resume.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>dict &#8220;$(head -n $(($(od -N 2 \/dev\/urandom|cut -d&#8217; &#8216; -f2 -s)%98326)) \/usr\/share\/dict\/british-english| tail -n1)&#8221; |less<\/p>\n<p>More people may prefer:<\/p>\n<p>dict &#8220;$(head -n $(($(od -N 2 \/dev\/urandom|cut -d&#8217; &#8216; -f2 -s)%$(wc -l \/usr\/share\/dict\/words|cut -d&#8217; &#8216; -f1))) \/usr\/share\/dict\/words| tail -n1)&#8221;|more<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":4,"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":56,"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions\/56"}],"wp:attachment":[{"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.boxheap.net\/ddaniels\/blog\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}