Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - Bash Redirect a String to a file

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

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

like image 694
Alex Brodov Avatar asked Aug 24 '14 10:08

Alex Brodov


People also ask

How do I redirect a output to a file in bash?

For utilizing the redirection of bash, execute any script, then define the > or >> operator followed by the file path to which the output should be redirected. “>>” operator is used for utilizing the command's output to a file, including the output to the file's current contents.

How do I redirect in bash?

The append >> operator adds the output to the existing content instead of overwriting it. This allows you to redirect the output from multiple commands to a single file. For example, I could redirect the output of date by using the > operator and then redirect hostname and uname -r to the specifications.

How redirect command output to a file in Linux?

You can use &> to redirect both stdout and stderr to a file. This is shorthand for command > output. txt 2>&1 where the 2>&1 means "send stderr to the same place as stdout" (stdout is file descriptor 1, stderr is 2).


Video Answer


1 Answers

You need to wrap your variables in double quotes:

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

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"

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

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.

like image 97
Tom Fenech Avatar answered Sep 22 '22 08:09

Tom Fenech