[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

checking the Unix login id before executing Unix script codes

Posted on 2010-09-19
4
Medium Priority
?
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:Dushyant Sharma
Dushyant Sharma earned 1080 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 1080 total points
ID: 33714077
yes you can write any number of lines between else and fi.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 920 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…
Suggested Courses

649 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