Bash脚本检查文件以查找特定数据,将其删除,然后用新数据替换。

huangapple go评论59阅读模式
英文:

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
==&gt; ubuntu_chrony.conf &lt;==
# pool ntp.ubuntu.com        iburst maxsources 4
# pool 0.ubuntu.pool.ntp.org iburst maxsources 1

==&gt; cent_chrony.conf &lt;==
# 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

huangapple
  • 本文由 发表于 2023年4月11日 11:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定