title: Read output of dd into a shell script variable
body: <p>Being very new to shell scripts, I have pieced together the following to search <code>/dev/sdd1</code>, sector by sector, to find a string. How do I get the sector data into the <code>$HAYSTACK</code> variable?</p>
  
  <pre><code>#!/bin/bash
  
  HAYSTACK=""
  START_SEARCH=$1
  NEEDLE=$2
  START_SECTOR=2048
  END_SECTOR=226512895+1
  SECTOR_NUMBER=$((START_SEARCH + START_SECTOR))
  while [  $SECTOR_NUMBER -lt $END_SECTOR ]; do
      $HAYSTACK=`dd if=/dev/sdd1 skip=$SECTOR_NUMBER count=1 bs=512`
      if [[ "$HAYSTACK" =~ "$NEEDLE" ]]; then
          echo "Match found at sector $SECTOR_NUMBER"
          break
      fi
      let SECTOR_NUMBER=SECTOR_NUMBER+1 
  done
  </code></pre>
  
  <p><strong>Update</strong>  </p>
  
  <p>The intention is not to make a perfect script to handle fragmented file scenarios (I doubt that is possible at all).  </p>
  
  <p>In my case not being able to distinguish stings with nulls is also a non-issue.</p>
  
  <p>If you could expand the pipe suggestions into an answer it would be more than enough. Thanks!</p>
  
  <p><strong>Background</strong></p>
  
  <p>I have managed to wipe my www folder and have been trying to recover as much of my source files as possible. I have used <a href="https://github.com/sleuthkit/scalpel" rel="nofollow noreferrer">Scalpel</a> to recover my php and html files. But the version I could get working on my Ubuntu 16.04 is <a href="https://pkgs.org/download/scalpel" rel="nofollow noreferrer">Version 1.60</a> which does not support regex in header/footer so I cannot make a good pattern for css, js, and json files.</p>
  
  <p>I remember fairly rare strings to search for and find my files, but have no idea where in a block the string could be. The solution I came up with is this shell script to read blocks from the partition and look for the substring and if a match is found print out the LSB number and exit. </p>