I am trying to write a script which will check to see if the contents of a file match a certain value and if so, overwrite it with contents of another file.
Example:
file1 =
A, B, C, D
1, 2, 3, 4
file2 =
E, F, G, H
5, 6, 7, 8
- Checks to see if grep of file1 =
A, B, C, D
- If yes,
cat file2
and write output back to file 1 - If no,
echo "Setting already configured"
and exit
Here is what I have:
#!/bin/bash
var01=$(cat /home/user/scripts/test01.txt | grep A)
var02=$(cat /home/user/scripts/test02.txt)
if [[ $var01 = "A, B, C, D" ]];
then
[[ echo $var02 > /home/user/scripts/test01.txt ]]
else
echo "Setting already configured"
fi
I keep getting the following errors for line 9:
line 9: conditional binary operator expected
line 9: syntax error near `$var02'
line 9: ` [[ echo $var02 > /home/user/scripts/test01.txt ]]'
Any pointers would be greatly appreciated. Thanks.
[[ ]]
around theecho
command -- square brackets make their contents a conditional expression, not a command, butecho
is a command. Also, double-quote your variables: useecho "$var02"
instead ofecho $var02
. shellcheck.net is good at spotting common mistakes like this, so I recommend running your scripts through it and fixing what it points out.grep
ping just forA