Bash Shell – Can Excess Copy-Paste Text Be Ignored

clipboardcommand lineinput-methodshelluser interface

Say, I have a "Quick&Dirty" Perl script in a gui text editor, and I start the Perl interpreter in a terminal window which is running Bash. I can copy-paste the Perl script to the terminal & press CTRL-D to execute it with Perl. Perl will interpret the script and execute it.

Sometimes, there is a typo in the script, which makes Perl print a FATAL ERROR and exit, but the remaining copy-paste text is given to the Bash shell which tries to execute that; Either that fails (lucky case) or executes but will Do something Different (unlucky case).

Eg : start Perl in Bash Prompt , copy-paste a script of 5 lines : lines 1 & 2 are fine , but line 3 makes Perl exit abnormally , hence line 4 & 5 are executed by Bash.

I am using Perl & Bash only for example; Problem can happen with many other interactive tools ( Eg Python, fdisk ) and other shells ( Eg zsh, csh )

Is there any way to inform the shell that this text is a copy-paste input and can be ignored ?
Something like "If faster than user can normally type, then Ignore" ?
Or something like "If Process finished, then flush input buffer before reading next shell input" ?

[[ Some tools do not have this problem, Eg MySQL cli will never exit on improper input. ]]

Best Answer

This is not an answer, but maybe it's an acceptable work-around:

alias p='perl; echo hit control-d again; cat > /dev/null'

Then, if your perl script exits prematurely, you'll harmlessly paste the remainder to /dev/null; if the perl script succeeds, you'll see your friendly reminder and hit control-d to exit the cat catcher.

Related Question