You are not logged in.

#1 2012-05-28 10:15:36

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,302

String substitution

I've written a short bash script that throws up a zenity requester to enable choosing a cpu frequency and then setting it in /etc/conf.d/cpufreq and restarting cpufreq. The zenity requester is very straightforward, offering 5 frequencies, and the choice is stored in a bash variable $freq. The script then uses sed to replace the max_freq in cpufreq config file. However, whilst I have this working I'm sure there must be a better way. The line for changing is:

max_freq="X.XGhz"

Including quotes. This creates a problem because to get the $freq into the sed expression  I have to double quote the whole thing. I'm currently using

eval "sed -i -e 's/\(max\_freq\=.\).*\(.Hz.\)/\1$freq\2/g' /etc/conf.d/cpufreq"

I'm sure that there must be a better way of doing this, though, if anyone has any ideas?

Incidentally, it's necessary because I often have to run my processor at 100% for extended periods, and during the hot weather this can lead to overheating, so it's handy to have a simple method of underclocking my CPU during these periods.


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Cooler Master N300 chassis, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Online

#2 2012-05-28 10:44:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: String substitution

First, I'm not sure why the eval is there.  Unless there is some need I am not understanding, I'd recommended dropping it.  Then the sed line could be

sed -i 's/\(max_freq="\)[0-9\.]*/\1'$freq'/' /etc/conf.d/cpufreq

Last edited by Trilby (2012-05-28 10:45:18)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-05-28 11:02:07

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,302

Re: String substitution

I never thought of un-quoting $freq in the sed line hmm Doh!

The eval was there because it seemed to be the only way that I could get $freq converted.


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Cooler Master N300 chassis, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Online

Board footer

Powered by FluxBB