使用bash/shell脚本打开并将数据写入文本文件

Adn*_*nan 207 bash shell

如何通过Linux中的shell脚本自动将数据写入文本文件?

我能够打开文件.但是,我不知道如何向它写入数据.

Tri*_*Man 356

echo "some data for the file" >> fileName
Run Code Online (Sandbox Code Playgroud)

  • operator>将输出重定向到覆盖文件的文件(如果存在).>>将附加到现有文件. (82认同)
  • 如果您需要以root权限执行此操作,请按以下方式执行此操作:```sudo sh -c'echo"文件的某些数据">> fileName'``` (7认同)
  • 这个运营商的名字是什么?我想找一本关于它的手册. (5认同)
  • 谢谢,这是维基页面的链接,以防有人需要它 http://en.wikipedia.org/wiki/Redirection_(computing)#Redirecting_standard_input_and_standard_output (4认同)

小智 128

#!/bin/sh

FILE="/path/to/file"

/bin/cat <<EOM >$FILE
text1
text2 # This comment will be inside of the file.
The keyword EOM can be any text, but it must start the line and be alone.
 EOM # This will be also inside of the file, see the space in front of EOM.
EOM # No comments and spaces around here, or it will not work.
text4 
EOM
Run Code Online (Sandbox Code Playgroud)

  • 请解释而不是简单地提出一段代码.它会让每个人都更有帮助 - 特别是新手. (8认同)
  • 新手将从确切知道如何处理此文件中受益.如何保存,如何运行等 (6认同)
  • @ATrubka /sf/ask/345645471/ (3认同)
  • 在这种情况下,带有$ some_var这样的变量的脚本会丢失。如何解决? (2认同)

sse*_*ano 53

您可以将命令的输出重定向到文件:

$ cat file > copy_file
Run Code Online (Sandbox Code Playgroud)

或附加

$ cat file >> copy_file
Run Code Online (Sandbox Code Playgroud)

如果要直接写命令是 echo 'text'

$ echo 'Hello World' > file
Run Code Online (Sandbox Code Playgroud)

  • 如果您需要 root 权限:`sudo sh -c 'cat file &gt; /etc/init/gunicorn.conf'`(感谢 lukaserat 上面的评论) (5认同)

小智 40

#!/bin/bash

cat > FILE.txt <<EOF

info code info 
info code info
info code info

EOF 
Run Code Online (Sandbox Code Playgroud)

  • 对代码的解释将大大有助于使其对其他人更有用.甚至像"here document"和"redirection"这样的词汇标注也可以将搜索者指向其他资源. (31认同)
  • 当您已经知道答案时,就没有必要进行解释。对我来说,由于缺乏细节,上面的代码没有帮助。 (15认同)
  • 关于`EOF`和相关技术的信息:http://stackoverflow.com/a/2954835/712334 (9认同)
  • 像这样的事情不需要解释。 (2认同)
  • EOF 之后 - 不应有空格。 (2认同)

tob*_*obi 22

我知道这是一个该死的老问题,但由于OP是关于脚本的,而且谷歌把我带到这里,同时也应该提到打开文件描述符进行读写.

#!/bin/bash

# Open file descriptor (fd) 3 for read/write on a text file.
exec 3<> poem.txt

    # Let's print some text to fd 3
    echo "Roses are red" >&3
    echo "Violets are blue" >&3
    echo "Poems are cute" >&3
    echo "And so are you" >&3

# Close fd 3
exec 3>&-
Run Code Online (Sandbox Code Playgroud)

然后cat在终端上的文件

$ cat poem.txt
Roses are red
Violets are blue
Poems are cute
And so are you
Run Code Online (Sandbox Code Playgroud)

这个例子导致文件poem.txt被打开以便在文件描述符3上进行读写.它还显示*nix框知道更多fd然后只是stdin,stdout和stderr(fd 0,1,2).它实际上拥有很多.通常,内核可以分配的文件描述符的最大数量可以在/proc/sys/file-max或者/proc/sys/fs/file-max使用或者使用9以上的任何fd是危险的,因为它可能与shell内部使用的fd冲突.所以不要打扰,只使用fd的0-9.如果你需要更多bash脚本中的9个文件描述符,你应该使用不同的语言:)

无论如何,fd可以用很多有趣的方式使用.


小智 12

我喜欢这个答案:

cat > FILE.txt <<EOF

info code info 
...
EOF
Run Code Online (Sandbox Code Playgroud)

但是cat >> FILE.txt << EOF如果你想在文件末尾添加一些内容而不删除已经存在的内容,则会建议

像这样:

cat >> FILE.txt <<EOF

info code info 
...
EOF
Run Code Online (Sandbox Code Playgroud)

  • 添加一些解释和答案,说明该答案如何帮助 OP 解决当前问题 (4认同)

tri*_*eee 11

对于此处文档不可用(MakefileDockerfile等)的环境,您通常可以使用printf合理易读且有效的解决方案。

printf '%s\n' '#!/bin/sh' '# Second line' \
    '# Third line' \
    '# Conveniently mix single and double quotes, too' \
    "# Generated $(date)" \
    '# ^ the date command executes when the file is generated' \
    'for file in *; do' \
    '    echo "Found $file"' \
    'done' >outputfile
Run Code Online (Sandbox Code Playgroud)

  • 您上面的评论 _Try echo "some data for the file" | sudo tee -a fileName &gt;/dev/null 只以 root 身份运行 tee._ 真的很有用! (2认同)

top*_*217 9

我认为有一些完美的答案,但没有对所有可能性的简明总结;因此:

这里大多数答案背后的核心原则是重定向。两个是用于写入文件的重要重定向运算符:

重定向输出:

echo 'text to completely overwrite contents of myfile' > myfile

附加重定向输出

echo 'text to add to end of myfile' >> myfile

这里的文件

其他人提到,echo 'text'您还可以通过“此处文档”以交互方式写入文件,而不是从固定输入源(如 )写入文件,上面的 bash 手册链接中也对此进行了详细说明。那些答案,例如

cat > FILE.txt <<EOF 或者 cat >> FILE.txt <<EOF

使用相同的重定向运算符,但通过“此处文档”添加另一层。在上述语法中,您通过 .txt 的输出写入 FILE.txt cat。写入仅在交互式输入被赋予一些特定字符串后发生,在本例中为“EOF”,但这可以是任何字符串,例如:

cat > FILE.txt <<'StopEverything' 或者 cat >> FILE.txt <<'StopEverything'

也能正常工作。此处文档还查找各种分隔符和其他有趣的解析字符,因此请查看文档以获取更多信息。

这里字符串

有点复杂,更多的是理解重定向和 Here Documents 语法的练习,但您可以将 Here Document 样式语法与标准重定向运算符结合起来,成为一个 Here 字符串:

重定向 cat 输入的输出

cat > myfile <<<'text to completely overwrite contents of myfile'

附加 cat 输入的重定向输出

cat >> myfile <<<'text to completely overwrite contents of myfile'


luk*_*rat 6

根据@lycono的要求,将我的评论作为答案移动

如果您需要以root权限执行此操作,请按以下方式执行此操作:

sudo sh -c 'echo "some data for the file" >> fileName'
Run Code Online (Sandbox Code Playgroud)

  • 您可能希望避免仅为打印字符串而运行root shell.尝试"回显"文件"|"的一些数据 sudo tee -a fileName>/dev/null`只能以root身份运行`tee`. (3认同)