tmux 发送键吞掉空格

发布于 2025-01-11 04:00:07 字数 703 浏览 0 评论 0原文

概要:“tmux send-keys”从 bash 命令中删除空格,我不明白为什么(或者如何,真的。)

test ()
  {
    tmux new -s testsession -d
    tmux send-keys -t testsession "time tar -I \"zstd -19 -T0\" -cvf ${1}.tar.zst "${@:2}""
    tmux attach -t testsession
  }

输入

输入1 输入2 输入3 i\ n\ p\ u\ t\ 4

预期(和期望)输出为

时间 tar -I "zstd -19 -T0" -cvf input1.tar.zst "input2" "input3" "in 放 4"

相反,我得到

时间 tar -I "zstd -19 -T0" -cvf input1.tar.zst "input2input3input4"

注意我省略了 ;厘米;在发送键末尾按 ENTER。 (而且我还简化了原始函数,因为其他部分更简单、更有效。)我这样做是为了更准确地理解昨晚花了几个小时试图暴力破解终端上输出的内容。 “正确”语法(无济于事。)

Synopsis: "tmux send-keys" strips the spaces from a bash command and I don't understand why (or how, really.)

test ()
  {
    tmux new -s testsession -d
    tmux send-keys -t testsession "time tar -I \"zstd -19 -T0\" -cvf ${1}.tar.zst "${@:2}""
    tmux attach -t testsession
  }

with an input of

input1 input2 input3 i\ n\ p\ u\ t\ 4

Expected (and desired) output is

time tar -I "zstd -19 -T0" -cvf input1.tar.zst "input2" "input3" "i n
p u t 4"

Instead I get

time tar -I "zstd -19 -T0" -cvf input1.tar.zst "input2input3input4"

Note I have omitted the ; C-m or ; ENTER at the end of the send-keys. (And I've also simplified the original function since other parts are more straightforward and work.) I've done that to get a more precise understanding of what is outputted on the terminal during several hours spent last night trying to brute-force the 'correct' syntax (, to no avail.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想你只要分分秒秒 2025-01-18 04:00:07

我偶然发现了同样的问题,并且发现了为空格添加关键字“Space”的不太好的解决方案。

因此,在您的情况下,我希望以下命令能够工作:

tmux send-keys -t testsession "time Space tar Space -I Space \"zstd Space -19 Space -T0\" Space -cvf Space ${1}.tar.zst Space "${@:2}""

我从本页的 发送密钥说明“密钥列表”部分。

I stumbled over the same problem and I found the not-so-nice solution of adding the keyword, "Space", for the spaces.

So, in your case, I would expect following command to work:

tmux send-keys -t testsession "time Space tar Space -I Space \"zstd Space -19 Space -T0\" Space -cvf Space ${1}.tar.zst Space "${@:2}""

I got this idea from this page's send-keys description "list of Keys" section.

暗地喜欢 2025-01-18 04:00:07

使用 "$*" 而不是 "$@"

tmux send-keys -t bar "$*" C-m

"$*" 将参数表示为单个字符串,而不是拆分为数组。

使用 "$@" 时,即使用引号包围,单词边界也会保留,因此将 "$@" 发送到另一个程序会单独发送每个输入。这使得将输入从包装函数传递到另一个函数变得很容易。

$@"$@" 之间的区别在于,虽然 "$@" 保留参数拆分,但不会进一步拆分。请参阅 shellcheck wiki

我用于测试的脚本 test.sh

#!/bin/sh

printargs()
{
    echo "\$1: $1"
    echo "\$2: $2"
    echo "\$3: $3"
}

foo="$@"

printargs $@
printargs "$@"
printargs $*
printargs "$*"
printargs $(echo "$@")
printargs "$(echo "$@")"
printargs $foo
printargs "$foo"

调用 ./test.sh foo 'bar baz' 打印到终端:

$1: foo
$2: bar
$3: baz
$1: foo
$2: bar baz
$3:
$1: foo
$2: bar
$3: baz
$1: foo bar baz
$2:
$3:
$1: foo
$2: bar
$3: baz
$1: foo bar baz
$2:
$3:
$1: foo
$2: bar
$3: baz
$1: foo bar baz
$2:
$3:

我正在使用 dash,一个 POSIX 兼容的 shell,但使用 bash 运行它会产生相同的结果。

Use "$*" instead of "$@":

tmux send-keys -t bar "$*" C-m

"$*" represents the arguments as single string instead of split into an array.

When using "$@", the word boundaries are kept even when surrounded by quotes, so sending "$@" into another program sends each input seperately. This makes it easy to pass input from a wrapper funciton into another function.

The difference between $@ and "$@" is that while "$@" preserves argument splits, it will not split it any further. See shellcheck wiki.

The script I used for testing, test.sh:

#!/bin/sh

printargs()
{
    echo "\$1: $1"
    echo "\$2: $2"
    echo "\$3: $3"
}

foo="$@"

printargs $@
printargs "$@"
printargs $*
printargs "$*"
printargs $(echo "$@")
printargs "$(echo "$@")"
printargs $foo
printargs "$foo"

Calling ./test.sh foo 'bar baz' prints to the terminal:

$1: foo
$2: bar
$3: baz
$1: foo
$2: bar baz
$3:
$1: foo
$2: bar
$3: baz
$1: foo bar baz
$2:
$3:
$1: foo
$2: bar
$3: baz
$1: foo bar baz
$2:
$3:
$1: foo
$2: bar
$3: baz
$1: foo bar baz
$2:
$3:

I am using dash, a POSIX compliant shell, but running it with bash produced the same result.

骄傲 2025-01-18 04:00:07

您可以使用 shell 参数扩展:

function quote() { echo "${@@Q}"; }

function test() { 
  tmux new -s testsession -d
  command="time tar -I \"zstd -19 -T0\" cvf ${1}.tar.zst $(quote "${@:2}")"
  tmux send-keys -t testsession "$command"
  tmux attach -t testsession
} 

Tmux send-keys 将带引号的输入作为按键序列读取,因此我们需要带引号的引号来使 tmux 输入引号。

You can quote the arguments correctly using shell parameter expansion:

function quote() { echo "${@@Q}"; }

function test() { 
  tmux new -s testsession -d
  command="time tar -I \"zstd -19 -T0\" cvf ${1}.tar.zst $(quote "${@:2}")"
  tmux send-keys -t testsession "$command"
  tmux attach -t testsession
} 

Tmux send-keys reads the quoted input as a sequence of key strokes, so we need quoted quotes to make tmux type the quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文