This is a really simple script, but it underscores some key concepts here. Variable assignments, while statements, checking for null input, reading input from the terminal, comparing strings, etc.
I tried to annotate this script so you could follow along the major concepts. This is pretty basic, and so are the concepts, but I hope it gives you enough to build upon.
Bash Pitfalls helps, but you need to be fairly familiar with Bash to begin with. This was over my head. YMMV applies.
Opera renders this page correctly, Chrome not so well.
Thanks Joe for the skill command/commentary
Wayno
#!/usr/bin/bash
#
#
# By. W. Guerrini 04/20/2011 V 1.0
#
# simple script to zap a user
#
#
# configuring parameters
#
# note a very common mistake is using a $ to assign a string a value
# here we are just defaulting all the string variables to null
#
# note that there is NO white noise around the equal (=) signs a very
# common mistake! (been there done that)
#
person="" # name of person we want to zap (string/not null)
ans="" # answer received from user (string/not null)
ok2nuke="Y" # the answer we expect if it's okay to zap user
#
# the echo command does just that. it echos the contents between the
# quote marks to the terminal
#
echo "Displaying Logged in Users"
#
# issue the who command to see who is logged in
#
who
#
# The who command listed all the people logged into the system.
# Choose one to terminate
echo "What person do you want to zap?"
#
# read waits for the person to enter some text on stdin, and puts
# the contents into the variable NOTE: person NOT $person
#
read person
#
# we are checking for a null string
# the while statement will loop, until the person string is NOT null
#
# the -z checks to see if the string is null. Notice here we use
# $string name ($person vs person) for the comparison
# the brackets [] are required because we are doing various type of
# operators
#
# also note the semi-colon ; at then end of the while statement --
# yup that's needed.
#
# so this while statement says, while the string $person is null
# echo hey the string is null and re-read the input
#
# once the condition is satifisfied (non null string) the while exits (done)
while [ -z "$person" ];
do
echo "Null string. Not permitted. Enter a person to zap: "
read person
done
echo "$person is NOT null."
#
# display to the user, the name of the person we want to zap.
# note again we want to display the contents of the string $person
echo "confirm you want to zap User" $person "(Y/N)"
read ans
echo "the answer is: " $ans
#
#
# again another while loop - the answer can't be a null (nothing) string
# keep prompting and waiting till the answer is not a null
#
while [ -z "$ans" ];
do
echo "$ans String is null."
read ans
done
#
#
#
echo "$ans is NOT null."
#
# now check the answer and make sure it is a CAPITAL Y
# remember we set ok2nuke to "Y" above
#
#
#
# again in the if statement note the $string names,
# the equal signs, the brackets [], and the semi-colon ;
#
# If we got a capital "Y" then execute the if statement (skill commented out)
#
# we want to compare the strings, so we enclose them in quotes (")
# NOTE: you could also use "Y" instead of $ok2nuke but this way
# it's easier to change the value
#
if [ "$ans" = "$ok2nuke" ];
then
echo "$ans was okay to zap"
# sudo skill -n -u $person
# where n is the signal you want to send
# (-1 is a graceful hangup, -15 is a termination, -9 is an immediate kill)
echo "He's Dead, Jim!"
fi
#
#
# if we didn't get a capital "Y" for the answer, it just falls through and
# exits.
#
echo "we are finished"
Script execution looks something like:
nwayno@Homer:~$ sh zap.sh
Displaying Logged in Users
nwayno tty7 2011-04-19 18:15 (:0)
nwayno pts/6 2011-04-20 14:25 (:0.0)
donuts tty9 2011-04-20 14:45 (:2)
nwayno pts/10 2011-04-20 16:16 (:0.0)
What person do you want to zap?
Null string. Not permitted. Enter a person to zap:
donuts
$person is NOT null.
confirm you want to zap User donuts (Y/N)
Y
the answer is: Y
$ans is NOT null.
$ans was okay to zap
He's Dead, Jim!
we are finished
nwayno@Homer:~$