LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2012, 01:56 PM   #1
finaccio
LQ Newbie
 
Registered: Mar 2010
Location: Italy
Distribution: kubuntu 12.04
Posts: 17

Rep: Reputation: 0
bash :: change file value on /sys directory


hi!

How I can change the value of a file in /sys virtual filesystem with a script that run regardless the users who is logged.

Thanks a lot for your help

Finaccio
 
Old 06-12-2012, 02:42 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
You use sudo to run the script with elevated access privileges.

A simple way to do that is as follows. First, put the privileged stuff in a separate script, say /usr/local/bin/do-the-sys-stuff:
Code:
#!/bin/bash
[ "`id -u`" = "0" ] || exec sudo -- "$0" "$@"

# do the /sys stuff here
The second line causes the script to rerun itself via sudo if run without root privileges.

Then, allow any user to use sudo to run that script as root without a password, by adding line
Code:
ALL ALL = (root) NOPASSWD: /usr/local/bin/do-the-sys-stuff
into /etc/sudoers.d/allow-do-the-sys-stuff if /etc/sudoers.d/ exists, in /etc/sudoers otherwise. If you create a new file, make sure it is owned by root and has mode 0440: chown 0:0 file && chmod 0440 file

After that, any user can run /usr/local/bin/do-the-sys-stuff as themselves, causing it to rerun itself with root privileges via sudo, and doing your /sys stuff with root privileges.

Questions?
 
Old 06-12-2012, 02:46 PM   #3
finaccio
LQ Newbie
 
Registered: Mar 2010
Location: Italy
Distribution: kubuntu 12.04
Posts: 17

Original Poster
Rep: Reputation: 0
no questions!!

thanks a lot for your answer!

I am going to try your advices and I will give you feedback asap!

Finaccio
 
Old 06-12-2012, 02:51 PM   #4
finaccio
LQ Newbie
 
Registered: Mar 2010
Location: Italy
Distribution: kubuntu 12.04
Posts: 17

Original Poster
Rep: Reputation: 0
fantastic! It works!

thanks a lot for your help Nominal Animal

Finaccio
 
Old 06-13-2012, 09:14 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
A small addition, if I may.

As long as we're using bash, we can test the built-in $UID variable instead of running the external id command. (There's also $USER if you want the username string).

You should also take care to use arithmetic operators when doing integer comparisons. That's "-eq" inside the square-bracket tests ("=" is a string comparison). But actually, the ((..)) arithmetic evaluation bracket is the recommended structure to use in bash or ksh, where "==" is used for equality.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031

I prefer using the "&&" logic myself, BTW. It reads more naturally.
Code:
#!/bin/bash
(( UID != 0 )) && exec sudo -- "$0" "$@"

# do the /sys stuff here
Using "(( UID ))" alone would also work here, since "0" evaluates as false, and other values as true.


And don't forget: $(..) is highly recommended over `..`
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
chroot:cannot change root directory to: No such file directory janakiramulu Linux From Scratch 5 04-23-2011 01:00 AM
error: sys/timerfd.h: No such file or directory manohar Programming 1 03-09-2011 01:29 AM
quick directory change (bash) talla Linux - General 8 02-06-2010 06:43 AM
how to change directory using bash or other simple language packets Programming 3 05-08-2009 04:28 AM
torsmo error scandir for /sys/bus/i2c/devices/: No such file or directory KaZiber Linux - General 2 04-17-2005 02:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration