su in scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting su in scripts
# 1  
Old 08-12-2005
Question su in scripts

I'm looking to write a .sh script that will allow me, from my home, directory to list and remove *.lst and *.bad *.dat files owned by user 1 (easy enough!). I then need to su to user 2 so that I have permissions to remove the *.lst *.log and *.bad files from user 2 home directory. I have superuser privilages if it can be done through root.

Any ideas/script examples would be appreciated. Thanks in advance.

Soots
# 2  
Old 08-12-2005
Since you have superuser privileges you do not need to do any sort of 'su'. Root automatically has the rights to remove files from any and all directories (regardless of the ownerships).
# 3  
Old 08-12-2005
Well, generally speaking, if you want to run as a command as another user, you don't use su, but sudo. Read it's man page--pretty easy to use. But you will be prompted for the user's password (though only one time, so that you can do a bunch of sudo commands before having the time period run out and you have to retype the password.) Prompting for the password means that you can't run your script as cronjobs. Else you have to store the password in some file, which is nt safe.
# 4  
Old 08-12-2005
Quote:
Originally Posted by hadarot
Well, generally speaking, if you want to run as a command as another user, you don't use su, but sudo.
That really depends on the scope of work and permissions required to carry it out as well as the level of access available to the user. If the user has root access then the su can be carried out without a password (for safety to not delete incorrect files as shown in the post which follows this one) or the work required here can be directly carried out as root.

Last edited by reborg; 08-12-2005 at 09:21 PM..
# 5  
Old 08-12-2005
If you have root:
su - user1 -c "rm *.lst *.bad *.dat"
su - user2 -c "rm *.lst *.bad *.dat"
# 6  
Old 10-27-2005
I have tried above script
actually i want to remove some files which in owned by root dir.

so i have written my script like this

su -xxxx -c "rm *.dat"
where xxx is root password.

but the error user xxx does n't exist was coming.


please help


regards
rajan
# 7  
Old 10-27-2005
"su -xxxx -c "rm *.dat"
where xxx is root password.

but the error user xxx does n't exist was coming."


The above is incorrect syntax, here is the correct:

su - useryouwantobe -c "rm *.dat"

Notice the space between "su - " and username. If you are running this as root then u won't be prompted for a password, but if not then you will be prompted for the password of the user u are switching to. imo, use sudo.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling multiple scripts from another scripts

Dear all, I am working on script which call other shell scripts in a loop but problem is from second script am not able to come out. Here is the snippet:- #!/bin/bash HSFILE=/root/Test/Components.txt LOGFile=/opt/domain/AdminDomain/application/logs... (3 Replies)
Discussion started by: sharsour
3 Replies

2. Shell Programming and Scripting

Calling scripts from with scripts

Hi all, I'm wondering if you could give me some advice. I am new to scripting and am getting rather frustrated that i can get my script to call another script if certain criteria is met, via command line, but I cannot get the same script to work thru the cron jobs. My first script monitors... (8 Replies)
Discussion started by: echoes
8 Replies

3. Shell Programming and Scripting

KSH - How to call different scripts from master scripts based on a column in an Oracle table

Dear Members, I have a table REQUESTS in Oracle which has an attribute REQUEST_ACTION. The entries in REQUEST_ACTION are like, ME, MD, ND, NE etc. I would like to create a script which will will call other scripts based on the request action. Can we directly read from the REQUEST_ACTION... (2 Replies)
Discussion started by: Yoodit
2 Replies

4. Shell Programming and Scripting

Changing the Bash Scripts to Bourne Scripts:URGENT

Hi, I have to write a program to compute the checksums of files ./script.sh I wrote the program using bash and it took me forever since I am a beginner but it works very well. I'm getting so close to the deadline and I realised today that actually I have to use normal Bourne shell... (3 Replies)
Discussion started by: pgarg1989
3 Replies

5. Shell Programming and Scripting

Running scripts within scripts from cron

Hi all, I have set up a cron job which calls another shell script shell script which in turn calls a Java process. The cron tab looks so. 0,30 7-18 * * 1-5 /u01/home/weblogic/brp/bin/checkstatus.sh >> /u01/home/weblogic/logs/checkstatus.log The checkstatus.sh scripts looks like this. ... (4 Replies)
Discussion started by: sirbrian
4 Replies

6. Shell Programming and Scripting

Help with Script using rsh and scripts within scripts

Hi, I've written a script that runs on a Database server. It has to shutdown the Application server, do an Oracle Dump and then restart the Application server. Its been a long time since I wrote any shells scripts. Can you tell me if the scripts that I execute within my script will be executed... (3 Replies)
Discussion started by: brockwile1
3 Replies

7. UNIX for Dummies Questions & Answers

Profile scripts versus rc scripts....

what is the difference between login and profile scripts versus the rc scripts? (1 Reply)
Discussion started by: rookie22
1 Replies

8. Shell Programming and Scripting

Calling expect scripts from other expect scripts

Hi, First, let me explain the issue I am trying to solve. We have a lot of expect scripts with the duplicated send/expect commands. So, I'd like to be able to extract the duplicated code into the common scripts that can be used by other scripts. Below is my test where I am trying to call... (0 Replies)
Discussion started by: seva
0 Replies

9. UNIX for Dummies Questions & Answers

Help with scripts

script that ask for "enter a file name" and removes that file and asks for confirmation before deletion if executed the output might look as enter the filename you intent to deleted remover file? Y file deleterd I knwo the comand I would use find . -name *.* -ok rm {}\; I guess... can... (1 Reply)
Discussion started by: LiTo
1 Replies
Login or Register to Ask a Question