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 02-16-2005, 05:54 AM   #1
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Rep: Reputation: 15
su in shell script


Hi
Im using the advanced bash scripting guide to learn shell scripts and i can't seem to find an example of using 'su - n'. The script is run as root, but then later i want to change from root to another user. This line causes the problem

Code:
 su - temper || { echo "Couldn't su"; exit 1 }
echo "now temper"
I know why the problem is caused, ('su - n') asks for a login shell, right ? So whats the syntax to do what i want to do, run script as root, then change to new user and run rest of script as new user ?

Thanks
 
Old 02-16-2005, 06:09 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Try: su -m temper
or: su -p temper

If that doesn't work, try: su -c "$0" temper
 
Old 02-16-2005, 06:10 AM   #3
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
...

If at the beginning you hava #!/bin/bash, you would create a 'new shell' with its own local variable. The other way to execute a shell script is (for bash at least) is to 'source ' shell source code.

ex:
toto.sh=
#!/bin/bash
ls

command ./toto.sh lists the files.

toto.sh=
ls

command ./toto.sh does not work
source toto.sh lists the directory.

I don't know if it can help...
 
Old 02-16-2005, 06:19 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Here are 3 ways (well 2, the third is a variation on the second):

Code:
#!/bin/bash

# Example 1:

echo "`id -un` - before"
echo ""

{
echo "id -un"
echo "echo \"Not root anymore\""
} | su - temper || { echo "Couldn't su -"; exit 1 ; }

echo ""
echo "`id -un` - after"

# Example 2:

echo "`id -un` - before"
echo ""

su - temper -c "id -un ; echo 'not root anymore'" || { echo "Couldn't su -"; exit 1 ; }

echo ""
echo "`id -un` - after"

# Example 3:

echo "`id -un` - before"
echo ""

su - temper -c "/home/temper/tets01" || { echo "Couldn't su -"; exit 1 ; }

echo ""
echo "`id -un` - after"
BTW: Your error exit echo's couldn't su, this sghould be couldn't su -

Hope this helps.
 
Old 02-16-2005, 12:07 PM   #5
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
Hi, thanks again druuna

Thing is i need to execute more than just a few commands as the other user, and after a command the script switches back to root. Is there not a way i can switch to the other user and stay as the other user until i give another su command ?
 
Old 02-16-2005, 12:19 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi again,

Code:
#!/bin/bash
echo "`id -un` - before"
echo ""
echo "do some automated stuff as user root"

su - temper

echo ""
echo "Back in script started by root as root"
echo ""
echo "do some more automated stuff"
echo ""
The above code will start a session for user temper, it will stay open untill you (manually!) close it. But I believe this is what you want. You can do lots of things as user tempes untill you type exit or ctrl-d.

If you want to automate it all, take another look at example 3 from my original post.

/home/temper/tets01 is a script that is started by user temper, and thus must obey to the owner/group settings of this user. Put all the commands/actions in this script and they will all be executed (if allowed).

If I misunderstood, try again
 
Old 02-16-2005, 01:02 PM   #7
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
Hi
thanks for the patience . .

when i run this code;
Code:
su - temper -c "id -un ; echo 'not root anymore'" || { echo "Couldn't su -"; exit 1 ; }
I am only temper for the first command and then after im back to root

when i run this code
Code:
su - temper || { echo "Couldn't su -"; exit 1 ; }
The script errors with 'dev/tty1 operation not permitted' or something like that. Then i am dumped back into bash but as temper

What i want is to run the script as root, then change to temper (in the script with su) and then execute a number of tasks in the script as user temper and later perhaps to su back to root.

But i was thinking perhaps this isn't the done thing and maybe better to run as root and then change to temper in the script and then call a different script to run as temper. however i'd rather not do this if i didn't have to. What do you think ?

Anyway hope thats clearer. btw I went to netherlands last summer for a cycling holiday, its a great country mt8 ! pity most people don't get past amsterdam tho
 
Old 02-16-2005, 02:11 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

One thing first, I think you don't realize this:

If you su - (or su) another shell is started, but the one you started it from is still there and waiting (patiently). To get back to the 'beginning' you need to exit the shell(s) you started. I say this because of the remarks in posts #5 ( i give another su command ) and #7 ( su back to root ).

In short, if you do a su <user> or su - <user> you start another shell that should be closed again.

Some ways to do this, closing, are pressing CTRL-d or typing exit.

If I simplify things then the same is true when a script is started, it will start a new shell, do the commands and closes that shell again.


Code:
su - temper || { echo "Couldn't su -"; exit 1 ; }
This should work. From a bash shell commandline:

[stasis] druuna ~ $ su - jade || { echo "Couldn't su -"; exit 1 ; }
Password:
[stasis] jade ~ $

How do you run this piece of code?

BTW: The above code snippet migth not behave the way you expect it to due to the point I made in the first part of this post.
The exit 1 will exit the 'parent' shell (the shell from where you executed the code). This could be correct when done from a shell script (script will end), but if done from the command line your shell (session) will be closed.

Quote:
What i want is to run the script as root, then change to temper (in the script with su) and then execute a number of tasks in the script as user temper and later perhaps to su back to root.

But i was thinking perhaps this isn't the done thing and maybe better to run as root and then change to temper in the script and then call a different script to run as temper. however i'd rather not do this if i didn't have to. What do you think ?
The part in italic should be: return to root.
No perhaps, no su

It all depends on how much automation you want to have (my opinion: automate all that is possible )

If you want to automate this, then I still think this is the way to go:

As root start a script. From within that script do some things as user root, start a script as user temper to do some things as user temper. Once this script is done, control is given back to the script started by root and it will continue (do some things as user root.

You need 2 scripts for that. The one that is started by root and the one called from that script

Here's the script that root starts:

Code:
#!/bin/bash
echo ""
echo "Running as user `id -un`"
echo ""

# need to do some things as user temper
# if something goes wrong exit this(!) script.

su - temper -c "/tmp/tets01" || { echo "Couldn't su -"; exit 1 ; }

echo ""
echo "Running as user `id -un`"
echo "Doing more things as root"
echo ""
Save it, make it executable and change owner/group to root.

This is the script that is called from the above script:

Code:
#!/bin/bash
echo ""
echo "  Running as user `id -un`"
echo "  Doing things"
echo ""
Save this last one as /tmp/tets01, make it executable an change its owner/group to that of temper.

It doesn't do much but print the user it runs under and echo some stuff. But, as you probably know, shellscripts can contain all sorts of commands.

Enough typing for one nigth I'll find out soon enough if there will be a 'to be continued'......

Last edited by druuna; 02-16-2005 at 02:15 PM.
 
Old 02-17-2005, 04:07 AM   #9
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
ok, its clear as crystal now. Thanks a lot

Things are kind of working, the first script calls the second, theres still a fair few errors but i reckon i should be able to get them sorted.

I spent a fair bit of time last night looking for a way to find out the path to the current running script. For example now i can hardcode the path to the second script because i know where it is, but what if i change the directory where the scripts are? also if someone downloads the scripts i don't know from where they will run the first script but if i knew this location i know the second script can be found there too. Anyone know about that ?

The other thing is that when googling for information on scripting its a bit tricky since i put the search term and then am never sure whether to put '+ shell scripts' '+ bash' '+unix scripting' etc maybe i need to get myself a book

well anyway, hope someone knows about the paththing cos i can find anything

thanks and laters
 
Old 02-17-2005, 05:07 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Quote:
ok, its clear as crystal now. Thanks a lot
You're welcome

Quote:
Things are kind of working, the first script calls the second, theres still a fair few errors but i reckon i should be able to get them sorted.

I spent a fair bit of time last night looking for a way to find out the path to the current running script. For example now i can hardcode the path to the second script because i know where it is, but what if i change the directory where the scripts are? also if someone downloads the scripts i don't know from where they will run the first script but if i knew this location i know the second script can be found there too. Anyone know about that ?
There are several ways to tackle this.

- 'Smart' programming,

A small example:

Do this:
Code:
#!/bin/bash
# Set 'base' directories
BIN_DIR="/usr/local/bin"
ALT_BIN_DIR="/home/temper/bin"
.
.
# execute script(s)
${BIN_DIR}/script
su - temper -c "${ALT_BIN_DIR}/tets01"
${BIN_DIR}/anotherscript
If you change the locatation of script and/or tets01 the only thing you have to do is change the BIN_DIR=".." and/or ALT_BIN_DIR=".." setting, you don't have to search the script for all the instances of /usr/local/bin and/or /home/temper/bin

- Include a README/Install file with pointers on how to set up the scripts/change variables etc.

There are more solutions, but those are to elaborate to explain here (create a installable package to name just one).

Quote:
The other thing is that when googling for information on scripting its a bit tricky since i put the search term and then am never sure whether to put '+ shell scripts' '+ bash' '+unix scripting' etc maybe i need to get myself a book
Books are my prefered choice, although the info on the net can be (very) usefull too.

Some books that could be of help:

Learning the Kornshell http://www.oreilly.com/catalog/korn2/ (also see last url 'usefull links').
Learning the Bash shell http://www.oreilly.com/catalog/bash2/
Sed/awk http://www.oreilly.com/catalog/sed2/

There are a lot more good books out there, these came to mind first.

Some usefull links:

Bash Guide for Beginners
Linux Shell Scripting Tutorial
Advanced Bash scripting
The GNU Awk User's Guide
sed, a stream editor
CD Bookshelfs

Hope this helps.

PS: I don't know if the last link (CD Bookshelf) is legal. It has been up for a long, long time, but I truly don't know what O'Reilly's thoughts are about this.........
 
Old 02-18-2005, 02:31 PM   #11
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
Once again, thanks
 
  


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
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM

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

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