Solved

checking the Unix login id before executing Unix script codes

Posted on 2010-09-19
4
388 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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