#!/usr/bin/env -S bash name='main' addr='d' myapp='backsshbash2' bye() { printf "${@:2}" exit "$1" } start() { [[ -p $i ]] || { kill -9 $( <"$p" ) $( <"$pp" ) ; rm -f "${a[@]}" ; } mkfifo "$i" "$i1" "$i2" "$o" while read -r -N 2 r ; do if [[ $r == c* ]] ; then printf %s "${r:1:1}" else printf %s "${r:1:1}" >&2 ; fi done <"$i1" >"$i" 2> >( while read -r -N 1 r ; do if [[ $r == e ]] ; then kill -0 $( <"$p" ) else kill -9 $( <"$p" ) $( <"$pp" ) ; rm -f "${a[@]}" ; fi done ) & printf $! >"$pp" bash -i >"$o" 2>&1 <"$i" & printf $! >"$p" <"$o" >"$i1" ssh "$addr" bash --init-file <( printf %s "$( declare -p z )"$' [[ -d $z ]] || mkdir -p "$z" cd "$z" || exit 1 [[ -p in || -p out ]] && rm -f {in,out} mkfifo -p {in,out} while cat in ; do sleep .15 ; done & while cat >out ; do sleep .15 ; done' ) } connect() { ssh "$addr" bash --init-file <( printf %s "$( declare -p z )"$' cd "$z" || exit 1 [[ -p in && -p out ]] && exit 1 while cat out ; do sleep .15 ; done & while cat >in ; do sleep .15 ; done' ) < <( while read -r -N 1 r ; do printf c%s "$r" ; done ) } help() { printf %s\\n $'simple args help possible args are name | session name addr | ssh addr str start | start bash and ssh to transit connect | connect to session in transit' exit 0 } [[ $BASH_SOURCE == */* ]] && name=${name%/*} name=${name##*/} || name=${PWD##*/} myd=~/."$myapp" z=$myd/$name \ i=$z/i i1=$z/1 i2=$z/2 o=$z/o p=$z/p pp=$p. \ a=( "$i" "$i1" "$i2" "$o" "$p" "$pp" ) [[ -d $z ]] || mkdir -p "$z" cd "$z" || bye 1 'cant cd to safety dir %s\n' "$z" declare -A \ supported0=( help '' start '' stop '' connect '' ) supported1=( name '' addr '' ) (( $# )) || set -- help fill=( '' "$@" ) i= while [[ -v fill[++i] ]] && arg=${fill[i]} ; do if [[ -v supported0[\$arg] ]] ; then "$arg" elif [[ -v supported1[\$arg] ]] ; then declare "$arg=${fill[++i]}" else printf $'unsupported arg %s, please consult running with \'help\' arg\n' "$arg" ; fi done