Skip to content

Instantly share code, notes, and snippets.

@darkone23
Created January 31, 2013 22:21
Show Gist options
  • Select an option

  • Save darkone23/4687174 to your computer and use it in GitHub Desktop.

Select an option

Save darkone23/4687174 to your computer and use it in GitHub Desktop.
get a random cow for use in `cowsay -f cow $msg`
#!/usr/bin/env python
import subprocess
from random import randint
COWSAY = "/usr/games/cowsay"
def get_randomth(arr):
return arr[randint(0, len(arr) - 1)]
def get_critters(cowsay):
(critters, err) = subprocess.Popen([cowsay, '-l'], stdout=subprocess.PIPE).communicate()
if critters:
animal_lines = critters.split('\n')[1:-1] # edges of stdout are garbage
return [animal for animals in animal_lines for animal in animals.split(' ')]
if __name__ == "__main__":
available_critters = get_critters(COWSAY)
print get_randomth(available_critters)
# corutils version:
# echo `/usr/games/cowsay -l | awk 'NR>1{ print $0 }' | xargs shuf -e | head -n 1`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment