maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Maemo 5 / Fremantle (https://talk.maemo.org/forumdisplay.php?f=40)
-   -   A equivilent of a .bat file on the n900? (https://talk.maemo.org/showthread.php?t=39165)

LozBlake 2010-01-03 22:43

A equivilent of a .bat file on the n900?
 
I'm a bit of a linux noob so bear with me.

Basically i wonder if theres a way i can store two commands for x terminal and store them in a file which i could just open and it would execute the commands for me, like how .bat files do with command prompt in windows.

This would make my life much easier as i can't find a way to copy and paste in x terminal.

sudo gainroot;
echo 118 > /sys/class/i2c-adapter/i2c-2/2-0063/power_level;

This is what i'd like to do, a solution like this would save me lots of tedious typing every car journey

CrashandDie 2010-01-03 22:47

Re: A equivilent of a .bat file on the n900
 
Code:

echo "#!/bin/bash" > powerlevel.sh
echo "sudo gainroot; echo 118 > /sys/class/i2c-adapter/i2c-2/2-0063/powerlevel;" >> powerlevel.sh
chmod +x powerlevel.sh


Z0l 2010-01-03 22:52

Re: A equivilent of a .bat file on the n900
 
Pretty good, 2 small things:
-there is no bash in the default install, and i'm not sure if the symlink exists, so please change the first line to "#!/bin/sh"
-you forgot to mention, that the script can be run by issuing the following command from the x terminal:
./powerlevel.sh

LozBlake 2010-01-03 22:59

Re: A equivilent of a .bat file on the n900
 
thanks guys for your quick replies. this looks like what i would need, so just to check, do i type the code as crashanddue posted in x terminal and it will create the file, and then to run i just need to type ./powerlevel.sh as zio said?

Z0l 2010-01-03 23:02

Re: A equivilent of a .bat file on the n900
 
Yes, you type in this in X Terminal:
Code:

echo "#!/bin/sh" > powerlevel.sh
echo "sudo gainroot; echo 118 > /sys/class/i2c-adapter/i2c-2/2-0063/powerlevel;" >> powerlevel.sh
chmod +x powerlevel.sh

after this, you can run it anytime by typing this
Code:

./powerlevel.sh

qwerty12 2010-01-03 23:04

Re: A equivilent of a .bat file on the n900
 
Code:

#!/bin/sh

echo "echo 118 > /sys/class/i2c-adapter/i2c-2/2-0063/power_level" | sudo gainroot

Be sure to chmod +x. The echo into sudo gainroot is important - or, otherwise, it'll try echoing into power_level as user, not as root.

LozBlake 2010-01-03 23:14

Re: A equivilent of a .bat file on the n900
 
I think i have this problem, i have write the script as written above amd made the change zoi said, it works fine when i run it when i'm already root in xterm however it doesn't change the powerlevel if i run it as user, even though the sudo root command is in there, all it does is give me root access.

Any ideas what i need to change?

qwerty12 2010-01-03 23:20

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by LozBlake (Post 451787)
I think i have this problem, i have write the script as written above amd made the change zoi said, it works fine when i run it when i'm already root in xterm however it doesn't change the powerlevel if i run it as user, even though the sudo root command is in there, all it does is give me root access.

Any ideas what i need to change?

http://talk.maemo.org/showpost.php?p=451774&postcount=6

RWFarley 2010-01-03 23:28

Re: A equivilent of a .bat file on the n900
 
is the problem that it should be entered as one line? My browser splits the part after the pipe onto a new line.

texaslabrat 2010-01-03 23:31

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by LozBlake (Post 451787)
I think i have this problem, i have write the script as written above amd made the change zoi said, it works fine when i run it when i'm already root in xterm however it doesn't change the powerlevel if i run it as user, even though the sudo root command is in there, all it does is give me root access.

Any ideas what i need to change?

You can't use sudo like that...the "sudo gainroot" launches a new shell process but the next command in your script executes in the exiting "user"-owned shell. It's not a hard problem to fix in a "normal" linux distro (I typically use "su" with flags to switch users inside a script) but I'm not sure how you would do it with the seemingly unique way that the root account and user account are related in maemo. On the surface it would seem as though you could simply issue a "sudo ./myscript" but I don't think that works out of the box in maemo. Hopefully some of the more seasoned maemo veterans will have an answer for the on-liner you are seeking to create.

One thing you *might* try is to setuid the script (chown file to root, and then chmod u+s the file) that does the things you want to do. You would then execute it as "user" but it would automagically run as root. Can provide a nice huge security hole in most unix systems, but with the "sudo gainroot" thing that doesn't even require a password, I'm not sure that's a concern in this particular case.

LozBlake 2010-01-03 23:49

Re: A equivilent of a .bat file on the n900
 
i tried to do as you said qwerty but i can't get it working, i'm not sure what to try now, i think texas has summed it up nicely, i'll have to run it from root for now.

Is there a way to clear the contents of the powerlevel.sh file from x terminal? i accidentially but the command in the file once wrong.

jaeezzy 2010-01-03 23:53

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by LozBlake (Post 451787)
I think i have this problem, i have write the script as written above amd made the change zoi said, it works fine when i run it when i'm already root in xterm however it doesn't change the powerlevel if i run it as user, even though the sudo root command is in there, all it does is give me root access.

Any ideas what i need to change?

Open up any terminal you are comfortable with and type the following:

Code:

#!/bin/sh
if [ `id -u` != 0 ]; then
sudo gainroot <<EOF
$0 $*
EOF
exit
fi
echo 118 > /sys/class/i2c-adapter/i2c-2/2-0063/power_level

It changes to root if you are just user and executes the command or else just executes the command.

qwerty12 2010-01-03 23:56

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by LozBlake (Post 451823)
i tried to do as you said qwerty but i can't get it working, i'm not sure what to try now, i think texas has summed it up nicely, i'll have to run it from root for now.

Is there a way to clear the contents of the powerlevel.sh file from x terminal? i accidentially but the command in the file once wrong.

Odd...

Well, this worked for me:
Code:

#! /bin/sh

VAL=118
FILE=/sys/class/i2c-adapter/i2c-2/2-0063/power_level

if [ $(id -u) = 0 ]; then
        echo $VAL > $FILE
else
        echo "echo $VAL > $FILE" | sudo gainroot
fi

cat $FILE


DaveQB 2010-01-03 23:57

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by texaslabrat (Post 451808)
You can't use sudo like that...the "sudo gainroot" launches a new shell process but the next command in your script executes in the exiting "user"-owned shell. It's not a hard problem to fix in a "normal" linux distro (I typically use "su" with flags to switch users inside a script) but I'm not sure how you would do it with the seemingly unique way that the root account and user account are related in maemo. On the surface it would seem as though you could simply issue a "sudo ./myscript" but I don't think that works out of the box in maemo. Hopefully some of the more seasoned maemo veterans will have an answer for the on-liner you are seeking to create.

One thing you *might* try is to setuid the script (chown file to root, and then chmod u+s the file) that does the things you want to do. You would then execute it as "user" but it would automagically run as root. Can provide a nice huge security hole in most unix systems, but with the "sudo gainroot" thing that doesn't even require a password, I'm not sure that's a concern in this particular case.

Just run visudo as root and add scripts/commands you want to be able to run as a normal user with with root privileges.

That's the idea of the sudo system.

RWFarley 2010-01-04 00:06

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by DaveQB (Post 451838)
Just run visudo as root and add scripts/commands you want to be able to run as a normal user with with root privileges.

That's the idea of the sudo system.

Is there a webpage describing the "sudo system"? Years ago I got some AIX experience, but I've forgotten a lot over the years....

texaslabrat 2010-01-04 00:07

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by DaveQB (Post 451838)
Just run visudo as root and add scripts/commands you want to be able to run as a normal user with with root privileges.

That's the idea of the sudo system.

That's not how it's done in maemo as far as I can tell. Take a look at /etc/sudoers.d. But you have the right idea, though for the OP's issue I still think just setuid'ing the file is the cleanest/most straightforward way to do it, but that's just my opinion.

LozBlake 2010-01-04 00:13

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by jaeezzy (Post 451831)
Open up any terminal you are comfortable with and type the following:

Code:

#!/bin/sh
if [ `id -u` != 0 ]; then
sudo gainroot <<EOF
$0 $*
EOF
exit
fi
echo 118 > /sys/class/i2c-adapter/i2c-2/2-0063/power_level

It changes to root if you are just user and executes the command or else just executes the command.

will this put the command into the powerlevel.sh file so in future i will just need to run ./powerlevel.sh or will i need to do more for that to happen? also do i type that in as one string or do i press enter after every line? sorry if this is all a bit dumb but i'm a complete novice with linux

rooted 2010-01-04 00:22

Re: A equivilent of a .bat file on the n900
 
You can use rootsh instead of sudo. For example: rootsh <command> will execute <command> as root.

jaeezzy 2010-01-04 00:28

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by LozBlake (Post 451861)
will this put the command into the powerlevel.sh file so in future i will just need to run ./powerlevel.sh or will i need to do more for that to happen? also do i type that in as one string or do i press enter after every line? sorry if this is all a bit dumb but i'm a complete novice with linux

Sorry, here's what you do:

1. Open up xterm.
2. In xterm type: vi powerlevel.sh
3. Press "i" key it will activate "Insert" mode.
4. Type as it is there (every line is a new line so there is enter after every line however, you don't press space in the front).
5. After you have finished typing everything, press the esc key and then ":" followed by "wq" (without double quotes) and hit enter.
6. It will save your file as powerlevel.sh in the same directory you are currently working.
7. Again in the xterm type: chmod +x powerlevel.sh
8. To execute: ./powerlevel.sh

That's it.

DaveQB 2010-01-04 00:29

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by RWFarley (Post 451849)
Is there a webpage describing the "sudo system"? Years ago I got some AIX experience, but I've forgotten a lot over the years....

Here's a start:
https://help.ubuntu.com/community/RootSudo

A little verbose, but you can skim read to the parts relevant to you [and Maemo]

DaveQB 2010-01-04 00:33

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by texaslabrat (Post 451853)
That's not how it's done in maemo as far as I can tell. Take a look at /etc/sudoers.d. But you have the right idea, though for the OP's issue I still think just setuid'ing the file is the cleanest/most straightforward way to do it, but that's just my opinion.

Perhaps.
I saw the sudoers.d dir but didn't get back to looking in there. I'll give it a peek.
Thanks.

ewan 2010-01-04 00:35

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by texaslabrat (Post 451853)
I still think just setuid'ing the file is the cleanest/most straightforward way to do it, but that's just my opinion.

Linux, as with most modern unixes, doesn't support suid shell scripts, only binaries.

jaeezzy 2010-01-04 00:42

Re: A equivilent of a .bat file on the n900
 
On top, to make life more easier and thats what I did in my n810 but haven't tried it in n900 as I don't own it yet (but for different purpose), you can, according to this, create a .desktop file in /usr/share/applications/hildon and this will create an icon in the main menu which you can just click and get the job done. Easy :)

DaveQB 2010-01-04 00:52

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by jaeezzy (Post 451911)
On top, to make life more easier and thats what I did in my n810 but haven't tried it in n900 as I don't own it yet (but for different purpose), you can, according to this, create a .desktop file in /usr/share/applications/hildon and this will create an icon in the main menu which you can just click and get the job done. Easy :)

http://talk.maemo.org/showthread.php...2&goto=newpost

LozBlake 2010-01-04 00:54

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by jaeezzy (Post 451886)
Sorry, here's what you do:

1. Open up xterm.
2. In xterm type: vi powerlevel.sh
3. Press "i" key it will activate "Insert" mode.
4. Type as it is there (every line is a new line so there is enter after every line however, you don't press space in the front).
5. After you have finished typing everything, press the esc key and then ":" followed by "wq" (without double quotes) and hit enter.
6. It will save your file as powerlevel.sh in the same directory you are currently working.
7. Again in the xterm type: chmod +x powerlevel.sh
8. To execute: ./powerlevel.sh

That's it.

thank you for typing the out for me!

I've done this and the file saved fine, i checked the contents with cat powerlevel.sh and it was correct, however when running it it doesn't change the powerlevel, any idea why? it looks fine to me and i did the chmod to, a message saying root enabled comes up when i run the file as user so it looks like its calling the correct file.

On a side note when following your instructions i had to type sudo gainroot to run the commands, would this have caused a problem?

texaslabrat 2010-01-04 00:56

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by ewan (Post 451902)
Linux, as with most modern unixes, doesn't support suid shell scripts, only binaries.

ah, well been a while since I've gone that route so I didn't notice the change. Good to know ;)

DaveQB 2010-01-04 01:02

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by LozBlake (Post 451924)
thank you for typing the out for me!

I've done this and the file saved fine, i checked the contents with cat powerlevel.sh and it was correct, however when running it it doesn't change the powerlevel, any idea why? it looks fine to me and i did the chmod to, a message saying root enabled comes up when i run the file as user so it looks like its calling the correct file.

On a side note when following your instructions i had to type sudo gainroot to run the commands, would this have caused a problem?

This might help you:
http://talk.maemo.org/showpost.php?p...4&postcount=91

jaeezzy 2010-01-04 01:33

Re: A equivilent of a .bat file on the n900
 
Quote:

Originally Posted by LozBlake (Post 451924)
On a side note when following your instructions i had to type sudo gainroot to run the commands, would this have caused a problem?

which command required you to hit "sudo gainroot". You should be able to create, chmod & execute (everything) as normal user. Try other commands that requires being root and see if they work coz I've no idea why it's not working for you.(in my n810 i'm restarting my device, change host/otg mode(not yet supported in n900 as far as I know) with similar scripts but not this one). Hope you are pointing to the right place.

mnaveed 2010-04-14 10:27

Re: A equivilent of a .bat file on the n900?
 
i did the following thing in xterminal without going in to sudo gainroot

Quote:

echo "#!/bin/sh" > oc.sh

echo "sudo gainroot; echo 124999 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq; echo 899999 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq; cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq; cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq;" >> oc.sh

chmod +x oc.sh



is it going to work when i write this command in xterm

./oc.sh

EDIT: i tried this with the ./oc.sh command with and without sudo gainroot but an error is shown with " permission denied"

can some body point oit my mistake, please.

DaveQB 2010-04-27 15:59

Re: A equivilent of a .bat file on the n900?
 
Permission denied usually means the executable bit is not set [you seemed to have taken care of that] or the underlying File System has noexec enabled, or it is a FAT or other weird FS.

Try

Code:

sh oc.sh


All times are GMT. The time now is 10:37.

vBulletin® Version 3.8.8