LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-02-2004, 08:32 PM   #1
toastermaker
Member
 
Registered: Oct 2003
Location: coastal alabama, united states
Distribution: Mandrake 10.0 official, slackware 9.1
Posts: 219

Rep: Reputation: 30
two commands in a script file not working


My (mis)understanding of running two commands in one script file is;

Simplest: Put each command on its own line and the shell will run them one after the other.
Or put the commands on the same line, separated with a semicolon.

I think I understand commands can be kind of ran together using pipes and if then stuff?

I have only tried the simplest methods so far, but cant figure out why they aren't working, like I thought they would, from what I have been reading.

What I'm trying to do, is an insmod for my ethernet driver, followed by dhclient eth0 in one file, to be executed by clicking a shortcut on my desktop.
It seems I will have to learn how to allow a regular user to to run these commands, but first I guess I should be able to run the script from the command line as root, but I cant make even that happen yet.

When I put the insmod command in a file and make the file executable, it works as I expect, I can just enter the name of the file at the command prompt and the insmod command runs and loads the driver for eth0.

Same for the command dhclient eth0 when it is the only command in the file.

But when I put both commands in the same file and try to run them by executing the file, only insmod works, dhclient eth0 does not work.It doesnt matter what command I put first in the file.

Any help understanding this greatly appreciated.
 
Old 01-02-2004, 08:40 PM   #2
poison
Member
 
Registered: Dec 2003
Location: Layer 7 =D
Distribution: Slackware, LFS, Rock Linux
Posts: 165

Rep: Reputation: 30
running commands after another in shell:

command1 && command2 && command3

u use pipes to direct the standard output (stdout) of one program to the standard input (stdin) of another one....
simple example:
ls -la|less

directs the output of ls -la to the pager less
 
Old 01-02-2004, 10:49 PM   #3
toastermaker
Member
 
Registered: Oct 2003
Location: coastal alabama, united states
Distribution: Mandrake 10.0 official, slackware 9.1
Posts: 219

Original Poster
Rep: Reputation: 30
Thank you poison,
Now the script runs fine when I'm logged as su.

I have chmod' the script file,and the file tries to open when I try to run it as a regular user, but I get the error " insmod command not found".

something tells me I shouldn't try to change permissions on insmod so that a regular user can run it.
What is a good way to make this work, to be able to run insmod and dhclient as a regular user?

Thank you for your replies and help.
 
Old 01-02-2004, 11:07 PM   #4
poison
Member
 
Registered: Dec 2003
Location: Layer 7 =D
Distribution: Slackware, LFS, Rock Linux
Posts: 165

Rep: Reputation: 30
sorry ^^
I didn't look to close at your post....
a shell script begins with the so called shebang line:
Code:
#!/bin/bash
this points to the location of the interpreter you want to use...and has to be at the VERY beginning of the file....in this case you use bash....
then you can add the commands you wish to issue...one line by another...
Code:
modprobe whatever
dhclient eth0
to make it executable,
chmod a+x <scriptname>

if it contains command which can onlly be issued by root, you have to set the uid or execute it as root...
to set the uid

chmod u+s <scriptname>

when you log out the root command now, you should be able to use it as plain user...
please notice that setting the uid on a executable is a security risk, since every user will be able to execute it with the rights of the super user....

regards
 
Old 01-03-2004, 05:34 AM   #5
toastermaker
Member
 
Registered: Oct 2003
Location: coastal alabama, united states
Distribution: Mandrake 10.0 official, slackware 9.1
Posts: 219

Original Poster
Rep: Reputation: 30
Thank you poison,
The script file will now load my ethernet driver by clicking a link on the desktop or from the command line as a regular user, but dhclient eth0 still does not run unless I am logged in as su. I have copied the error returned below:

can't create /var/lib/dhcp/dhclient.leases: Permission denied
SIOCSIFADDR: Permission denied
SIOCSIFFLAGS: Permission denied
SIOCSIFFLAGS: Permission denied
Open a socket for LPF: Operation not permitted

Thanks for help and replies.
 
Old 01-03-2004, 07:14 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
if it contains command which can onlly be issued by root, you have to set the uid or execute it as root...
to set the uid

chmod u+s <scriptname>

when you log out the root command now, you should be able to use it as plain user...
Having a script with SUID set doesn't work. Only binary executables can be set SUID. This is done deliberately because of the security risks. If you want to run a script as root while being a regular user, you should use su -c <scriptname> but you'll have to provide the root password every time when you run it. Use sudo <scriptname> if you want to type type root password every time. For sudo you'll have to make an entry for ypur script in /etc/sudoers. See "man sudo" and /etc/sudoers for more info.

Last edited by Hko; 01-03-2004 at 07:17 AM.
 
Old 01-04-2004, 07:15 AM   #7
toastermaker
Member
 
Registered: Oct 2003
Location: coastal alabama, united states
Distribution: Mandrake 10.0 official, slackware 9.1
Posts: 219

Original Poster
Rep: Reputation: 30
Thank you Hko,

I have tried using 3 differant tutorials and of course the man pages but still can only get sudo to work with:

teabear ALL=(ALL) ALL

I am the user "teabear". I want to allow teabear to run as root only one script file- makitgo, that has two commands in it - insmod and dhclient.

I have tried:

teabear ALL=/sbin/insmod, /sbin/dhclient, /home/teabear/makitgo

I took this from a tutorial section "limit the user to only specific commands"
It looked straight forward but isnt working as I'm trying to use it.
I get the error mesg. "teabear is not allowed to run makitgo on localhost as root"

Thank you for any help.

<edit>
To add to what I have tried,
A couple of variations of "teabear ALL=........" and when I searched these forums for "sudo" I only got 9 pages of results so I have looked through most of those threads but not yet found specific info that I'm after.

<edit>
I hit on this as the entry in my sudoers file under "user privlidge specification" it still allows me to run the script file as a regular user from the command line and the commands in the file work as I want them to:

teabear localhost=/home/teabear/makitgo

This looks like it will only give access to the one command, is that correct?
If so, my problem now is running it (makitgo) from a link on my desktop.

I will start wacking away at that, but if any one has info to help me out it is still greatly appreciated!

<edit>
Ok, functional now, I tacked , /sbin/dhclient onto the line teabear localhost=/home/teabear/makitgo,

Still if anyone sees security problem or poor security practice with this please let me know.

Thank you very much.

Last edited by toastermaker; 01-05-2004 at 12:53 AM.
 
  


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
console commands as script? rooman Linux - Newbie 14 02-02-2006 07:49 PM
How do I script these commands? johnnybhoy67 Linux - General 6 10-21-2005 09:53 AM
Commands from shell script not working Grassie Coetzee Linux - Software 2 03-13-2005 02:31 PM
run 2 commands in a script ddpicard Linux - General 10 06-13-2003 04:50 PM
Using vi commands in a bash script tiger Programming 1 05-21-2001 05:08 PM

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

All times are GMT -5. The time now is 01:19 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