gpt4 book ai didi

linux - 在 bash 中存储命令输出 "Here Document"

转载 作者:太空宇宙 更新时间:2023-11-04 09:41:33 25 4
gpt4 key购买 nike

下面是一个小的 bash 脚本。连接到服务器后的预期输出是打印 Hello World 并在下一行打印当前月份和年份 - 如 Jan 2014。为了在“此处文档”中使用 VARIABLE1,我需要进行美元扩展,因此不引用终止符 ~

VARIABLE1="World"
ssh username@server.domain.com <<~
echo "Hello $VARIABLE1"
COMMAND1=`date +%b`
COMMAND2=$(date +%Y)
echo "$COMMAND1 $COMMAND2"
~

我得到的实际输出是这样的:

Pseudo-terminal will not be allocated because stdin is not a terminal.
Password:
Hello World

ssh 以详细模式运行时,这是输出的最后一部分:

debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: *****
debug1: Trying private key: *****
debug1: Next authentication method: keyboard-interactive
Password:
debug1: Authentication succeeded (keyboard-interactive).
debug1: Final hpn_buffer_size = *****
debug1: HPN Disabled: 0, HPN Buffer Size: *****
debug1: channel 0: new [client-session]
debug1: Enabled Dynamic Window Scaling

debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
Hello World

debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
Transferred: sent 1552, received 2360 bytes, in 0.1 seconds
Bytes per second: sent 17253.2, received 26235.6
debug1: Exit status 0

有人可以指出这里的错误吗?为什么不打印 Jan 2014

最佳答案

有一些信息here关于如何强制 tty,但是,如果您正确地转义字符,您的脚本应该可以工作:

VARIABLE1="World"
ssh username@server.domain.com <<EOF
echo Hello $VARIABLE1
COMMAND1=\`date +%b\`
COMMAND2=\$(date +%Y)
echo "\$COMMAND1 \$COMMAND2"
EOF

如果你想强制 tty:

ssh -t -t user@domain.com <<EOF
echo Hello $VARIABLE1
COMMAND1=\`date +%b\`
COMMAND2=\$(date +%Y)
echo "\$COMMAND1 \$COMMAND2"
exit
EOF

或者,您可以将脚本作为参数发送给 ssh:

ssh user@domain.com "echo Hello $VARIABLE1;COMMAND1=\`date +%b\`; COMMAND2=\$(date +%Y); echo \$COMMAND1 \$COMMAND2"

编辑:如果您想禁用有关伪终端分配的消息,请使用“-T”:

VARIABLE1="World"
ssh -T username@server.domain.com <<EOF
echo Hello $VARIABLE1
COMMAND1=\`date +%b\`
COMMAND2=\$(date +%Y)
echo "\$COMMAND1 \$COMMAND2"
EOF

关于linux - 在 bash 中存储命令输出 "Here Document",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20902476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com