How to find specific shortcodes in a post or in a widget and pass them to a variable?

I’m a little unclear from the question what exactly you’re trying to do. Is there multiple shortcodes per page? Are you on the page when you need to get the book id?

If you’re on the page with the shortcode, then this function would return an array of book id’s:

function book_id() {
    return preg_match_all( '/(?<=\[book id=").+?(?="])/', get_post()->post_content, $match )[0];
}

If post content was [book id="432"][book id="434"] this would return:

array(2) {
  [0]=>
  string(3) "432"
  [1]=>
  string(3) "434"
}

If there is only one book id per page, you could use preg_match rather than preg_match_all and the function above would return a string rather than an array.