Last active
          April 13, 2016 14:56 
        
      - 
      
 - 
        
Save andrewh/1385770 to your computer and use it in GitHub Desktop.  
    Prints the distribution of primes in the terminal using OpenSSL
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/sh | |
| die () { | |
| echo $1 | |
| exit $2 | |
| } | |
| red () { | |
| if [ -x $(which tput) ]; then | |
| tput setaf 1 | |
| fi | |
| } | |
| blue () { | |
| if [ -x $(which tput) ]; then | |
| tput setaf 4 | |
| fi | |
| } | |
| yellow () { | |
| if [ -x $(which tput) ]; then | |
| tput setaf 3 | |
| fi | |
| } | |
| term_reset () { | |
| if [ -x $(which tput) ]; then | |
| tput sgr0 | |
| fi | |
| } | |
| check_openssl () { | |
| openssl=$(which openssl) | |
| test -x $openssl || die "can't find openssl - is it installed?" 1 | |
| } | |
| status () { | |
| yellow | |
| printf "[$(basename $0)] " | |
| term_reset | |
| echo "${@}" | |
| } | |
| hide_cursor () { | |
| tput civis | |
| } | |
| show_cursor () { | |
| tput cnorm | |
| } | |
| main () { | |
| check_openssl | |
| status "print the distribution of primes using openssl!" | |
| echo | |
| status "enter the upper limit to count to -" | |
| read ulimit | |
| echo | |
| red | |
| printf "|" | |
| term_reset | |
| echo " = prime" | |
| blue | |
| printf "." | |
| term_reset | |
| echo " = composite" | |
| echo | |
| trap echo EXIT | |
| hide_cursor | |
| for num in $(seq 1 ${ulimit:-1000}); | |
| do | |
| $openssl prime $num | grep --quiet 'is prime' 2>&1; | |
| case $? in | |
| 0) red; /bin/echo -n '|'; term_reset ;; | |
| *) blue; /bin/echo -n '.'; term_reset ;; | |
| esac | |
| done | |
| show_cursor | |
| } | |
| main | 
      
      
  Author
  
  
      
          
      
      
            andrewh
  
      
      
      commented 
        Apr 13, 2016 
      
    
  
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment