Skip to content

Instantly share code, notes, and snippets.

@rcmdnk rcmdnk/echo_test.sh

Last active Jan 4, 2016
Embed
What would you like to do?
memo & scripts for read test
#!/usr/bin/env bash
subsub () {
echo subsub start >/dev/tty
echo ' 'tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty
trap "
echo subsub killed: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty;
return 1
" 1 2 3 15
#trap "[ \"$(tty)\" != \"not a tty\" ] && stty echo;echo subsub' 'exit: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty" 0
#trap " [ $BASHPID = $$ ] && stty echo;echo subsub' 'exit: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty; exit" 0
#trap " stty echo;echo subsub' 'exit: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty; exit" 0
echo "read in subsub: " >/dev/tty
read -s -n 1 c </dev/tty
echo "subsub input:$c" >/dev/tty
}
sub () {
echo sub start >/dev/tty
echo ' 'tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty
# trap "
# echo sub' 'killed: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty;
# return 1
# " 1 2 3 15
# trap "echo sub' 'exit: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty>/dev/tty" 0
#echo "" >/dev/tty
#echo "=============================" >/dev/tty
#echo "read in sub: " >/dev/tty
#read -s -n 1 c </dev/tty
#echo "sub input:$c" >/dev/tty
#echo "" >/dev/tty
#echo "=============================" >/dev/tty
#echo "Call subsub in \$()" >/dev/tty
#echo "$(subsub)" >/dev/tty
#echo "" >/dev/tty
echo "=============================" >/dev/tty
echo "Call subsub in pipe" >/dev/tty
echo pipe|subsub >/dev/tty
echo "" >/dev/tty
#echo "=============================" >/dev/tty
#echo "Call subsub in \$()+pipe" >/dev/tty
#echo "$(echo hoge|subsub)" >/dev/tty
#echo "" >/dev/tty
#echo "=============================" >/dev/tty
#echo "Call subsub in pipe+\$()" >/dev/tty
#echo "hoge" |$(subsub) >/dev/tty
#echo "" >/dev/tty
#echo "=============================" >/dev/tty
#echo "Call subsub directly" >/dev/tty
#subsub >/dev/tty
#echo "" >/dev/tty
#echo "=============================" >/dev/tty
echo sub end >/dev/tty
}
#echo parent start
#echo ' 'tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID
#trap "
# echo parent killed: BASHPID=$BASHPID >/dev/tty;
# return 1
# " 1 2 3 15
#trap "echo parent' 'exit: BASHPID=$BASHPID >/dev/tty" 0
echo $(sub)
echo parent end
# if C-c is called in the function:
## Direct call
C-c is given only to function, and return.
In the parent function/shell, process is proceeded as usual.
## Subshell $()
C-c is given to function, and also exit.
It is also given to parent shell, and exit (or return if parent is function).
## Pipe
C-c is given only to function and return.
It is also given to parent shell, and exit (or return if parent is function).
## Subshell+pipe $(echo hoge|func)
C-c is given only to function and return.
It is also given to parent shell, and exit (or return if parent is function).
## pipe+subshell echo hoge|$(func)
C-c is given to function, and also exit.
It is also given to parent shell, and exit (or return if parent is function).
********************************************************
(*_*) $ ./read_test.sh
parent start
tty=/dev/ttys000, PPID=54841, PID=82861, BASHPID=82861
sub start
tty=/dev/ttys000, PPID=54841, PID=82861, BASHPID=82861
=============================
read in sub:
sub input:a
=============================
Call subsub in $()
subsub start
tty=/dev/ttys000, PPID=54841, PID=82861, BASHPID=83008
read in subsub:
subsub input:a
subsub exit: tty=/dev/ttys000, PPID=54841, PID=82861, BASHPID=83008
=============================
Call subsub in pipe
subsub start
tty=not a tty, PPID=54841, PID=82861, BASHPID=83016
read in subsub:
subsub input:a
=============================
Call subsub in $()+pipe
subsub start
tty=not a tty, PPID=54841, PID=82861, BASHPID=83022
read in subsub:
subsub input:a
=============================
Call subsub directly
subsub start
tty=/dev/ttys000, PPID=54841, PID=82861, BASHPID=82861
read in subsub:
subsub input:a
=============================
sub end
parent end
subsub exit: tty=/dev/ttys000, PPID=54841, PID=82861, BASHPID=82861
(-_-) $
(^_-) $
(-_-) $
(-_-) $
(-_-) $
(-_-) $ ./read_test.sh
parent start
tty=/dev/ttys000, PPID=54841, PID=83319, BASHPID=83319
sub start
tty=/dev/ttys000, PPID=54841, PID=83319, BASHPID=83319
=============================
read in sub:
sub killed: tty=/dev/ttys000, PPID=54841, PID=83319, BASHPID=83319
parent end
sub exit: tty=/dev/ttys000, PPID=54841, PID=83319, BASHPID=83319
(^_^) $
(-_-) $
(^_^) $
(-_-) $
(-_-) $
(-_-) $ ./read_test.sh
parent start
tty=/dev/ttys000, PPID=54841, PID=83470, BASHPID=83470
sub start
tty=/dev/ttys000, PPID=54841, PID=83470, BASHPID=83470
=============================
read in sub:
sub input:a
=============================
Call subsub in $()
subsub start
tty=/dev/ttys000, PPID=54841, PID=83470, BASHPID=83475
read in subsub:
subsub killed: tty=/dev/ttys000, PPID=54841, PID=83470, BASHPID=83475
subsub exit: tty=/dev/ttys000, PPID=54841, PID=83470, BASHPID=83475
sub killed: tty=/dev/ttys000, PPID=54841, PID=83470, BASHPID=83470
parent end
sub exit: tty=/dev/ttys000, PPID=54841, PID=83470, BASHPID=83470
(-_-) $
(-_-) $
(-_-) $
(-_-) $ ./read_test.sh
parent start
tty=/dev/ttys000, PPID=54841, PID=83626, BASHPID=83626
sub start
tty=/dev/ttys000, PPID=54841, PID=83626, BASHPID=83626
=============================
read in sub:
sub input:a
=============================
Call subsub in $()
subsub start
tty=/dev/ttys000, PPID=54841, PID=83626, BASHPID=83768
read in subsub:
subsub input:a
subsub exit: tty=/dev/ttys000, PPID=54841, PID=83626, BASHPID=83768
=============================
Call subsub in pipe
subsub start
tty=not a tty, PPID=54841, PID=83626, BASHPID=83776
read in subsub:
subsub killed: tty=not a tty, PPID=54841, PID=83626, BASHPID=83776
sub killed: tty=/dev/ttys000, PPID=54841, PID=83626, BASHPID=83626
parent end
sub exit: tty=/dev/ttys000, PPID=54841, PID=83626, BASHPID=83626
(-_-) $ (-_-) $ (-_-) $ (-_-) $
(-_-) $
(-_-) $
(-_-) $
(-_-) $ ./read_test.sh
parent start
tty=/dev/ttys000, PPID=54841, PID=84216, BASHPID=84216
sub start
tty=/dev/ttys000, PPID=54841, PID=84216, BASHPID=84216
=============================
read in sub:
sub input:a
=============================
Call subsub in $()
subsub start
tty=/dev/ttys000, PPID=54841, PID=84216, BASHPID=84221
read in subsub:
subsub input:a
subsub exit: tty=/dev/ttys000, PPID=54841, PID=84216, BASHPID=84221
=============================
Call subsub in pipe
subsub start
tty=not a tty, PPID=54841, PID=84216, BASHPID=84229
read in subsub:
subsub input:a
=============================
Call subsub in $()+pipe
subsub start
tty=not a tty, PPID=54841, PID=84216, BASHPID=84235
read in subsub:
subsub killed: tty=not a tty, PPID=54841, PID=84216, BASHPID=84235
sub killed: tty=/dev/ttys000, PPID=54841, PID=84216, BASHPID=84216
parent end
sub exit: tty=/dev/ttys000, PPID=54841, PID=84216, BASHPID=84216
(-_-) $ (-_-) $ (-_-) $ (-_-) $
(^_^) $
(-_-) $
(-_-) $
(-_-) $ ./read_test.sh
parent start
tty=/dev/ttys000, PPID=54841, PID=84528, BASHPID=84528
sub start
tty=/dev/ttys000, PPID=54841, PID=84528, BASHPID=84528
=============================
read in sub:
sub input:a
=============================
Call subsub in $()
subsub start
tty=/dev/ttys000, PPID=54841, PID=84528, BASHPID=84533
read in subsub:
subsub input:a
subsub exit: tty=/dev/ttys000, PPID=54841, PID=84528, BASHPID=84533
=============================
Call subsub in pipe
subsub start
tty=not a tty, PPID=54841, PID=84528, BASHPID=84541
read in subsub:
subsub input:a
=============================
Call subsub in $()+pipe
subsub start
tty=not a tty, PPID=54841, PID=84528, BASHPID=84547
read in subsub:
subsub input:a
=============================
Call subsub directly
subsub start
tty=/dev/ttys000, PPID=54841, PID=84528, BASHPID=84528
read in subsub:
subsub killed: tty=/dev/ttys000, PPID=54841, PID=84528, BASHPID=84528
=============================
sub end
parent end
subsub exit: tty=/dev/ttys000, PPID=54841, PID=84528, BASHPID=84528
(-_-) $
(-_-) $
(.^.) $
(-_-) $
(-_-) $
(-_-) $ ./read_test.sh
parent start
tty=/dev/ttys000, PPID=54841, PID=84713, BASHPID=84713
sub start
tty=/dev/ttys000, PPID=54841, PID=84713, BASHPID=84713
=============================
read in sub:
sub input:a
=============================
Call subsub in $()
subsub start
tty=/dev/ttys000, PPID=54841, PID=84713, BASHPID=84718
read in subsub:
subsub input:a
subsub exit: tty=/dev/ttys000, PPID=54841, PID=84713, BASHPID=84718
=============================
Call subsub in pipe
subsub start
tty=not a tty, PPID=54841, PID=84713, BASHPID=84726
read in subsub:
subsub input:a
=============================
Call subsub in $()+pipe
subsub start
tty=not a tty, PPID=54841, PID=84713, BASHPID=84732
read in subsub:
subsub input:a
=============================
Call subsub directly
subsub start
tty=/dev/ttys000, PPID=54841, PID=84713, BASHPID=84713
read in subsub:
subsub input:a
=============================
sub end
parent end
subsub exit: tty=/dev/ttys000, PPID=54841, PID=84713, BASHPID=84713
(-_-) $
=============================
Call subsub in pipe+$()
subsub start
tty=not a tty, PPID=55026, PID=52520, BASHPID=52688
read in subsub:
subsub killed: tty=not a tty, PPID=55026, PID=52520, BASHPID=52688
subsub exit: tty=not a tty, PPID=55026, PID=52520, BASHPID=52688
sub killed: tty=/dev/ttys001, PPID=55026, PID=52520, BASHPID=52520
0
parent end
sub exit: tty=/dev/ttys001, PPID=55026, PID=52520, BASHPID=52520
(^_-) $
*********************************************************
# ssty echo:
## if trap is defined in all:
stty echo is disabled at pipe, pipe|$()
## No traps
stty echo is disabled at pipe and pipe|$()
(they don't send kill to sub)
## Only in subsub
stty echo is disabled at Only pipe ( $(pipe) is fine, pipe|$() even doesn't send kill to sub)
## Only in parent
stty echo is disabled at pipe, $(pipe) pipe|$()
# if `stty echo` is defined in trap 0 in subsub:
Call subsub in $()
subsub start
tty=/dev/ttys001, PPID=55026, PID=93744, BASHPID=93746
read in subsub:
subsub killed: tty=/dev/ttys001, PPID=55026, PID=93744, BASHPID=93746
stty: tcsetattr: Input/output error
(*_*) $ subsub exit: tty=/dev/ttys001, PPID=55026, PID=93744, BASHPID=93746
(T_T) $
## pipe case
exit trap is enabled at parent, too, so `stty echo` is done at the end of script.
parent end
subsub exit: tty=/dev/ttys001, PPID=55026, PID=3205, BASHPID=3205
=============================
Call subsub in $()+pipe
subsub start
tty=not a tty, PPID=55026, PID=9720, BASHPID=10033
read in subsub:
subsub killed: tty=not a tty, PPID=55026, PID=9720, BASHPID=10033
(@o@) $
=============================
Call subsub directly
subsub start
stty: stdin isn't a terminal
subsub exit: tty=not a tty, PPID=55026, PID=10615, BASHPID=10641
tty=/dev/ttys001, PPID=55026, PID=10615, BASHPID=10615
read in subsub:
subsub input:
=============================
*******************************
#!/usr/bin/env bash
subsub () {
echo subsub start >/dev/tty
echo ' 'tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty
trap "
echo subsub killed: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty;
return 1
" 1 2 3 15
trap "echo subsub' 'exit: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty" 0
echo "read in subsub: " >/dev/tty
#read -s -n 1 c </dev/tty
read -n 1 c </dev/tty
echo "subsub input:$c" >/dev/tty
}
sub () {
echo sub start
echo ' 'tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID
trap "
echo sub' 'killed: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty;
return 1
" 1 2 3 15
trap "echo sub' 'exit: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty>/dev/tty" 0
echo ""
echo "============================="
echo "read in sub: "
read -s -n 1 c </dev/tty
echo "sub input:$c"
echo ""
echo "============================="
echo "Call subsub in \$()"
echo "$(subsub)"
echo ""
echo "============================="
echo "Call subsub in pipe"
echo pipe|subsub
echo ""
echo "============================="
echo "Call subsub in \$()+pipe"
echo "$(echo hoge|subsub)"
echo ""
echo "============================="
echo "Call subsub in pipe+\$()"
echo "hoge" |$(subsub)
echo ""
echo "============================="
echo "Call subsub directly"
subsub
echo ""
echo "============================="
echo sub end
}
echo parent start
echo ' 'tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID
trap "
echo parent killed: BASHPID=$BASHPID >/dev/tty;
return 1
" 1 2 3 15
trap "echo parent' 'exit: BASHPID=$BASHPID >/dev/tty" 0
sub
echo $?
echo parent end
#!/usr/bin/env bash
subsub () {
echo subsub start >/dev/tty
echo ' 'tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty
trap "
echo subsub killed: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty;
return 1
" 1 2 3 15
trap "echo subsub' 'exit: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty" 0
echo "sleep" >/dev/tty
sleep 10
echo "subsub end" >/dev/tty
}
sub () {
echo sub start
echo ' 'tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID
trap "
echo sub' 'killed: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty >/dev/tty;
return 1
" 1 2 3 15
trap "echo sub' 'exit: tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID >/dev/tty>/dev/tty" 0
echo ""
#echo "============================="
#echo "sleep" >/dev/tty
#sleep 10
#echo ""
#echo "============================="
#echo "Call subsub in \$()"
#echo "$(subsub)"
#echo ""
#echo "============================="
#echo "Call subsub in pipe"
#echo pipe|subsub
#echo ""
#echo "============================="
#echo "Call subsub in \$()+pipe"
#echo "$(echo hoge|subsub)"
#echo ""
echo "============================="
echo "Call subsub directly"
subsub
echo ""
echo "============================="
echo sub end
}
echo parent start
echo ' 'tty=$(tty), PPID=$PPID, PID=$$, BASHPID=$BASHPID
trap "
echo parent killed: BASHPID=$BASHPID >/dev/tty;
return 1
" 1 2 3 15
trap "echo parent' 'exit: BASHPID=$BASHPID >/dev/tty" 0
sub
echo parent end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.