G
G
Grigory Shepelev2021-09-24 17:53:19
bash
Grigory Shepelev, 2021-09-24 17:53:19

How to prevent write using SED if value exists?

There is a 1.conf file with the contents:
/opt/pki/ca.crt
/opt/pki/ca.key It is
necessary to add after ".crt" for example "Ivanov"
Everything is clear here:
#!/bin/bash
filename="/opt /pki/1.conf"
search=".crt"
replace="Ivanov"

if ; then
sed -i "s/$search/& $replace/" $filename
fi
What if /opt/pki/1.conf already contains:
/opt/pki/ca.crt Ivanov
/opt/pki/ca. key
On restart it adds "/opt/pki/ca.crt Ivanov Ivanov" :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
paran0id, 2021-09-24
@gshepelev

Search not for '.crt', but for '.crt$' - with the end of the string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question