Question

Korn Shell arrays

Asked by: j8vy

Good morning all,

I have a script that I wrote this morning that I can not figure out how to finish. This script will be used to kick a user out of my application. My goal is to prompt the user for the username of the person that they wish to kick off, retain that username in a variable, load in the userid associated with that username, and then use the userid to remove them from the system. My problem is, I want to propmt the user for the username, but the only way that I can kick someone out of the application is via the userid.

I am trying to take the following output:

UID  user  The rest is really useless information for this purpose ...
365  jx5h  T00A1AD.opr.statefarm.org  ECS_GUI  03/13/03 08:41:49:389920
447  jx5h  T0093E5.opr.statefarm.org  ECS_GUI  03/17/03 18:12:23:268080
450  c8zg  T000FDA.opr.statefarm.org  ECS_GUI  03/18/03 07:18:17:484342

retrieved with the following command:

ecs.ctl -U $euser -P $epass -C GUI_Server -M sf40ctm1 -cmdstr PGUI |grep -v Open |grep -v have |grep -v The

and feed the uid into the following command to remove them from the app:

ecs.ctl -U $euser -P $epass -C GUI_Server -M sf40ctm1 -cmdstr KICK $uid

Note: Currently I am printing some info to the user about the program, prompting
them for a username, and verifying that the username entered is currently logged
in to the application, and futher giving them only three attempts to enter a valid
username.

The following is what I have so far:

#!/usr/bin/ksh

logo="Remove ECS Users"

prmpt() {
clear
print "\n\t\t\t+----------------------------+"
print "\t\t\t|      ${logo}      |"
print "\t\t\t|\t - $(hostname) -\t     |"
print "\t\t\t+----------------------------+"
print "\n\n\n\n\n\tThis program will allow you to 'Kick' a user off of ECS."
print "    Please do not kick a user off of ECS without notifying them first!"
print "\n\tEnter the the userid of the person to be removed below."
print "\t        The userid will be the same as the SFID."
print "\nUserid -> \c"
read userid
}

pgui_out="/opt/incontrol/sf_scripts/admin/j8vy/pgui_rpt.txt"
euser=$(awk '{print $4}' /opt/incontrol/sf_scripts/xfile)
epass=$(awk '{print $5}' /opt/incontrol/sf_scripts/xfile)

set -A id $(ecs.ctl -U $euser -P $epass -C GUI_Server -M sf40ctm1 -cmdstr PGUI |grep -v The |grep -v have |grep -v Open |awk '{print $2}')

prmpt
cnt="-1"
while [[ ${userid} != ${id} ]]; do
if [[ ${cnt} < 2 ]] && [[ ${cnt} > "-1" ]]; then
   print "\nOops ... That user is not currently logged in!"
   print "Please enter a valid userid."
   sleep 3
  prmpt
elif [[ ${cnt} = 2 ]]; then
   print "\nYou have reached the maximum number or retries!"
   print "Please try again at a later time.\n"
   exit 1
fi
 let cnt+=1
done

Your thoughts are appreciated.
P.S. I would prefer to keep this in Korn Shell if it is possible. Thanks.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-03-21 at 02:49:42ID20558487
Tags

kick

Topic

Unix Systems Programming

Participating Experts
3
Points
100
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Switching to Korn shell
    My user account is placed in the C-shell by default. I would like to have the Korn shell as my default shell. What file(s) need(s) to be modified and what commands are used?
  2. Korn Shell Script Problem!
    I have a problem I have to write a Korn Shell script that takes its input from a flat file and then runs as a batch job every 5am. My questions are how do I get the script to take input from a flat file I have never written a script that way? And second How do I schedual a jo...
  3. Pass a variable to awk inside a Korn shell script.
    I'm parsing a logfile for referring pages. I want to structure my script such that I can pass a variable to awk to tell it which URL to use as the referring page. I can't figure out how to pass the variable. I'm using the Korn shell. Here's the script: egrep ' 20[0-9] | 3...
  4. korn shell scripting
    I'd like to open a file and store the rows in an array or iterate through the file line by line? can someone show me how.. it must korn shell and not Perl etc.
  5. I want to write a ksh (korn shell) script to show disk usa…
    I want to write a ksh (korn shell) script to show disk usage per user. The script should take in a single command-line argument--the directory name, and display the usage in MB per user. I have managed to get the files listings in the directory and then check who the owner ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: j8vyPosted on 2003-03-21 at 03:11:02ID: 8179983

I am sure that I need to assign the uid to an array.

I did this for the username so that I could verify if the username being entered was a valid user.

You will notice this in my code:

set -A id $(ecs.ctl -U $euser -P $epass -C GUI_Server -M sf40ctm1 -cmdstr PGUI |grep -v The |grep -v have |grep -v Open |awk '{print $2}')

I know that I can also do this for the uid,
like this:
set -A uid $(ecs.ctl -U $euser -P $epass -C GUI_Server -M sf40ctm1 -cmdstr PGUI |grep -v The |grep -v have |grep -v Open |awk '{print $1}')

but my question is, how do I relate the uid and the username if they are in seperate arrays.

Thanks again.

 

by: j8vyPosted on 2003-03-21 at 04:49:47ID: 8180436

I also just realized that in my 'while loop' test to see if the user is currently logged in:

while [[ ${userid} != ${id} ]]; do

using:

set -A id $(ecs.ctl -U $euser -P $epass -C GUI_Server -M sf40ctm1 -cmdstr PGUI |grep -v The |grep -v have |grep -v Open |awk '{print $2}')

to get the userid

The while loop is only checking the first occurrence in the 'id' array field. I.e. The output would be:

365  jx5h  T00A1AD.opr.statefarm.org  ECS_GUI  03/13/03 08:41:49:389920
447  jx5h  T0093E5.opr.statefarm.org  ECS_GUI  03/17/03 18:12:23:268080
450  c8zg  T000FDA.opr.statefarm.org  ECS_GUI  03/18/03 07:18:17:484342

and if I try user 'jx5h' the test will pass, but if I try to use 'c8zg' the test will fail every time!

I know that the 'id' array contains all of the data, but somehow I am not reading all of that data in my 'while' test.

 

by: HamdyHassanPosted on 2003-03-21 at 11:49:07ID: 8183222

Why array?

list the current users to temp file /tmp/current_users.txt
grep on this file
if grep sucess then you found it


let me know if you agree so I can give you code example

 

by: ellesdPosted on 2003-03-21 at 11:54:21ID: 8183251

I'm sure there are many ways to do it, but off the top of my head, try something similar to:
  let idx=1
  while [ "$idx" -lt "${#id[*]}" ]
  do
    if [ "$userid" == "$id[$idx]" ]
    then
      found=1
      break
    else
      found=0
      let idx=$idx+6
    fi
done
if [ "$found" -eq 0 ]
then
   kick them off
else
   try again
fi

Set idx equal to the element of the array you are checking; they start numbering at zero.  Add the number of fields in each line to idx to check the next value you are looking for; the same field in each line will have the same offset equal to the number of fields.  You should be able incorporate this into your code.  If you need more explanation, let me know.

 

by: ahoffmannPosted on 2003-03-21 at 14:32:32ID: 8184248

assuming your ecw.ctl pruduces a line like:
  365  jx5h  T00A1AD.opr.statefarm.org  ECS_GUI  03/13/03
then do following (sh syntax:
user=`ecw.ctl ... |awk '{print $2}'`
uid=awk -F: '/^'$uid'/{print $3}' /etc/passwd

 

by: j8vyPosted on 2003-03-24 at 22:01:32ID: 8200570

I'm sorry for the long delay in my response, but each of your post have made me start thinking.

I think that I am going to approach this from a different angle, so I may need more help with this later on. If the question varies too much, I will post a new question.

-- HamdyHassan's post made me think about the fact that I should do this differently entirely. I will ask more questions later.

Thanks.

 

by: j8vyPosted on 2003-03-25 at 04:13:55ID: 8202134

Thanks for everyones help with this.

I have a question that applies to this program, but I will post as a new question if so desired.

-- I need to create a multi-column output of my usernames.
I will generating the list from the following command:

$(ecs.ctl -U $euser -P $epass -C GUI_Server -M sf40ctm1 -cmdstr PGUI |grep -v The |grep -v have |grep -v Open |awk '{print $2}')

which will generate something like:
j8vy
c4db
h4ds
...
...

This list will be very long at times, so I would like to display all of these usernames like:

j8vy  bdfg  g6eg
c4db  c4ka  jdse
h4ds  dsfg  dfgs
....  ....  ....
....  ....  ....

current settings are:

$COLUMNS=80
$LINES=24

and I would like to just space the columns evenly across the screen. I am thinking that maybe 20 lines and however many columns (2 spaces between) will fit. I am thinking that I should set the columns up first, then just let the lines go as long as they need to, but I am unsure of how this procedure is accomplished. There are not going to be more than 100 - 150 users on the system at anytime, so 30 lines should be more than enough.

I plan to just use the "resize" command to resize the
window to whatever size it should be to display the proper
length (however many lines we come up with)

Your thoughts are appreciated.

 

by: ahoffmannPosted on 2003-03-25 at 06:50:20ID: 8203128

# replace you last awk with:
awk '{printf" %s",$2;x++;if(x%3==0){printf"\n"}}'

 

by: HamdyHassanPosted on 2003-03-25 at 07:45:10ID: 8203568

Unix offer command for this multi columns issue
"pr" command

for example
$(ecs.ctl -U $euser -P $epass -C GUI_Server -M sf40ctm1 -cmdstr PGUI |grep -v The |grep -v have |grep -v Open |awk '{print $2}')
|   pr -3    

will make three columns



take a look at man page for more details


 

by: j8vyPosted on 2003-03-25 at 10:54:22ID: 8204908

... and there I was sitting here looking like a dumbass, trying to make something harder than it was.

I would like to personally say thanks to everyone for their help and assistance, but I feel that I should give the points to HamdyHassan.

HamdyHassan, you have helped me before, and you have helped me here. If you will recall, I had asked you earlier not to post solutions, but to just tip me in the right direction, because my purpose of coming here is not to have you do the work, but to learn from this experience and take that knowlege with me. I believe that it has already been proven that you guys know how to do this stuff!!

Which brings me to my point, I was headed in a totally different direction when I started this program, but after reading HamdyHassan's first post the other day, I literally opened up my program in vi editor and held the 'd' key!! Yep..., I deleted the whole damn thing and started over, and the following is the result (a program that works great, impressed my teammates, and will save us time in the future:

Here is the code:
#!/usr/bin/ksh
#-----------------------------------------------------------------------------
# Program       : kick_ecsuser.ksh
# Purpose       : Print a detailed list of all users logged into ECS and then
#                 "KICK" those users out of the ECS application based on
#                 their alias.
# Contact       : Control-M Support PM
# Author      : matthew.vaden.j8vy@statefarm.com
# Dependencies      : This program has been tested on ECS ver 6.0.03 w/ the
#                 the following patches:
#                 ECS: 6.0.03; Oracle 8.1.7.4
#                 FD: 4333, 4494, & 4646
#                 It is NOT currently known to work on any higher versions,
#                 and futhermore will NOT work w/o the listed dependencies.
#            : Rc values:
#              rc=0 -> Program completed successfully
#                 rc=1 -> Incorrect userid was entered
#                 rc=2 -> "KICK" command did not execute successfully
#                 rc=3 -> User quit out of program
# Warranty      : This program comes without any warranty express or implied
#                 and is NOT part of the original install of Control-M and
#                 therefore is NOT supported by BMC software. For support
#                 with this program, please contact Control-M Support PM.
# Maintenance   :
# Date          Description
# --------      --------------------------------------------------------------
# 03/19/03      Initial testing phase and planning
# 03/25/03      Final Code development
#-----------------------------------------------------------------------------

#----------------------------------
# Step 1
# Define local vars
#----------------------------------
host=$(hostname)
logo="Remove ECS Users"
pgui_out="/opt/incontrol/sf_scripts/admin/j8vy/pgui_rpt.txt"
kick_out="/opt/incontrol/sf_scripts/admin/j8vy/kick_rpt.txt"
euser=$(awk '{print $4}' /opt/incontrol/sf_scripts/xfile)
epass=$(awk '{print $5}' /opt/incontrol/sf_scripts/xfile)

#----------------------------------
# Step 2
# Define local functions
#----------------------------------
prmpt() {
clear
print "\n\t\t\t+----------------------------+"
print "\t\t\t|      ${logo}      |"
print "\t\t\t|    ECS Server: $host    |"
print "\t\t\t+----------------------------+"
print "\n\n\n\n\n\tThis program will allow you to 'Kick' a user off of ECS."
print "    Please do not kick a user off of ECS without notifying them first!"
print " First enter your answer, then enter the sfid of the person to be removed below.\n"
}

#----------------------------------
# Step 3
# Main body
#----------------------------------
while true; do
prmpt
print "    Would you like to see a list of users currently logged in to ECS?"
print "\nEnter response (y/n/q): \c"
read ans
case ${ans} in
#----------------------------------
# Step 3b
# First step(if $ans is 'y' or 'Y')
#----------------------------------
y|Y)
    resize -s 28 80 >/dev/null
ecs.ctl -U $euser -P $epass -C GUI_Server -M ${host} -cmdstr "PGUI" |grep -v The |grep -v have |grep -v Open |awk '{print $2}' |pr -tr -7

print "\nEnter the sfid -> \c"
read userid

ecs.ctl -U $euser -P $epass -C GUI_Server -M ${host} -cmdstr "PGUI" |grep -v The |grep -v have |grep -v Open > ${pgui_out}

cat ${pgui_out} |while read line; do
    uid=$(echo ${line} |awk '{print $1}')
    sfid=$(echo ${line} |awk '{print $2}')
  if [[ ${userid} = ${sfid} ]]; then
      for id in ${uid}; do
         ecs.ctl -U $euser -P $epass -C GUI_Server -M ${host} -cmdstr "KICK ${id}" > ${kick_out}
      done
  fi
done
 if [[ -a ${kick_out} ]]; then
  cat ${kick_out} |grep not >/dev/null
    if [[ ${?} = 1 ]]; then
       print "\nThe following user has been 'kicked' out of ECS:"
       print "SFID: ${userid}"
       print "\nJust restart the program to kick off another user."
      rm -f ${pgui_out} >/dev/null
      rm -f ${kick_out} >/dev/null
      exit 0
    elif [[ ${?} = 0 ]]; then
       print "\nNo users were kicked out of ECS!"
       print "SFID: ${userid} is not currently logged in to ECS."
       print "\nHint: Choose 'y' when asked if you wish to see the users currently logged into ECS.\n"
      rm -f ${pgui_out} >/dev/null
      rm -f ${kick_out} >/dev/null
      exit 1
    fi
 else
   print "\nNo output was generated."
   print "It appears that the 'KICK' command did not execute."
   print "Please contact Control-M Support PM if problems persist."
   exit 2
 fi
break ;;

#----------------------------------
# Step 3c
# 2nd  step (if $ans is 'n' or 'N')
#----------------------------------
n|N)
    print "\nEnter the sfid -> \c"
    read userid

ecs.ctl -U $euser -P $epass -C GUI_Server -M ${host} -cmdstr "PGUI" |grep -v The |grep -v have |grep -v open > ${pgui_out}

cat ${pgui_out} |while read line; do
    uid=$(echo ${line} |awk '{print $1}')
    sfid=$(echo ${line} |awk '{print $2}')
  if [[ ${userid} = ${sfid} ]]; then
      for id in ${uid}; do
         ecs.ctl -U $euser -P $epass -C GUI_Server -M ${host} -cmdstr "KICK ${id}" > ${kick_out}
      done
  fi
done
 if [[ -a ${kick_out} ]]; then
  cat ${kick_out} |grep not >/dev/null
    if [[ ${?} = 1 ]]; then
       print "\nThe following user has been 'kicked' out of ECS:"
       print "SFID: ${userid}"
       print "\nJust restart the program to kick off another user."
      rm -f ${pgui_out} >/dev/null
      rm -f ${kick_out} >/dev/null
      exit 0
    elif [[ ${?} = 0 ]]; then
       print "\nNo users were kicked out of ECS!"
       print "SFID: ${userid} is not currently logged in to ECS."
       print "\nHint: Choose 'y' when asked if you wish to see the users currently logged into ECS.\n"
      rm -f ${pgui_out} >/dev/null
      rm -f ${kick_out} >/dev/null
      exit 1
    fi
 else
   print "\nNo output was generated."
   print "It appears that the 'KICK' command did not execute."
   print "Please contact Control-M Support PM if problems persist."
   exit 2
 fi
break ;;

q|Q)
   exit 3
   break ;;

*)
  print "\nYour answer can not be blank!"
  print "You must enter a valid user"
  print "Please try again ...\n"
  sleep 4
  ;;
esac
done
 

... thanks for the help and inspiration.

 

by: j8vyPosted on 2003-03-25 at 10:56:09ID: 8204922

Thanks man ... You can answer my questions anytime!

 

by: HamdyHassanPosted on 2003-03-26 at 07:19:02ID: 8211014

wow that script looks good.  Congratualtion j8vy

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...