Bourne script: Check for root and oracle user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bourne script: Check for root and oracle user
# 1  
Old 09-24-2008
Bourne script: Check for root and oracle user

I have 2 separate Bourne shell scripts with 2 questions in Sun O/S & UNIX environment.

Question 1: One of the scripts is supposed to be executed by "root" user only but cannot be executed after user executes "su - oracle". How can I check in the script whether the current user is "root" user? "who am i" does not prevent the user from executing this script after executing "su - oracle".

Question 2: This is opposite of question 1. The other script should be executable only if the "root" user executes "su - oracle". How can I prevent the script from continue execution if the user does not execute "su - oracle"?
# 2  
Old 09-24-2008
Install your scripts in a non-NFS-mounted directory. Set ownership (chown) both scripts to root. Allow only root to execute the first script (chmod og-x). Allow anyone to run the second script (chmod og+rx). Now, in the SECOND script, you can check to see if it's root running, and exit if so:

Code:
test `id -u` = 0 && exit 2;

(Using this code only works to prove that you are NOT root. Proving that you are root is better left to the Operating System.) Feel free to engineer your own error message.
# 3  
Old 09-24-2008
Thanks a lot. I think "id -u" works perfectly for me to use in both cases.
# 4  
Old 09-24-2008
Just found out "id -u" does not work in some of the machines. Not really sure why. Could be due to individual machine's configuration issue. Not very safe for me to use.

Found the following command from a colleague with some modification by myself. It seems more reliable for me:

env | grep 'USER='| sed 's/USER=//'

This one can check for both "oracle", "root" or whatever user you want to check.

Thanks to otheus anyway as I have learnt something new.
# 5  
Old 09-24-2008
Quote:
Originally Posted by totziens
Just found out "id -u" does not work in some of the machines. Not really sure why. Could be due to individual machine's configuration issue.
In that case, hardcode the path:

/usr/bin/id -u

Or always use the GNU version:

/usr/local/bin/id -u # if id is the GNU version installed in /usr/local/bin.

You can get it from coreutils.

Alternatively, you can get it from ps:
Code:
$ ps u $$ |awk 'NR == 2 { print $1 }'
10362

It could be you get a name, in which case you then get it from getent:
Code:
getent `ps u $$ |awk 'NR == 2 { print $1 }'` passwd

# 6  
Old 09-25-2008
Thanks, otheus.

I tried the following commands and the results are as follows. It's not really the command "id" cannot be found but it's the "-u" option giving problem (except when I used /usr/local/bin/). It's no longer a problem for me in the script using the method I mentioned in the previous post but I am just curious about "id -u" - my colleague also complains about facing similar problem with "id" command...so he avoids using it in our machines.

Command: id -u
Result:
id: illegal option -- u
Usage: id [-ap] [user]

Command: /usr/bin/id -u
Result:
/usr/bin/id: illegal option -- u
Usage: id [-ap] [user]

Command: /usr/local/bin/id -u
Result:
/usr/local/bin/id: Command not found.


Any idea what could have caused "-u" option not being recognised?
# 7  
Old 09-25-2008
Right, so download and install GNU coreutils. This might mean getting other GNU utils as well, such as make.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Switch from Local user to root user from a shell script?

Hi, I need to switch from local user to root user in a shell script. I need to make it automated so that it doesn't prompt for the root password. I heard the su command will do that work but it prompt for the password. and also can someone tell me whether su command spawns a new shell or... (1 Reply)
Discussion started by: Little
1 Replies

2. UNIX and Linux Applications

Oracle Database - How to check if user roles and system roles are separated?

I have these two table. How do I see if user roles and system roles are seperated? SQL> desc DBA_ROLES; Name Null? Type ----------------------------------------- -------- ---------------------------- ROLE NOT NULL... (1 Reply)
Discussion started by: alvinoo
1 Replies

3. Homework & Coursework Questions

New user needs help with bourne shell script

This is my question, below the question is the template Write and execute a Bourne shell script called homework that will From within the script, create three background processes: a) (2 points) one that saves a long listing of your hidden files to a file named hiddenlist b) (2 points) ... (4 Replies)
Discussion started by: luislozoya
4 Replies

4. Red Hat

How to check local accounts have root and user access rights ?

Hi, I have three servers,For 3 servers how i can take output,all the local accounts and details of whether the access is Root or User access. cheers (1 Reply)
Discussion started by: ranjithm
1 Replies

5. Shell Programming and Scripting

How to Login as another user through Shell script from current user[Not Root]

Hi Every body, I would need a shell script program to login as different user and perform some copy commands in the script. example: Supppose ora_toms is the active user ora_toms should be able to run a script where user: ftptomsp pass: XXX should login through and run the commands ... (9 Replies)
Discussion started by: ujjwal27
9 Replies

6. UNIX for Dummies Questions & Answers

Sudo to delegate permission from non-root user to another non-root user

I've been through many threads before i decide to create a separate thread. I can't really find the solution to my (simple) problem. Here's what I'm trying to achieve: As "canar" user I want to run a command, let's say "/opt/ocaml/bin/ocaml" as "duck" user. The only to achieve this is to... (1 Reply)
Discussion started by: canar
1 Replies

7. Shell Programming and Scripting

Need to run a bash script that logs on as a non-root user and runs script as root

So I have a script that runs as a non-root user, lets say the username is 'xymon' . This script needs to log on to a remote system as a non-root user also and call up a bash script that runs another bash script as root. in short: user xymon on system A needs to run a file as root user and have... (2 Replies)
Discussion started by: damang111
2 Replies

8. Shell Programming and Scripting

Script to check Oracle tablespace

I'm new to unix script, and I need to check the tablespaces daily, here is the script(quite simple), but it does not work. Did I missed something ? Please guide me, Many thanks ! #!/bin/sh echo "ORA TABLEASPACE:" sqlplus system/oracle as sysdba; set pagesize 9999; set linesize 132; ... (5 Replies)
Discussion started by: dehetoxic
5 Replies

9. Shell Programming and Scripting

root user command in shell script execute as normal user

Hi All I have written one shell script for GPRS route add is given below named GPRSRouteSet.sh URL="www.google.com" VBURL="10.5.2.211" echo "Setting route for $URL for GPRS" URL_Address=`nslookup $URL|grep Address:|grep -v "#"|awk -F " " '{print $2}'|head -1` echo "Executing ... (3 Replies)
Discussion started by: mnmonu
3 Replies

10. Solaris

Where's the .profile environment config for root under Bourne shell?

I don't know where the environment config file for root user is in the Bourne shell on Solaris 10? Can you help me, or am I helpless???? (2 Replies)
Discussion started by: Joncamp
2 Replies
Login or Register to Ask a Question