How to write a Drush script to retrieve data?
You can start checking the code of the Drush Entity module.


  With drush entity you can query the entity system available for Drupal
  8, 7 and (some of) 6.


And as Greg says you can use the Drushify module to start. 

In this links you can find good references:


How to Create Your Own Drush Command
How to write drush commands and hooks
Drush Integration for your modules