我有这个多行字符串(包括引号):
abc'asdf"
$(dont-execute-this)
foo"bar"''
Run Code Online (Sandbox Code Playgroud)
如何在Bash中使用heredoc将其分配给变量?
我需要保留换行符.
我不想逃避字符串中的字符,这会很烦人......
我正在尝试在bash heredoc中插入变量:
var=$1
sudo tee "/path/to/outfile" > /dev/null << "EOF"
Some text that contains my $var
EOF
Run Code Online (Sandbox Code Playgroud)
这不符合我的预期(按$var字面意思处理,不进行扩展).
我需要使用,sudo tee因为创建文件需要sudo.做类似的事情:
sudo cat > /path/to/outfile <<EOT
my text...
EOT
Run Code Online (Sandbox Code Playgroud)
不起作用,因为>outfile在当前shell中打开文件,而不使用sudo.
有人可以告诉我为什么这不起作用吗?
#!/bin/bash
cd /home
touch somefile
/usr/bin/expect<<FILETRANSFER
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "assword:"
send "MyPassWord\r"
interact
FILETRANSFER
echo "It's done"
Run Code Online (Sandbox Code Playgroud)
它没有给出任何错误,但是文件没有传输到远程服务器。我尝试了许多方法仍然找不到任何解决方案。
在任何人说使用公钥之前,这不是一个选项,因为我正在与客户服务器打交道。
我有一个脚本来维护一个文件,其中包含我客户服务器的所有 IP/用户名/密码。通过脚本,我选择了一个服务器并使用 here 文档来登录该服务器。
#### Code up here to set the variables.
exp_internal 1
/usr/bin/expect << EOFFF
if { $USE_TUNNEL == 1 } {
spawn ssh -L 4567:localhost:3306 $user\@$ip
} else {
spawn ssh $user\@$ip
}
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "$pass\r"
}
}
expect {
"$ " { send "<<<Send stuff>>>" }
"# " { send "<<<Send stuff>>>" }
}
interact
EOFFF
# End of bash script
Run Code Online (Sandbox Code Playgroud)
我的问题是,在 here 文档结束后,它会关闭 …