bash 写入文件,但如果存在则覆盖它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4676459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:04:36  来源:igfitidea点击:

Write to file, but overwrite it if it exists

bashunix

提问by switz

echo "text" >> 'Users/Name/Desktop/TheAccount.txt'

How do I make it so it creates the file if it doesn't exist, but overwrites it if it already exists. Right now this script just appends.

我如何制作它以便它在文件不存在时创建它,但如果它已经存在则覆盖它。现在这个脚本只是附加。

回答by Nylon Smile

A little understanding of how *nix pipes work would help.

稍微了解 *nix 管道的工作原理会有所帮助。

In short the >>redirection operator will append lines to the end of the specified file, where-as the single greater than >will empty and overwrite the file.

简而言之,>>重定向操作符会将行附加到指定文件的末尾,其中单个大于>将清空并覆盖文件。

echo "text" > 'Users/Name/Desktop/TheAccount.txt'

回答by BrDaHa

In Bash, if you have set noclobber a la set -o noclobber, then you use the syntax >|

在 Bash 中,如果您设置了 noclobber a la set -o noclobber,则使用语法>|

For example:

例如:

echo "some text" >| existing_file

echo "some text" >| existing_file

This also works if the file doesn't exist yet

如果文件尚不存在,这也有效



  • Check if noclobber is set with: set -o | grep noclobber

  • For a more detailed explanation on this special type of operator, see this post

  • For a more exhaustive list of redirection operators, refer to this post

  • 检查 noclobber 是否设置为: set -o | grep noclobber

  • 有关这种特殊类型运算符的更详细说明,请参阅此帖子

  • 有关更详尽的重定向运算符列表,请参阅此帖子

回答by Alex Gray

Despite NylonSmile's answer, which is "sort of" correct.. I was unable to overwritefiles, in this manner..

尽管NylonSmile's answer,这是“有点”正确..我无法以这种方式覆盖文件..

echo "i know about Pipes, girlfriend" > thatAnswer

echo "i know about Pipes, girlfriend" > thatAnswer

zsh: file exists: thatAnswer

zsh: file exists: thatAnswer

to solve my issues.. I had to use... >!, á la..

解决我的问题.. 我不得不使用... >!, á la..

[[ $FORCE_IT == 'YES' ]] && echo "$@" >! "$X" || echo "$@" > "$X"

Obviously, be careful with this...

显然,要小心这个......

回答by computerist

If your environment doesn't allow overwriting with >, use pipe |and teeinstead as follows:

如果您的环境不允许的方式覆盖>,使用管道|tee替代如下:

echo "text" | tee 'Users/Name/Desktop/TheAccount.txt'

Note this will also print to the stdout. In case this is unwanted, you can redirect the output to /dev/nullas follows:

请注意,这也将打印到标准输出。如果这是不需要的,您可以将输出重定向到/dev/null如下:

echo "text" | tee 'Users/Name/Desktop/TheAccount.txt' > /dev/null

回答by Balaji Boggaram Ramanarayan

#!/bin/bash

cat <<EOF > SampleFile

Put Some text here 
Put some text here
Put some text here

EOF

回答by greensaxman

Just noting that if you wish to redirect both std::cerr and std::cout to a file while you have noclobber set (i.e. set -o noclobber), you can use the code:

请注意,如果您希望在设置 noclobber 时将 std::cerr 和 std::cout 重定向到文件(即set -o noclobber),则可以使用以下代码:

    cmd >|file.txt 2>&1

More information about this can be seen at https://stackoverflow.com/a/876242.

有关这方面的更多信息,访问https://stackoverflow.com/a/876242

Also this answer's @TuBui's question on the answer @BrDaHa provided aboveat Aug 9 '18 at 9:34.

此外,这个答案是@TuBui 在 2018 年8 月 9 日 9:34 对上面提供的答案@BrDaHa 提出的问题。

回答by Narendra Maru

To overwrite one file's content to another file. use cat eg.

将一个文件的内容覆盖到另一个文件。使用猫,例如。

echo  "this is foo" > foobar.txt
cat foobar.txt

echo "this is bar" > bar.txt
cat bar.txt

Now to overwrite foobar we can use a cat command as below

现在要覆盖 foobar 我们可以使用如下的 cat 命令

cat bar.txt >> foobar.txt
cat foobar.txt

enter image description here

在此处输入图片说明

回答by aaron-coding

If you have output that can have errors, you may want to use an ampersand anda greater than, as follows:

如果您的输出可能有错误,您可能需要使用与号和大于号,如下所示:

my_task &> 'Users/Name/Desktop/task_output.log'this will redirect both stderr and stdout to the log file (instead of stdout only).

my_task &> 'Users/Name/Desktop/task_output.log'这会将 stderr 和 stdout 都重定向到日志文件(而不仅仅是 stdout)。