我如何在这里写一个文档到Bash脚本文件?
当前回答
对于将来可能遇到这个问题的人,下面的格式是有效的:
(cat <<- _EOF_
LogFile /var/log/clamd.log
LogTime yes
DatabaseDirectory /var/lib/clamav
LocalSocket /tmp/clamd.socket
TCPAddr 127.0.0.1
SelfCheck 1020
ScanPDF yes
_EOF_
) > /etc/clamd.conf
其他回答
此外,如果您正在写入一个文件,检查写入是否成功或失败可能是一个好主意。例如:
if ! echo "contents" > ./file ; then
echo "ERROR: failed to write to file" >& 2
exit 1
fi
要对heredoc进行同样的操作,有两种可能的方法。
1)
if ! cat > ./file << EOF
contents
EOF
then
echo "ERROR: failed to write to file" >& 2
exit 1
fi
if ! cat > ./file ; then
echo "ERROR: failed to write to file" >& 2
exit 1
fi << EOF
contents
EOF
您可以通过将目标文件./file替换为/file(假设您不是以根用户身份运行)来测试上述代码中的错误情况。
当需要root权限时
当目标文件需要root权限时,使用|sudo tee而不是>:
cat << 'EOF' |sudo tee /tmp/yourprotectedfilehere
The variable $FOO will *not* be interpreted.
EOF
cat << "EOF" |sudo tee /tmp/yourprotectedfilehere
The variable $FOO *will* be interpreted.
EOF
使用tee代替cat和I/O重定向可能会有用:
tee newfile <<EOF
line 1
line 2
line 3
EOF
它更简洁,而且与重定向操作符不同,如果您需要以根权限写入文件,它可以与sudo结合使用。
例如,你可以使用它:
首先(进行ssh连接):
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
第二个(executing的聚会):
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
第三(执行命令):
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
(使用变量)。
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
阅读高级bash脚本编写指南第19章。这里的文档。
下面是一个将内容写入/tmp/yourfilehere文件的示例
cat << EOF > /tmp/yourfilehere
These contents will be written to the file.
This line is indented.
EOF
注意,最后的'EOF'(限制字符串)在单词前面不应该有任何空白,因为这意味着限制字符串将不会被识别。
在shell脚本中,您可能希望使用缩进来使代码可读,但这可能会产生对here文档中的文本缩进的不良影响。在这种情况下,使用<<-(后面跟着破折号)禁用开头的制表符(注意,为了测试这一点,您需要用制表符替换开头的空白,因为我不能在这里打印实际的制表符)。
#!/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