d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Shell Script (korn) Problem
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 12 2013 10:34pm
So i am working on a script which is supposed to alert you when someone is logged onto our schools server.

at the basic level, it is just a while ( sleep x ) loop with a nested for to go through a list of user names (args) and do various shit.

Anyways, the script works, but I am trying to find ways to improve it.

For example, I know you can run a script in the background with

Code
SCRIPTNAME &


I am wondering if there is a way to run the script in the background, but initialize that within the script, rather than at execution.



Another problem I am having is that our schools AT JOB service is not working. So we have to do a ghetto version of AT in our scripts

something like

Code
while ( sleep x ); do
timeCurrent=`date`
if [ timeCurrent -eq timeToExecute ]; then
#do shit
break
fi
done


I figured out a way to handle the date command (since checking every second won't be worth it, I will just have the user enter the time as a 4 digit number military time no spaces or colons), but I need to figure out how to run my script at that time, and then when it is done to exit the script so it doesn't run twice?

I was thinking something like

Code
while ( sleep x ); do
timeCurrent=`date`
if [ timeCurrent -eq timeToExecute ]; then
$0 $@
break
fi
done


but I am not sure if this would work at all etc.


My script is here:

Code
#!/usr/dt/bin/dtksh

#This script is intended to alert the user when anyone from a list of users (args to the script) is online on admiral.
#The script optionally accepts a message to be sent to the user when someone is by utilizing the -m option.
#The script optionally accepts a date to run the script at. However there is a limiate due to at jobs not working correctly at the moment (outlined below)
#The date arg entered must be in the form of something like: Tue Mar 1200 2013

USAGE="Correct usage: alert -w [optional date] -m [optional message] [user list]"

if [ $# -lt 1 ]; then
   echo "Must enter at least 1 user to search for" && exit 1
fi

while getopts w:m: var
do
   case $var in
   w)      wflag=1
           wval="$OPTARG";;
   m)      mflag=1
           mval="$OPTARG";;
   ?)      echo $USAGE && exit 0
   esac
done

shift $(($OPTIND -1))

while ( sleep 66 ); do
   for i; do
       id $i > /dev/null
       if [ $? -eq 0 ]; then
           finger | grep ^$i > /dev/null
           if [ $? -eq 0 ]; then
               finger | grep ^$USER
               if [ $? -eq 0 ]; then
                   banner $i IS ON
                   if [ ! -z "$mflag" ]; then
                       echo "$mval" | write $USER
                   fi
               else banner "$i IS ON" | elm -s $mval $USER
               fi
               if [ $# -eq 1 ]; then
                   exit 0
               fi
               set $(echo $@ | sed "s/$i//")
           fi
       else echo "$i is not a user. Please re-run the script with a correct user name [Consider searching for the username with the id command]" && echo "\n$USAGE" && exit 0
       fi
   done
done


don't make fun of me if it is too many lines etc I am basically completely newb at shell scripting

This post was edited by Eep on Mar 12 2013 10:50pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 13 2013 05:53am
uhm, maybe you'd like to have something like this?

Code
#!/bin/sh
oldusers="Starting..."
while [ 1 ]
do
 newusers=`users`
 if [ "$newusers" != "$oldusers" ]
   then
   echo $newusers
   oldusers="$newusers"
 fi  
 sleep 2
done
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 13 2013 06:49pm
major derp moment fixed it
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll