Sharat Chandra
Sharat Chandra

Reputation: 4544

How to execute commands requiring root privileges inside a script?

I have to change the frequency of CPU (through slide privileges) . I have written the command to change frequencies inside a script .However , when I run the script, I get the following error message

 #!/bin/bash
slide
        for i in 0 1 2 3 
        do
                echo 1600000 > /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_setspeed
        done

when I run the script , it gives following error

./change_freq.sh: line 4: /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed: Permission denied

Any suggestions ?

Upvotes: 2

Views: 835

Answers (1)

Dennis Williamson
Dennis Williamson

Reputation: 360005

I don't know how slide works, but a possibly similar action using sudo that produces the same kind of error would be:

sudo echo foo > /path/to/file

The way to solve that is:

echo foo | sudo tee /path/to/file > /dev/null

I would be curious to know if any of the following provide further information on the origin of slide:

type -a slide
man slide
slide -v
slide --version
slide -h
slide -?

Upvotes: 1

Related Questions