Linux - Bash 将字符串重定向到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25470652/
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-18 11:13:18  来源:igfitidea点击:

Linux - Bash Redirect a String to a file

linuxbashshellawksed

提问by Alex Brodov

I wrote a simple script that is reading the file content and incrementing a a number inside this file, then i'm holding the change using awk, when i'm trying ro redirect the new String using '>' the whole string is redirected in one line and not like the original was which is 4 lines.

我写了一个简单的脚本,它正在读取文件内容并在这个文件中增加一个数字,然后我使用 awk 保持更改,当我尝试使用 '>' 重定向新字符串时,整个字符串被重定向到一个线而不是像原来的那样是 4 行。

#!/bin/bash -x

# This script is for Incrementing build numbers

path=/home/RND/abrodov
file=tst.txt
tst=`cat $path/$file`
printf "this is the content of the file before incrementing: \n $tst"
newexpr=`awk '/^Build Number/{=+1;}1' /home/RND/abrodov/tst.txt`
printf "\n the new content \n $newexpr"
echo $newexpr > $path/$file

This is the original file before running the script:

这是运行脚本之前的原始文件:

Major Release Number = 4
Minor Release Number = 1
Service Pack Release Number = 2
Build Number = 22

This is the content after i used the script:

这是我使用脚本后的内容:

Major Release Number = 4 Minor Release Number = 1 Service Pack Release Number = 2 Build Number = 23

I'm trying to figure out how can i redirect the text in the original format which is 4 lines.

我想弄清楚如何以原始格式(4 行)重定向文本。

回答by Tom Fenech

You need to wrap your variables in double quotes:

您需要将变量用双引号括起来:

echo "$newexpr" > "$path/$file"

The quotes around $path/$filearen't actually necessary in this case but they do no harm.

$path/$file在这种情况下,周围的引号实际上并不是必需的,但它们没有害处。

More generally, you should also use $( )rather than backticks:

更一般地,您还应该使用$( )而不是反引号:

newexpr=$(awk '/^Build Number/{=+1;}1' "$path/$file")

If you want to achieve the effect of changing the file "in-place", you don't need to use a variable. You can use a temporary file like this:

如果要实现“就地”更改文件的效果,则不需要使用变量。您可以使用这样的临时文件:

awk '/^Build Number/{=+1;}1' "$path/$file" > /tmp/file && mv /tmp/file "$path/$file"


The importance of using quotes

使用引号的重要性

The double quotes preserve the original format of the data. See this simple example, which uses set -xto activate debug mode. The commands that are being executed by the shell are shown on the lines beginning with +. Actually I see that you're already using #!/bin/bash -x. set -xdoes the same thing as that.:

双引号保留数据的原始格式。请参阅此简单示例,它用于set -x激活调试模式。shell 正在执行的命令显示在以+. 实际上,我看到您已经在使用#!/bin/bash -x. set -x做同样的事情。

$ s="1
> 2"
$ set -x
$ echo $s
+ echo 1 2
1 2
$ echo "$s"
+ echo '1
2'
1
2

The original string contains a newline but when you echoit without quotes, it is interpreted as two arguments to echo, instead of one argument that contains a newline. This is called field splitting. You can learn more about the importance of using double quotes by reading this this wiki article.

原始字符串包含一个换行符,但是当echo它不带引号时,它被解释为 的两个参数echo,而不是一个包含换行符的参数。这称为字段拆分。通过阅读这篇 wiki 文章,您可以了解有关使用双引号的重要性的更多信息。