Solved

checking the Unix login id before executing Unix script codes

Posted on 2010-09-19
4
386 Views
Last Modified: 2012-05-10
I currently has a Shell script (running in Bourne ie sh ) but there's been cases where a wrong
Unix id is used to execute this Shell script, so need a little enhancement that will check for
the login id (be it direct login id or "su - xxxxx" id where xxxxx is the desired Unix login id that
can be permitted to run the actual codes.

Kindly recode the following pseudo code with actual Bourne Shell script codes:

#/bin/sh
loginid=extract this from the 1st column of  "id" command's output
if loginid is not "xxxxx"
  then
     echo "You used incorrect id to run this script, pls use xxxxx"
     exit
  else
     run ...my existing codes .....
endif
0
Comment
Question by:sunhux
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:Dushyant Sharma
Dushyant Sharma earned 270 total points
ID: 33714039
Try this
loginid=`whoami`
if [ "$loginid" != "root" ]
then
      echo "you used incorrect id to run this script, please use 'root'"
else
      echo "welcome $loginid"
fi
0
 

Author Comment

by:sunhux
ID: 33714066

For the else section, does it permit multiple lines of codes, ie between
the "else" to the "fi", can I insert multiple lines as follows:

. . . . .
else
      echo "welcome $loginid"
      date
      /path/script1.sh
      another if_else check
      & exit if the condition is not met
fi
0
 
LVL 14

Assisted Solution

by:Dushyant Sharma
Dushyant Sharma earned 270 total points
ID: 33714077
yes you can write any number of lines between else and fi.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 230 total points
ID: 33715222

#/bin/sh
if [ `whoami` != xxxxx ]
then
   echo "You used incorrect id to run this script, pls use xxxxx"
   exit
fi

rest of your code

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now