如何在Bash脚本中将Here文档写入文件?


#1楼

代替使用cat和I / O重定向,可以使用tee代替:

tee newfile <<EOF
line 1
line 2
line 3
EOF

它更加简洁,而且与重定向运算符不同,如果您需要使用root权限写入文件,可以将其与sudo结合使用。


#2楼

需要root权限时

当目标文件需要root权限时,请使用|sudo tee而不是>

cat << 'EOF' |sudo tee /tmp/yourprotectedfilehere
The variable $FOO will *not* be interpreted.
EOF

#3楼

例如,您可以使用它:

首先(建立ssh连接):

while read pass port user ip files directs; dosshpass -p$pass scp -o 'StrictHostKeyChecking no' -P $port $files $user@$ip:$directs
done <<____HEREPASS    PORT    USER    IP    FILES    DIRECTS.      .       .       .      .         ..      .       .       .      .         ..      .       .       .      .         .PASS    PORT    USER    IP    FILES    DIRECTS
____HERE

秒(执行命令):

while read pass port user ip; dosshpass -p$pass ssh -p $port $user@$ip <<ENDSSH1COMMAND 1...COMMAND n
ENDSSH1
done <<____HEREPASS    PORT    USER    IP.      .       .       ..      .       .       ..      .       .       .PASS    PORT    USER    IP
____HERE

第三(执行命令):

Script=$'
#Your commands
'while read pass port user ip; dosshpass -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; dosshpass -p$pass ssh -o 'StrictHostKeyChecking no' -p $port $user@$ip fileinput=$fileinput 'bash -s'<<ENDSSH1#Your command > $fileinput#Your command > $fileinput
ENDSSH1
done <<____HEREPASS    PORT    USER    IP      FILE-OUTPUT.      .       .       .          ..      .       .       .          ..      .       .       .          .PASS    PORT    USER    IP      FILE-OUTPUT
____HERE

#4楼

注意:

  • 以下内容浓缩并整理了该主题中的其他答案,尤其是Stefan Lasiewski和Serge Stroobandt的出色著作
  • 我和Lasiewski建议在《高级Bash脚本指南》中的第19章(此处为文档)

问题(如何在bash脚本中将here文档(aka heredoc )写入文件?)具有(至少)3个主要的独立维度或子问题:

  1. 您要覆盖现有文件,追加到现有文件还是写入新文件?
  2. 您的用户或其他用户(例如root )是否拥有该文件?
  3. 您要按字面意义编写Heredoc的内容,还是让Bash解释Heredoc内部的变量引用?

(还有其他我认为不重要的维度/子问题。请考虑编辑此答案以添加它们!)以下是上述问题的维度的一些更重要的组合,带有各种不同的定界标识符-没有任何内容关于EOF是神圣的,只需确保您用作定界标识符的字符串不会在Heredoc中出现:

  1. 要覆盖您拥有的现有文件(或写入新文件),请在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 
  2. 要附加您拥有的现有文件(或写入新文件),请在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 
  3. 要使用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 
  4. 要附加您拥有的现有文件(或写入新文件)以及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 
  5. 要覆盖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 
  6. 要附加用户= foo拥有的现有文件(或写入新文件),并带有heredoc的文字内容:

     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 

#5楼

对于以后可能遇到此问题的人们,以下格式适用:

(cat <<- _EOF_LogFile /var/log/clamd.logLogTime yesDatabaseDirectory /var/lib/clamavLocalSocket /tmp/clamd.socketTCPAddr 127.0.0.1SelfCheck 1020ScanPDF yes_EOF_
) > /etc/clamd.conf

#6楼

阅读《高级Bash脚本指南》 第19章 。

这是一个将内容写入/tmp/yourfilehere此处的示例

cat << EOF > /tmp/yourfilehere
These contents will be written to the file.This line is indented.
EOF

请注意,最后的'EOF'( LimitString )在单词前不应有任何空格,因为这意味着LimitString将不会被识别。

在shell脚本中,您可能希望使用缩进来使代码可读,但是这样做可能会对缩进here文档中的文本产生不良影响。 在这种情况下,请使用<<- (后接破折号)来禁用前导制表符( 请注意 ,要进行测试,您需要用制表符替换前导空格 ,因为我无法在此处打印实际的制表符。)

#!/usr/bin/env bashif true ; thencat <<- EOF > /tmp/yourfilehereThe 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

#7楼

要以@Livven的答案为基础,以下是一些有用的组合。

  1. 变量替换,保留前导选项卡,覆盖文件,回显到stdout

     tee /path/to/file <<EOF ${variable} EOF 
  2. 无变量替换 ,保留前导选项卡,覆盖文件,回显到stdout

     tee /path/to/file <<'EOF' ${variable} EOF 
  3. 变量替换, 删除前导选项卡 ,覆盖文件,回显到stdout

     tee /path/to/file <<-EOF ${variable} EOF 
  4. 变量替换,保留前导选项卡, 追加到文件 ,回显到stdout

     tee -a /path/to/file <<EOF ${variable} EOF 
  5. 变量替换,保留前导选项卡,覆盖文件, 不回显stdout

     tee /path/to/file <<EOF >/dev/null ${variable} EOF 
  6. 以上可以与sudo结合使用

     sudo -u USER tee /path/to/file <<EOF ${variable} EOF 

#8楼

我喜欢缩进脚本中的简洁,可读性和表示方式:

<<-End_of_file >file
→       foo bar
End_of_file

其中是制表符。

如何在Bash脚本中将Heredoc写入文件?相关推荐

  1. 如何在Bash脚本中将DOS / Windows换行符(CRLF)转换为Unix换行符(LF)?

    本文翻译自:How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script? How can I pro ...

  2. linux脚本中如何读取文件,如何在Shell脚本中逐行读取文件

    原标题:如何在Shell脚本中逐行读取文件 在这里,我们学习Shell脚本中的3种方法来逐行读取文件. 方法一.使用输入重定向 逐行读取文件的最简单方法是在while循环中使用输入重定向. 为了演示, ...

  3. 如何在bash脚本中提示用户进行确认? [重复]

    本文翻译自:How do I prompt a user for confirmation in bash script? [duplicate] This question already has ...

  4. linux脚本里用expect,如何在bash脚本中使用expect

    这是我在 following bash脚本中使用的代码片段: for user_input in `awk '{print}' testfile_$$.txt` do ipaddress=`echo ...

  5. oracle数据库无法写入文件,如何在ORACLE的PL/SQL中将数据写入文件

    在数据库的开发中,当PL/SQL语句很复杂的时候,我想写一些Log,就像java的Log4j那样.这样就可以很好的跟踪程序的运行情况.在网上找到的文章不是很好用,修改了一下. 1:在Oracle服务器 ...

  6. linux 杀死脚本,linux – 如何在Bash脚本被杀死时杀死当前命令

    我能够在评论中测试我发布的prctl想法,它似乎有效.你需要编译这个: #include "sys/prctl.h" #include "stdlib.h" # ...

  7. shell bash脚本_如何在Windows 10上创建和运行Bash Shell脚本

    shell bash脚本 With the arrival of Windows 10's Bash shell, you can now create and run Bash shell scri ...

  8. linux bash 写入文件

    目录 方法1:echo 方法2:tee 方法1:echo 要将Bash命令的输出写入文件,可以使用右尖括号符号(>)或双右尖符号(>>): 右尖括号(>) 右尖括号号(> ...

  9. 如何在bash中等待多个子进程完成并在任何子进程以代码!= 0结尾时返回退出代码!= 0?

    如何在bash脚本中等待从该脚本派生的多个子进程完成并返回退出代码!= 0,当任何子进程以代码!= 0结尾时? 简单脚本: #!/bin/bash for i in `seq 0 9`; dodoCa ...

最新文章

  1. linux_OEL5.4_安装Oracle11g中文教程图解
  2. 树 - 定义和基本概念
  3. Python面试基础题-2018-12-26
  4. 2007白领职场成功需要哪“十商”
  5. Spring Boot文档阅读笔记-构建Restful风格的WebService客户端
  6. python分词统计词频_基于结巴分词做的全文分词统计词频小脚本
  7. Python怎么去写单元测试用例去测试hello world呢
  8. day15【前台】项目发布
  9. 黑客伦理(hacker ethic)--《黑客与画家》
  10. 蚂蚁金服 Service Mesh 渐进式迁移方案|Service Mesh Meetup 实录
  11. 远程服务器网刻系统,无需U盘,网络批量安装系统,pxe网刻工具
  12. ABB机器人示教器修改IP
  13. 土地利用/土地覆盖数据整理
  14. python里面else什么意思_python中if else和if elif else有什么区别?
  15. Android API 中文(13) —— ToggleButton
  16. java下载Excel文件并设置表头内容与下拉框
  17. qca wifi相关操作命令
  18. 萤石云设备下线是什么导致的_设备下线
  19. 19.Oracle数据库SQL开发之 笛卡尔积
  20. 我知道你不知道,我到底知不知道?

热门文章

  1. 简单的Site to site ipsec ×××实验
  2. Linux文件默认权限——umask
  3. [C#] 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性
  4. Thrift的安装和简单示例
  5. ORA-01940: cannot drop a user that is currently connected
  6. hadoop 完全分布式模式的安装和配置
  7. ArcGIS 10.X功能增减(转)
  8. Centos Ubuntu防爆破ssh脚本
  9. (工作中)Apache常见配置
  10. Nginx 常见面试题