Tri*_*Man 356
echo "some data for the file" >> fileName
Run Code Online (Sandbox Code Playgroud)
小智 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)
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)
小智 40
#!/bin/bash
cat > FILE.txt <<EOF
info code info
info code info
info code info
EOF
Run Code Online (Sandbox Code Playgroud)
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)
tri*_*eee 11
对于此处文档不可用(Makefile、Dockerfile等)的环境,您通常可以使用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)
我认为有一些完美的答案,但没有对所有可能性的简明总结;因此:
这里大多数答案背后的核心原则是重定向。两个是用于写入文件的重要重定向运算符:
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 >> myfile <<<'text to completely overwrite contents of myfile'
根据@lycono的要求,将我的评论作为答案移动
如果您需要以root权限执行此操作,请按以下方式执行此操作:
sudo sh -c 'echo "some data for the file" >> fileName'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
606212 次 |
| 最近记录: |