Skip to content

Instantly share code, notes, and snippets.

@rdpate
Created July 14, 2011 14:16
Show Gist options
  • Save rdpate/1082528 to your computer and use it in GitHub Desktop.
Save rdpate/1082528 to your computer and use it in GitHub Desktop.
#!/bin/bash
# http://nedbatchelder.com/blog/201107/caged_python.html
short_banner=true
coproc PYTHONSTARTUP= python -i 2>&1
pid=$! read="${COPROC[0]}" write="${COPROC[1]}"
if $short_banner; then
IFS= read -u$read line || exit
printf %s\\n "$line"
while IFS= read -u$read line; do
[[ _"$line" == _'Type '* ]] && break
done
line=
fi
while IFS= read -N1 -u$read c; do
if [ _"$c" = _$'\n' ]; then
printf %s\\n "$line"
line=
continue
fi
line="$line$c"
case "$line" in
'>>> '|'... ')
IFS= read input || break
[ -z "$input" -a "$line" = '>>> ' ] && line=
printf %s%s\\n "$line" "$input"
printf %s\\n "$input" >&$write
line=
;;
esac
done
exec {write}>&-
wait $pid
#!/bin/bash
# http://nedbatchelder.com/blog/201107/caged_python.html
short_banner=true
# both avoid user's PYTHONSTARTUP (if any) and set up our own prompts
coproc PYTHONSTARTUP=<(echo 'import sys; sys.ps1, sys.ps2 = ">>> \n", "... \n"; del sys') python -iu 2>&1
pid=$! read="${COPROC[0]}" write="${COPROC[1]}"
if $short_banner; then
IFS= read -u$read line || exit
printf %s\\n "$line"
while IFS= read -u$read line; do
[[ _"$line" == _'Type '* ]] && break
done
line=
fi
while IFS= read -u$read line; do
case "$line" in
'>>> '|'... ')
IFS= read input || break
[ -z "$input" -a "$line" = '>>> ' ] && line=
printf %s%s\\n "$line" "$input"
printf %s\\n "$input" >&$write
;;
*)
printf %s\\n "$line"
esac
done
exec {write}>&-
wait $pid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment