The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calling expect scripts from other expect scripts seva Shell Programming and Scripting 0 04-03-2008 10:45 AM
FTP Scripts raghav1982 Shell Programming and Scripting 27 12-07-2007 03:38 AM
use of ssh in scripts esham Shell Programming and Scripting 7 11-16-2005 08:27 AM
Help with GDL to SQL scripts ch4r1e5 Shell Programming and Scripting 0 10-31-2005 02:51 PM
using su in scripts uchachra UNIX for Dummies Questions & Answers 3 03-11-2002 10:24 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-12-2005
Registered User
 

Join Date: Aug 2005
Location: Loch Lomond
Posts: 1
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
Reply With Quote
Forum Sponsor
  #2  
Old 08-12-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
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).
Reply With Quote
  #3  
Old 08-12-2005
Registered User
 

Join Date: Jul 2005
Posts: 33
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.
Reply With Quote
  #4  
Old 08-12-2005
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,644
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 05:21 PM.
Reply With Quote
  #5  
Old 08-12-2005
Registered User
 

Join Date: Dec 2000
Location: Greater Dallas area
Posts: 405
If you have root:
su - user1 -c "rm *.lst *.bad *.dat"
su - user2 -c "rm *.lst *.bad *.dat"
__________________
[url=http://chuckb.1le.net/]My website[/url]
Reply With Quote
  #6  
Old 10-27-2005
Registered User
 

Join Date: Sep 2005
Posts: 130
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
Reply With Quote
  #7  
Old 10-27-2005
Registered User
 

Join Date: Oct 2005
Posts: 22
"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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0