Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a shell script which outputs shell commands which are supposed to be copy-pasted with the mouse from one terminal window to another (I can test it in the same window though). The output should not contain any significant whitespace characters other than plain space, and the output lines should be truncated so that the code can be copied properly even on terminals where soft line breaks are copied as hard line breaks. I'd like to make sure. I'm thinking something like this:

$ eval `resize -s 24 80`
$ reset
$ my_script
$ mouse_copy *all of the terminal history except for the first line*
$ mouse_paste
$ assert *the paste created the proper result*

Emulating this with something like xclip would be nice, if possible.

The script should run on a custom Linux server which has no X. I don't know how in detail how the terminal output is sent to the client, but maybe the script would have to ask the X server on the client side what it's currently displaying?

share|improve this question
Will the script produce the same output if you pipe it into another program, or does it require its output to be a terminal? – Gilles Jul 5 '12 at 23:27

xsel - manipulate the X selection.

xsel --clipboard --input reads stdin into clipboard

xsel --clipboard --output write content of clipboard to stdout

share|improve this answer
Nice, but unfortunately it's not available in the stripped-down distro that I'm using. – l0b0 Jul 9 '12 at 9:16

Here's a shell snippet (untested) that should do something close to what you want, from what I understand of your question.

set -e
# Collect the output of the script in a variable
# Check that the script output is nice and copypastable
awk '
    /[^[:print:]]/ { print NR ": non-printable character"; exit 1 }
    / $/ { print NR ": trailing whitespace"; exit 1 }
    /.{79}/ { print NR ": line too long"; exit 1 }
' <<EOF

# Use expect to spawn a shell, feed it the script output, and
# check the result against expectations
export script_output
expect <<'EOF'
spawn sh
send "[array get env script_output]\n"
expect "the proper result"
share|improve this answer

As I understood you need to rerun commands in exactly the same sequence you already did since the beginning of session. Right?

So may be history command is useful here. Actually it depends on your HISTFORMAT, but if it has default value you can use some flag command like echo SOME_FLAG and use :

history | sed -n 'H;/SOM_FLAG/{/history/!{x;d}};${x;s/\n\s\+[0-9]*\s\+/\n/g;p}'

that will extract last commands since SOME_FLAG. How does sed work here:
H just copies current pattern to hold buffer
/SOME_FLAG/ watches if string matches to SOME_FLAG
if it does it also checks if it doesn't matches to history (it also depends on your shell stiings, sometimes command is already in history just after execution and histry will see its entry in it)
if it matches it delete all previous entries. and we have all commands after the last SOME_FLAG flag.

After that you can send it to shell via pipe for example:

history | sed -n 'H;/man/{/history/!{x;d}};${x;s/\n\s\+[0-9]*\s\+/\n/g;p}' | bash -x

Unfortunately it is pretty dangerous method: in case you're using some commands with deletion or with modifying important data it may destroy something. However method with automatic copy&paste with mouse simulation may make similar damages. So just be careful using this.

And it also need to set flag each time you need this feature.

share|improve this answer
Nope, sorry. I need to emulate the copying of code with a mouse, to check if there are any significant whitespace or other traps in the output. – l0b0 Jul 19 '12 at 20:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.