2

I'm trying to create an insult generator for one of my classes and I'm not quite sure how to finish it.

I have the whole thing doing what I want, but I need to convert the external commands I'm using in the script to bash equivalents.

The file I'm reading from has 3 columns of words that together make up a Shakespearean insult.

The code:

while read ; do
    Insult=$(shuf -n 1 | tr "\t" " ")
    echo "thou $Insult"
done < /class/SIG_data

The tr command is in there for readability, as the columns are separated with tabs. I just need to know how to do this without using tr and shuf.

agc
  • 7,973
  • 2
  • 29
  • 50
  • 1
    See [Bash FAQ 0026](http://mywiki.wooledge.org/BashFAQ/026) for replacing `shuf`. Replacing `tr` can be done with [Shell Parameter Expansion](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion) in bash. – Etan Reisner May 05 '15 at 18:09
  • I'm trying to use $RANDOM to get a random number and bash arrays to hold the file. Could you point me to how to do that? –  May 05 '15 at 18:36
  • Unless this script is going to output more than one value over time I'm not sure loading the file into memory is worth it. Just use one of the solutions from [Bash FAQ 0011](http://mywiki.wooledge.org/BashFAQ/011) (which includes a solution for reading a file into an array and link to a different faq for other ways to do that). – Etan Reisner May 05 '15 at 18:40
  • possible duplicate of [How can I shuffle the lines of a text file on the Unix command line or in a shell script?](http://stackoverflow.com/questions/2153882/how-can-i-shuffle-the-lines-of-a-text-file-on-the-unix-command-line-or-in-a-shel) – pasaba por aqui Aug 22 '15 at 18:58
  • Frankly, the performance requirements on this script are minimal and the clarity that using `tr` and `shuf` gives is probably better than any intricate shell scripting to avoid them. Shell scripts run commands — whole programs. Demonstrate there's an actual performance problem before trying to optimize them away. – Jonathan Leffler Aug 26 '16 at 04:59

1 Answers1

0
set -- `cat /class/SIG_data`
( shift $(((RANDOM % $#) + 1)) ; echo thou $1 ; )

Note: set should work, since a Shakespearean wordlist is unlikely to be big enough to be any problem.

agc
  • 7,973
  • 2
  • 29
  • 50