#!/bin/bash

bad=$(( $$ + 1 ))
PID=$(pgrep -x shqueue |grep -v "^$bad$" |grep -vE "^$$$" )
if [ -z "$PID" ]; then

 [ ! -z "$1" ] && echo Main program not running. Please start with no parameters && exit 0

 function exitf {
  rm $FIFO
 }

 trap exitf EXIT

 set -e
 fn=$$
 FIFO=/tmp/sq$fn
 mkfifo $FIFO
 set +e

 echo To submit commands, run this again from elsewhere.
 while true; do
   cat $FIFO | tee /dev/stderr | bash
   #echo command done.
 done

else

 [ ! -z "$1" ] && pre="${*@Q} " || pre=""
 [ ! -z "$1" ] && ppre="$* " || pre=""

 FI=/tmp/sq$PID
 [ ! -p "$FI" ] && echo not a pipe, close all shqueue instances and start again. && exit
 echo Enter bash commands one per line. ^D to finish
 while read -p "$ppre" -r l; do
   echo "${pre}${l}" > $FI
 done
fi

