英文:
Bash script check file for specific data, remove them, replace them with new data
问题
抱歉,由于你的请求,我将只为你翻译代码部分:
We have number of Linux Ubuntu 20-22, CentOS 7/RedHat 8 servers all on wrong timezone.
I am tasked to synchronize the time on all of them.
I tried many options, sed, awk, grep. "awk" has been the most confusing, and not friendly.
"sed" allowed me to replace the existing entries, but i am unable to implement it in a for loop that will check if the entries are correct then exit, if not then replace them all.
I have been struggling with this for the past 2 weeks now because I am fairly new to Bash programming.
So, I am trying to create a generic script for all of them to replace the entries in chrony.conf
For example on Ubuntu the `chrony.conf` has the following entries:
# pool ntp.ubuntu.com iburst maxsources 4
# pool 0.ubuntu.pool.ntp.org iburst maxsources 1
On CentOS 7 `chrony.conf` has the following entries
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
If the script encounters the entries above, then it should replace with the entries below:
pool time.nrc.ca iburst
pool time.chu.nrc.ca iburst
英文:
We have number of Linux Ubuntu 20-22, CentOS 7/RedHat 8 servers all on wrong timezone.
I am tasked to synchronize the time on all of them.
I tried many options, sed, awk, grep. "awk" has been the most confusing, and not friendly.
"sed" allowed me to replace the existing entries, but i am unable to implement it in a for loop that will check if the entries are correct then exit, if not then replace them all.
I have been struggling with this for the past 2 weeks now because I am fairly new to Bash programming.
So, I am trying to create a generic script for all of them to replace the entries in chrony.conf
For example on Ubuntu the chrony.conf
has the following entries:
# pool ntp.ubuntu.com iburst maxsources 4
# pool 0.ubuntu.pool.ntp.org iburst maxsources 1
On CentOS 7 chrony.conf
has the following entries
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
If the script encounters the entries above, then it should replace with the entries below:
pool time.nrc.ca iburst
pool time.chu.nrc.ca iburst
Any help / guidance is appreciated.
答案1
得分: 1
以下是代码部分的翻译:
#!/usr/bin/env bash
awk '
{
despaced = $0
gsub(/[[:space:]]+/," ",despaced)
}
( despaced == "# pool ntp.ubuntu.com iburst maxsources 4" ) ||
( despaced == "# pool 0.ubuntu.pool.ntp.org iburst maxsources 1" ) {
$0 = "pool time.nrc.ca iburst"
}
( despaced == "# server 0.centos.pool.ntp.org iburst" ) ||
( despaced == "# server 1.centos.pool.ntp.org iburst" ) {
$0 = "pool time.chu.nrc.ca iburst"
}
{ print }
' "${@:--}"
希望这有所帮助。
英文:
Your requirements aren't clear in a few areas, e.g. what to do if only one of the target lines is present, what to do if the target text appears mid-line, what to do if neither line is present, what to do if the target text and replacement text are both already present, where to place the replacement text, etc., so here's one guess at what you might want, using any POSIX awk:
$ cat tst.sh
#!/usr/bin/env bash
awk '
{
despaced = $0
gsub(/[[:space:]]+/," ",despaced)
}
( despaced == "# pool ntp.ubuntu.com iburst maxsources 4" ) ||
( despaced == "# pool 0.ubuntu.pool.ntp.org iburst maxsources 1" ) {
$0 = "pool time.nrc.ca iburst"
}
( despaced == "# server 0.centos.pool.ntp.org iburst" ) ||
( despaced == "# server 1.centos.pool.ntp.org iburst" ) {
$0 = "pool time.chu.nrc.ca iburst"
}
{ print }
' "${@:--}"
<p>
$ head ubuntu_chrony.conf cent_chrony.conf
==> ubuntu_chrony.conf <==
# pool ntp.ubuntu.com iburst maxsources 4
# pool 0.ubuntu.pool.ntp.org iburst maxsources 1
==> cent_chrony.conf <==
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
<p>
$ ./tst.sh ubuntu_chrony.conf
pool time.nrc.ca iburst
pool time.nrc.ca iburst
<p>
$ ./tst.sh cent_chrony.conf
pool time.chu.nrc.ca iburst
pool time.chu.nrc.ca iburst
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论