Advertisement
PackRat-2017

taoread.sh

Apr 22nd, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #!/bin/sh
  2. # a script for outputting a verse of the Tao Te Ching.
  3.  
  4. #tao=$HOME/bin/tao.txt
  5. tao=$HOME/conky/tao.txt
  6. verse=0
  7. # you can chose between /dev/random which is slow but provides ``more random''
  8. # numbers than the faster /dev/urandom
  9. dev_rand=/dev/urandom
  10. #dev_rand=/dev/random
  11.  
  12. # if we have no arguments we pick a random verse
  13. if [ $# -eq 0 ]; then
  14. # find a random number between 1 and 81 and save it to verse
  15. while [ $verse -lt 1 ] || [ $verse -gt 81 ]; do
  16. # take one byte (-n1) from /dev/[u]random and save its decimal value to verse (-e '"%1d"') (see "man hexdump")
  17. verse=`hexdump -e '"%1d"' -n1 $dev_rand`
  18. done
  19. # if we have exactly one argument we pick that verse
  20. elif [ $# -eq 1 ]; then
  21. # the verse must be a number between 1 and 81
  22. if [ $1 -ge 1 ] && [ $1 -le 81 ]; then
  23. verse=$1
  24. else
  25. echo "The Tao Te Ching only has 81 verses." >&2
  26. echo "If you require even more wisdom, sit down and think of nothing." >&2
  27. exit 1
  28. fi
  29. # we got the wrong number of arguments
  30. else
  31. echo "Too many arguments." >&2
  32. echo "Use 'tao' for a random verse, or 'tao [1-81]' for a specific verse." >&2
  33. exit 1
  34. fi
  35.  
  36. stop=$(($verse+1))
  37. # look into the text file and search for the verse number
  38. sed -n -e '/^---'$verse'---$/,${/^---'$stop'---$/q;p;}' $tao
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement