title: Dpkg-reconfigure in BASH
body: <p>I've recently done a script that reconfigures all packages on a Debian Stable (7.3) system. I'm using the command </p>
  
  <pre><code>    dpkg-reconfigure -pcritical -a --force
  </code></pre>
  
  <p>Everything worked fine, but today I made some changes to the code (I don't think they are related) and now I'm getting this error:</p>
  
  <pre><code>Can't exec "dpkg-query": Not a directory at /usr/sbin/dpkg-reconfigure line 98.
  Can't exec "dpkg": Not a directory at /usr/sbin/dpkg-reconfigure line 82.
  Use of uninitialized value $_ in pattern match (m//) at /usr/sbin/dpkg-reconfigure line 83.
  Use of uninitialized value $_ in pattern match (m//) at /usr/sbin/dpkg-reconfigure line 84.
  Use of uninitialized value $_ in pattern match (m//) at /usr/sbin/dpkg-reconfigure line 85.
  Use of uninitialized value $_ in pattern match (m//) at /usr/sbin/dpkg-reconfigure line 86.
  Can't exec "dpkg-query": Not a directory at /usr/sbin/dpkg-reconfigure line 98.
  Can't exec "dpkg": Not a directory at /usr/sbin/dpkg-reconfigure line 82.
  Use of uninitialized value $_ in pattern match (m//) at /usr/sbin/dpkg-reconfigure line 83.
  Use of uninitialized value $_ in pattern match (m//) at /usr/sbin/dpkg-reconfigure line 84.
  Use of uninitialized value $_ in pattern match (m//) at /usr/sbin/dpkg-reconfigure line 85.
  Use of uninitialized value $_ in pattern match (m//) at /usr/sbin/dpkg-reconfigure line 86.
  Can't exec "dpkg-query": Not a directory at /usr/sbin/dpkg-reconfigure line 98.
  Can't exec "dpkg-query": Not a directory at /usr/sbin/dpkg-reconfigure line 202.
  readline() on closed filehandle QUERY at /usr/sbin/dpkg-reconfigure line 204.
  </code></pre>
  
  <p>There are thousands of lines full of similar lines (these are the last ones).
  The system is running in runlevel 1 - single-user - and as user root. And another interesting thing is that dpkg exit code is still 0.</p>
  
  <p>Thank you for your help in advance.</p>
  
  <p>EDIT: here's the script</p>
  
  <pre><code>#!/bin/bash
  DPKG_RECONFIG_PATH=/usr/sbin/dpkg-reconfigure
  # Function for checking critical executables (exits on error)
  chk_crit ()
  {
  TARGET=$1
  PATH=$2
  if [ -e $PATH ] # Pass 1 - checking existence
  then printf "$TARGET exists\n"
  else
      printf "$TARGET doesn't exist!\n"
      exit
  fi
  if [ -x $PATH ] # Pass 2 - checking execution permissions
  then
      printf "$TARGET is executable\n"
  else
      printf "$TARGET is not executable, attempting chmod!\n"
      chmod 755 $TARGET_PATH
      CHMOD_EXIT=$?
      if [ $CHMOD_EXIT = 0 ] # Checking if chmod has succeeded
      then
          printf "chmod succeeded\n"
  else
          printf "chmod failed, $TARGET is unusable!\n"
          exit
      fi
  fi
  }
  
  chk_crit dpkg-reconfigure $DPKG_RECONFIG_PATH
  
  # Reconfiguring all packages, only asking critical questions
  $DPKG_RECONFIG_PATH -pcritical -a --force
  DPKG_EXIT=$?
  if [ $DPKG_EXIT = 0 ]
  then printf "Reconfiguration succeeded\n"
  else printf "Reconfiguration failed\n"
  exit $DPKG_EXIT
  fi
  exit
  </code></pre>