title: Bash script output not going to stdout
body: <p>I have a build process, kicked off by <code>Make</code>, that executes a lot of child scripts.</p>
  
  <p>A couple of these child scripts require <code>root privileges</code>, so instead of running everything as <code>root</code>, or everything as <code>sudo</code>, I'm trying to only execute the scripts that need to be as <code>root</code>, as <code>root</code>.</p>
  
  <p>I'm accomplishing this like so:</p>
  
  <pre><code>execute_as_user() {
  
      su "$1" -s /bin/bash -c "$2;exit \$?"
  
  }
  </code></pre>
  
  <p>Arg $1 is the user to run the script as, arg $2 is the script.</p>
  
  <p>Arg $1 is either <code>root</code> (gotten with: <code>$(whoami)</code> since everything is under sudo), or the current user's account (gotten with: <code>$(logname)</code>)</p>
  
  <p>The entire build is kicked off as:</p>
  
  <pre><code>sudo make all
  </code></pre>
  
  <p>Sample from the Makefile:</p>
  
  <pre><code>LOG="runtime.log"
  ROTATE_LOG:=$(shell bash ./scripts/utils/rotate_log.sh)
  
  system:
      /bin/bash -c "time ./scripts/system.sh 2&gt;&amp;1 | tee ${LOG}"
  </code></pre>
  
  <p>My problem is... none of the child scripts are printing output to <code>stdout</code>. I believe it to be some sort of issue with an almost recursive call of <code>su root</code>... but I'm unsure. From my understanding, these scripts should already be outputting to <code>stdout</code>, so perhaps I'm mistaken where the output is going?</p>
  
  <p>To be clear, I'm seeing no output in either the logfile nor displaying to the terminal (stdout).</p>
  
  <p><code>Updating for clarity:</code></p>
  
  <p>Previously, I just ran all the scripts either with <code>sudo</code> or just as the logged in user... which with my makefile above, would print to the terminal (<code>stdout</code>) and logfile. Adding the <code>execute_as_user()</code> function is where the issue cropped up. The scripts execute and build the project... just no display "that it's working" and no logs.</p>
  
  <p><code>UPDATE</code></p>
  
  <p>Here is some snippets:</p>
  
  <p>system.sh snippet:</p>
  
  <pre><code>execute_script() {
      echo "Executing as user $3: $2"
      RETURN=$(execute_as_user $3 ${SYSTEM_SCRIPTS}/$2)
      if [ ${RETURN} -ne ${OK} ]
      then
          error $1 $2 ${RETURN}
      fi
  }
  
  build_package() {
      local RETURN=0
      case "$1" in
          system)
              declare -a scripts=(\
                  "rootfs.sh" \
                  "base_files.sh" \
                  "busybox.sh" \
                  "iana-etc.sh" \
                  "kernel.sh" \
                  "firmware.sh" \
                  "bootscripts.sh" \
                  "network.sh" \
                  "dropbear.sh" \
                  "wireless_tools.sh" \
                  "e2fsprogs.sh" \
                  "shared_libs.sh"
              )
  
              for SCRIPT_NAME in "${scripts[@]}"; do
                  execute_script $1 ${SCRIPT_NAME} $(logname)
                  echo ""
                  echo -n "${SCRIPT_NAME}"
                  show_status ${OK}
                  echo ""
              done
  
              # finalize base system
              echo ""
              echo "Finalizing base system"
              execute_script $1 "finalize.sh" $(whoami)
              echo ""
              echo -n "finalize.sh"
              show_status ${OK}
              echo ""
  
              # package into tarball
              echo ""
              echo "Packing base system"
              execute_script $1 "archive.sh" $(whoami)
              echo ""
              echo -n "archive.sh"
              show_status ${OK}
              echo ""
  
              echo ""
              echo -n "Build System: "
              show_status ${OK}
              ;;
          *)
              echo "$1 is not supported!"
              exit 1
      esac
  }
  </code></pre>
  
  <p>sample child script executed by system.sh</p>
  
  <pre><code>cd ${CLFS_SOURCES}/
  tar -xvjf ${PKG_NAME}-${PKG_VERSION}.tar.bz2
  
  cd ${CLFS_SOURCES}/${PKG_NAME}-${PKG_VERSION}/
  
  make distclean
  RESPONSE=$?
  if [ ${RESPONSE} -ne 0 ]
  then
      pkg_error ${RESPONSE}
      exit ${RESPONSE}
  fi
  
  ARCH="${CLFS_ARCH}" make defconfig
  RESPONSE=$?
  if [ ${RESPONSE} -ne 0 ]
  then
      pkg_error ${RESPONSE}
      exit ${RESPONSE}
  fi
  
  # fixup some bugs with musl-libc
  sed -i 's/\(CONFIG_\)\(.*\)\(INETD\)\(.*\)=y/# \1\2\3\4 is not set/g' .config
  sed -i 's/\(CONFIG_IFPLUGD\)=y/# \1 is not set/' .config
  
  etc...
  </code></pre>
  
  <p>Here's the entire system.sh script:</p>
  
  <p><a href="https://github.com/SnakeDoc/LiLi/blob/master/scripts/system.sh" rel="nofollow">https://github.com/SnakeDoc/LiLi/blob/master/scripts/system.sh</a></p>
  
  <p>(i know the project is messy... it's a learn-as-you-go style project)</p>