Solved

checking the Unix login id before executing Unix script codes

Posted on 2010-09-19
4
385 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:ddsh79
ddsh79 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:ddsh79
ddsh79 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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.:
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…

706 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

22 Experts available now in Live!

Get 1:1 Help Now