Solved

checking the Unix login id before executing Unix script codes

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remote Change Dates on AIX Automation 7 63
Issue with cURL on Windows 7 96
Shell script to extract rar files in severa directories 7 81
Shell Script on AIX 7 99
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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 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.

832 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