Newbie help with Shell scripts (using Korn shell)

Newbie help with Shell scripts (using Korn shell)

Post by Mila » Wed, 07 Nov 2001 08:28:36



Hi,

I am fairly new to UNIX, but keen to learn. I have a few scripts I am trying
to write and was wondering if anyone could be of some assistance?

Script 1.
I am trying to display/list the top 15 users of HDD space, and if they
exceed the quota (5000KB) then display "Yes" next to their name, and "NO" if
they haven't.

Script 2.
List any inactive users or users that haven't logged on for 3 months.

Script 3.
Make a script called "IP". Then if I run IP device_name  (where device_name
is a device on the network) it returns the IP address of the device.

I have had a good go at the 3 scripts, but not having too much luck. Any
help would be greatly appreciated.

Milan.

 
 
 

Newbie help with Shell scripts (using Korn shell)

Post by Richard Howlet » Wed, 07 Nov 2001 08:38:49



> Hi,

> I am fairly new to UNIX, but keen to learn. I have a few scripts I am trying
> to write and was wondering if anyone could be of some assistance?

> Script 1.
> I am trying to display/list the top 15 users of HDD space, and if they
> exceed the quota (5000KB) then display "Yes" next to their name, and "NO" if
> they haven't.

> Script 2.
> List any inactive users or users that haven't logged on for 3 months.

> Script 3.
> Make a script called "IP". Then if I run IP device_name  (where device_name
> is a device on the network) it returns the IP address of the device.

> I have had a good go at the 3 scripts, but not having too much luck. Any
> help would be greatly appreciated.

How about posting what you have already attempted from your homework
assignment?

--
Richard Howlett



 
 
 

Newbie help with Shell scripts (using Korn shell)

Post by Mila » Wed, 07 Nov 2001 14:19:15


Quote:> > Script 1.
> > I am trying to display/list the top 15 users of HDD space, and if they
> > exceed the quota (5000KB) then display "Yes" next to their name, and
"NO" if
> > they haven't.

> How about posting what you have already attempted from your homework
> assignment?

Sorry about that. OK starting off with the first one:

clear
tabs 2,33,45,60,75   #set tab positions

echo "Top 15 USERS of HDD (Students)"
echo "\tUser Name \tFull Name \tHDD Usage (in KB) \tEXCEEDED QUOTA? "

du -s /home/student/* | while read line  #process only users in
/home/student directory

do
     set -- $line
     hddusage=$1
     userid=`echo $2|cut -d"/" -f4`
     fullname=`grep "^$userid" /etc/passwd | cut -d":" -f5`
     if test "$1" -gt 5000
     then
          exquota="Yes"
     else
          exquota="No"
     fi
     echo "\t$hddusage \t$userid \t$fullname \t$exquota " >> student_list
done

sort -r -n student_list | tail +15 > student_top15_list   #Sorts students by
descending order of HDD usage

cat student_top15_list | while read line
do
     set -- $line
     hddusage=$1
     userid=$2
     fullname=$3
     exquota=$4
     echo "\t$userid \t$fullname \t$hddusage \t$exquota "
done

 
 
 

Newbie help with Shell scripts (using Korn shell)

Post by Richard Howlet » Thu, 08 Nov 2001 09:02:00



> > > Script 1.
> > > I am trying to display/list the top 15 users of HDD space, and if they
> > > exceed the quota (5000KB) then display "Yes" next to their name, and
> "NO" if
> > > they haven't.

> > How about posting what you have already attempted from your homework
> > assignment?

> Sorry about that. OK starting off with the first one:

> clear
> tabs 2,33,45,60,75   #set tab positions

> echo "Top 15 USERS of HDD (Students)"
> echo "\tUser Name \tFull Name \tHDD Usage (in KB) \tEXCEEDED QUOTA? "

> du -s /home/student/* | while read line  #process only users in
> /home/student directory

> do
>      set -- $line
>      hddusage=$1
>      userid=`echo $2|cut -d"/" -f4`
>      fullname=`grep "^$userid" /etc/passwd | cut -d":" -f5`
>      if test "$1" -gt 5000
>      then
>           exquota="Yes"
>      else
>           exquota="No"
>      fi
>      echo "\t$hddusage \t$userid \t$fullname \t$exquota " >> student_list
> done

> sort -r -n student_list | tail +15 > student_top15_list   #Sorts students by
> descending order of HDD usage

> cat student_top15_list | while read line
> do
>      set -- $line
>      hddusage=$1
>      userid=$2
>      fullname=$3
>      exquota=$4
>      echo "\t$userid \t$fullname \t$hddusage \t$exquota "
> done

You aren't doing too badly. This is a pretty reasonable stab at the problem
- at least you have had a stab at it. Without completely changing what you
have already there are a few things that stand out.

1. "tabs" doesn't work as I'd expect on my system. You may be better off
using printf to get your fields tabulated nicely. printf will also allow you
to right justify the usage figure.
2. You will need the -k option of du to get your usage value in kb.
3. "read" can read more than one variable so I would change your read
statement to something like ...... while read hddusage homedir .....
4. Which means the set and next line down are not needed.
5. The assignment of userid can now be made much simpler with basename or in
ksh or bash ${homedir##*/}
man basename
6. The test line should now be written as:  if [ $hddusage -gt 5000 ]
7. Rather than using >> to add to the file for each line processed redirect
stdout after the "done" statement. Eg: done > student_list
8. You need to use head instead of tail to get the top 15
9. If you don't need the intermediate file you could continue the pipeline
to the sort and therefore remove the reference to student_file in the sort
command. Furthermore this pipeline could also include the while read of your
output stage making: done | sort -rn |head -15 |while read line ; do
10. You should put the variable that can have multiple words ($fullname) at
the end so your echo becomes: echo "\t$hddusage \t$userid \t$exquota
\t$fullname"
11. Swap fullname and exquota around in your output loop, put a "shift 3"
after exquota and assign fullname using: fullname=$*

I am by no means the cleanest coder here but here is a working example to
show the solution I came up with:

clear
echo "Top 15 USERS of HDD (Students)"
echo "User Name  Full Name                  Usage  EXCEEDED QUOTA?"
du -sk /home/student/* |\
while read hddusage homedir ; do
    userid=${homedir##*/}
    exquota=No
    if [ $hddusage -gt 5000 ] ; then
        exquota=Yes
    fi
    echo "$hddusage:$userid:$exquota:\
`grep "^$userid" /etc/passwd |cut -d":" -f5`"
done |sort -rn |head -15 |\
awk -F: '{printf "%-10s %-25s %6s  %-3s\n",$2,$4,$1,$3}'

--
Richard Howlett


 
 
 

Newbie help with Shell scripts (using Korn shell)

Post by Mila » Sat, 10 Nov 2001 12:38:25


Thanks very much Richard, I had not used "awk" before so I had to go read up
on it a little before I continued.

OK, script 2 is supposed to show a list of users who have not logged on, or
who haven't logged on for 3 months or more.

Using the header:

INACTIVE USERS (Students) as at <todays date>
User Name        Full Name                Last Logged on

here is what I have written, but cannot seem to get any users from:

clear
date '+INACTIVE USERS (Students) as at %d %b %y'
echo "User Name Full Name  Last Logged on"

finger /home/students/* |tail +3 |head -1 |\
while read line ; do
     userid=${homedir##*/}
     currentmonth='date "+ %m"'
     lastonmonthnumber=0
     if [ $1 == "Never" ] ; then
          lastonmonth=Never
     else
          lastonmonth=$3
     fi
     if [ $4 == "Jan" ] ; then
          lastonmonthnumber=4
     elif [ $4 == "Feb"  ] ; then
          lastonmonthnumber=5
     elif [ $4 == "Mar" ] ; then
          lastonmonthnumber=6
     elif [ $4 == "Apr" ] ; then
          lastonmonthnumber=7
     elif [ $4 == "May" ] ; then
          lastonmonthnumber=8
     elif [ $4 == "Jun" ] ; then
          lastonmonthnumber=9
     elif [ $4 == "Jul" ] ; then
          lastonmonthnumber=10
     elif [ $4 == "Aug" ] ; then
          lastonmonthnumber=11
     elif [ $4 == "Sep" ] ; then
          lastonmonthnumber=12
     fi
     if [ $lastonmonthnumber -le $currentmonth ] ; then
          echo "$userid:$lastonmonth:\
          `grep "^$userid" /etc/passwd | cut -d":" -f5`"
          awk -F: '{printf "%-12s %-24s %-25s\n",$1,$3,$2}'
     fi
done

 
 
 

Newbie help with Shell scripts (using Korn shell)

Post by Chris F.A. Johnso » Sat, 10 Nov 2001 13:44:31



> Thanks very much Richard, I had not used "awk" before so I had to go read up
> on it a little before I continued.

> OK, script 2 is supposed to show a list of users who have not logged on, or
> who haven't logged on for 3 months or more.

> Using the header:

> INACTIVE USERS (Students) as at <todays date>
> User Name        Full Name                Last Logged on

> here is what I have written, but cannot seem to get any users from:

At what point does it not work?

Quote:> clear
> date '+INACTIVE USERS (Students) as at %d %b %y'
> echo "User Name Full Name  Last Logged on"

> finger /home/students/* |tail +3 |head -1 |\

Did you get the output you want from this when you tried it by itself?

How many lines do you think "head -1" will give you?
Will that line give you the information you need?

Quote:> while read line ; do
>      userid=${homedir##*/}

Where did you assign a value to $homedir?

Quote:>      currentmonth='date "+ %m"'

Move this outside the loop. You only need to do it once.

Quote:>      lastonmonthnumber=0
>      if [ $1 == "Never" ] ; then
>           lastonmonth=Never
>      else
>           lastonmonth=$3

See my next note.

Quote:>      fi
>      if [ $4 == "Jan" ] ; then

Is the month in the 3rd or the 4th field?

Quote:>           lastonmonthnumber=4

Why is "Jan" number 4?

Quote:>      elif [ $4 == "Feb"  ] ; then
>           lastonmonthnumber=5
>      elif [ $4 == "Mar" ] ; then
>           lastonmonthnumber=6
>      elif [ $4 == "Apr" ] ; then
>           lastonmonthnumber=7
>      elif [ $4 == "May" ] ; then
>           lastonmonthnumber=8
>      elif [ $4 == "Jun" ] ; then
>           lastonmonthnumber=9
>      elif [ $4 == "Jul" ] ; then
>           lastonmonthnumber=10
>      elif [ $4 == "Aug" ] ; then
>           lastonmonthnumber=11
>      elif [ $4 == "Sep" ] ; then
>           lastonmonthnumber=12
>      fi
>      if [ $lastonmonthnumber -le $currentmonth ] ; then
>           echo "$userid:$lastonmonth:\

Where did you assign a value to $userid?

Quote:>           `grep "^$userid" /etc/passwd | cut -d":" -f5`"
>           awk -F: '{printf "%-12s %-24s %-25s\n",$1,$3,$2}'
>      fi
> done

Try soemthing like this:

currentmonth=`date +%m`

for dir in /home/students/*
do
    finger `basename $dir` | {
      read _ userid _ username ### underscore '_' is a throw-away
      read _
      read ever _ day lastonmonth date time

      if [ "$lastonmonth" != "Never" }
      then
          ### do your calculations here

      fi
    }
done

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Newbie help with Shell scripts (using Korn shell)

Post by Richard Howlet » Sun, 11 Nov 2001 06:45:45



> Thanks very much Richard, I had not used "awk" before so I had to go read up
> on it a little before I continued.

You weren't hoping to pass my effort off as yours were you? You may get
caught!

Also you sent an email with exactly the same message? I read the newsgroup
every day so there was no need to email me.

--
Richard Howlett


 
 
 

1. Convert Bash shell script to Korn shell script

The following bash script works fine to delete all files that are
older than $1 minutes. To execute this script,
bash cleanup +10

find /mypath -type f -cmin $1 -exec rm -f {} \;

Unfortunately, now I just realize I need to make it work in Korn
Shell. Even I
change the header from #!/bin/bash to #!/usr/bin/ksh, and execute the
script by ksh cleanup +10, it still have
different errors:

cleanup[9]: -cmin:  not found.
cleanup[10]: -type:  not found.

Looks like it doesn't work anymore. I think I need to re-write the
Korn Shell script that do the task.
I tried to search for equivalent Korn shell commands but unsuccessful.

Please help. thanks!!

2. xf86config-XFree86-3.1?????

3. Korn shell script help - Newbie stumbling along....

4. How to boot FreeBSD 4.4 on Asus A7V266 Motherboard

5. best korn shell resources and is there a korn shell faq

6. Ghostview PS --> EPS

7. c-shell script won't run from korn-shell / SETUID

8. Argh -- AHA-2940 bootdisk not working?

9. How to attach file to EMAIL MESSAGE within shell script(Korn shell)

10. Pointers/Help with a basic shell script (Shell Newbie alert)

11. help help help to write a script in korn shell

12. Korn Shell Script Newbie Question

13. Shell Script Help (C-Shell Script)