在測試腳本中模擬滑鼠剪下和貼上行為

在測試腳本中模擬滑鼠剪下和貼上行為

我有一個 shell 腳本,它輸出應該是的 shell 指令用滑鼠複製貼上從一個終端視窗到另一個終端視窗(不過我可以在同一個視窗中測試它)。輸出應該除純空格外不包含任何重要的空白字符,且輸出行應該被截斷,以便即使在將軟換行符複製為硬換行符的終端上也可以正確複製程式碼。我想確定一下。我在想這樣的事情:

$ 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*

如果可能的話,用類似的東西來模擬xclip會很好。

該腳本應該在沒有 X 的自訂 Linux 伺服器上運行。

答案1

xsel- 操縱 X 選擇。

xsel --clipboard --input將標準輸入讀入剪貼簿

xsel --clipboard --output將剪貼簿的內容寫入標準輸出

答案2

這是一個 shell 片段(未經測試),根據我對你的問題的理解,它應該做一些接近你想要的事情。

set -e
# Collect the output of the script in a variable
script_output=$(my_script)
# 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
$script_output
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"
EOF

答案3

據我了解,您需要以與會話開始後完全相同的順序重新運行命令。正確的?

history所以命令在這裡可能很有用。實際上這取決於你的HISTFORMAT,但如果它有預設值,你可以使用一些標誌命令,例如echo SOME_FLAG和 use :

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

這將提取自 SOME_FLAG 以來的最後命令。sed這裡是如何工作的:
H只需複製當前模式以保存緩衝區,
/SOME_FLAG/監視字串是否與 SOME_FLAG 匹配,
如果匹配,它還會檢查它是否不匹配history(這也取決於您的shell stiings,有時命令在執行後就已經在歷史記錄中,並且histry 會在其中看到它的條目)
如果匹配則刪除所有先前的條目。我們有最後一個 SOME_FLAG 標誌之後的所有指令。

之後,您可以透過管道將其發送到 shell,例如:

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

不幸的是,這是非常危險的方法:如果您使用某些命令刪除或修改重要數據,它可能會破壞某些內容。然而,使用滑鼠模擬自動複製和貼上的方法可能會造成類似的損害。所以使用這個時要小心。

每次需要此功能時還需要設定標誌。

相關內容