Linux的 - 巴什重定向一个字符串到一个文件 [英] Linux - Bash Redirect a String to a file

查看:163
本文介绍了Linux的 - 巴什重定向一个字符串到一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的脚本读取文件内容,并增加此文件中AA号码,那么我用awk控股的变化,当我试图使用RO重定向新的字符串'>'整个字符串在同一行,而不是像原来重定向是这是4行。

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.

#!/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/{$4=$4+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

我试图找出我怎么能重定向这是4行原来的格式文本。

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

推荐答案

您需要包装在双引号中的变量:

You need to wrap your variables in double quotes:

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

围绕 $ PATH / $文件的引号不是在这种情况下,确有必要,但他们没有坏处。

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

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

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

newexpr=$(awk '/^Build Number/{$4=$4+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/{$4=$4+1;}1' "$path/$file" > /tmp/file && mv /tmp/file "$path/$file"


使用引号的重要性

双引号preserve数据的原始格式。看到这个简单的例子,它使用设置-x 来激活调试模式。正在由Shell执行的命令显示与 + 开始的行。 <子>其实我看到你已经在使用#!/斌/ bash的-x 设置-x 做同样的事作为:


The importance of using quotes

The double quotes preserve the original format of the data. See this simple example, which uses set -x to 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 -x does the same thing as that.:

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

原来的字符串包含一个换行符,但是当你回声它不包括引号,这是PTED为两个参数回声,而不是包含一个换行符一个参数。这就是所谓的场分裂的。你可以通过阅读这篇这个wiki文章了解使用双引号的重要性。

The original string contains a newline but when you echo it 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.

这篇关于Linux的 - 巴什重定向一个字符串到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆