while read pass port user ip files directs; do
sshpass -p$pass scp -o 'StrictHostKeyChecking no' -P $port $files $user@$ip:$directs
done <<____HERE
PASS PORT USER IP FILES DIRECTS
. . . . . .
. . . . . .
. . . . . .
PASS PORT USER IP FILES DIRECTS
____HERE
Second(executing commands):
while read pass port user ip; do
sshpass -p$pass ssh -p $port $user@$ip <<ENDSSH1
COMMAND 1
.
.
.
COMMAND n
ENDSSH1
done <<____HERE
PASS PORT USER IP
. . . .
. . . .
. . . .
PASS PORT USER IP
____HERE
Third(executing commands):
Script=$'
#Your commands
'
while read pass port user ip; do
sshpass -p$pass ssh -o 'StrictHostKeyChecking no' -p $port $user@$ip "$Script"
done <<___HERE
PASS PORT USER IP
. . . .
. . . .
. . . .
PASS PORT USER IP
___HERE
Forth(using variables):
while read pass port user ip fileoutput; do
sshpass -p$pass ssh -o 'StrictHostKeyChecking no' -p $port $user@$ip fileinput=$fileinput 'bash -s'<<ENDSSH1
#Your command > $fileinput
#Your command > $fileinput
ENDSSH1
done <<____HERE
PASS PORT USER IP FILE-OUTPUT
. . . . .
. . . . .
. . . . .
PASS PORT USER IP FILE-OUTPUT
____HERE
(还有其他维度/子问题,我对 EOF 没什么神圣的,只要确保你用作分界标识符的字符串在你的heredoc中发生 not :
要覆盖您拥有的现有文件(或写入新文件),请替换heredoc中的变量引用:
cat << EOF > /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, with the variable contents substituted.
EOF
要附加您拥有的现有文件(或写入新文件),请替换heredoc中的变量引用:
cat << FOE >> /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, with the variable contents substituted.
FOE
使用heredoc的文字内容覆盖您拥有的现有文件(或写入新文件):
cat << 'END_OF_FILE' > /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, without the variable contents substituted.
END_OF_FILE
使用heredoc的文字内容追加您拥有的现有文件(或写入新文件):
cat << 'eof' >> /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, without the variable contents substituted.
eof
要覆盖root拥有的现有文件(或写入新文件),请替换heredoc中的变量引用:
cat << until_it_ends | sudo tee /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, with the variable contents substituted.
until_it_ends
使用heredoc的文字内容追加user = foo拥有的现有文件(或写入新文件):
cat << 'Screw_you_Foo' | sudo -u foo tee -a /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, without the variable contents substituted.
Screw_you_Foo
在shell脚本中,您可能希望使用缩进来使代码可读,但是这会使您在此文档中缩进文本产生不良影响 . 在这种情况下,使用 <<- (后跟短划线)来禁用前导标签( Note 用于测试,您需要 replace the leading whitespace with a tab character ,因为我无法在此处打印实际的标签字符 . )
#!/usr/bin/env bash
if true ; then
cat <<- EOF > /tmp/yourfilehere
The leading tab is ignored.
EOF
fi
如果您不想解释文本中的变量,请使用单引号:
cat << 'EOF' > /tmp/yourfilehere
The variable $FOO will not be interpreted.
EOF
通过命令管道管道heredoc:
cat <<'EOF' | sed 's/a/b/'
foo
bar
baz
EOF
输出:
foo
bbr
bbz
...或者使用 sudo 将heredoc写入文件:
cat <<'EOF' | sed 's/a/b/' | sudo tee /etc/config_file.conf
foo
bar
baz
EOF
8 回答
我喜欢这种方法在缩进脚本中简洁,可读和呈现:
其中
→
是制表符 .例如,您可以使用它:
First(making ssh connection):
Second(executing commands):
Third(executing commands):
Forth(using variables):
Note:
以下浓缩并组织该线程中的其他答案,尤其是Stefan Lasiewski和Serge Stroobandt的出色工作
Lasiewski和我推荐Ch 19 (Here Documents) in the Advanced Bash-Scripting Guide
问题(如何在bash脚本中将文件(也就是heredoc)写入文件?)(至少)有3个主要的独立维度或子问题:
是否要覆盖现有文件,附加到现有文件或写入新文件?
您的用户或其他用户(例如
root
)是否拥有该文件?你想在字面上写下你的heredoc的内容,还是要在你的heredoc里面用bash解释变量引用?
(还有其他维度/子问题,我对
EOF
没什么神圣的,只要确保你用作分界标识符的字符串在你的heredoc中发生 not :要构建@Livven的answer,这里有一些有用的组合 .
sudo
结合使用阅读Advanced Bash-Scripting Guide Chapter 19. Here Documents .
这是一个将内容写入
/tmp/yourfilehere
文件的示例请注意,最终的'EOF'(
LimitString
)不应该在单词前面有任何空格,因为这意味着LimitString
将无法识别 .在shell脚本中,您可能希望使用缩进来使代码可读,但是这会使您在此文档中缩进文本产生不良影响 . 在这种情况下,使用
<<-
(后跟短划线)来禁用前导标签( Note 用于测试,您需要 replace the leading whitespace with a tab character ,因为我无法在此处打印实际的标签字符 . )如果您不想解释文本中的变量,请使用单引号:
通过命令管道管道heredoc:
输出:
...或者使用
sudo
将heredoc写入文件:而不是使用
cat
和I / O重定向,而不是使用tee
可能是有用的:它更简洁,加上与重定向操作符不同,如果您需要使用root权限写入文件,它可以与
sudo
结合使用 .需要root权限时
如果目标文件需要root权限,请使用
|sudo tee
而不是>
:对于可能遇到此问题的未来人员,以下格式有效: