Operating System - HP-UX
1833178 Members
2831 Online
110051 Solutions
New Discussion

Re: help with script break down ..

 
someone_4
Honored Contributor

help with script break down ..

Hello everyone .. I got this script that someone posted for me on another post. I am adding it to a bigger script and i am playing with it to get the data format that I want. Now I have decided that instead of just asking for script help that I want to actully understand how and why the scripts work. I think in the long run this will help me learn more. So here we go .. I need help understanding why this part is doing what it does. ..
#### I have
last | grep -v ftp |grep -v wtmp |awk ' /still logged/ { print $1 ".*still logged in"; next } { print $1 }' |sort -u | while read name
do
last | grep -v ftp | grep -v wtmp | grep -c "^$name" | read count
echo "$name \t$count"
done

# that gives me this out put ..
5
aford.*still logged in 1
rbiersch.*still logged in 1
rleon 2
rleon.*still logged in 1

# the only part I dont understand is where is the 5 comming from? And where is that outputting from without an echo?

I changed the first line to
last | grep -v ftp |grep -v wtmp | grep [A-z]|
And when I ran it the 5 went away. Why did the 5 go away when I added the grep [A-z]?

Thanks,

Richard -
"Give a man a fish feed him for a day,teach a man to fish feed him for a lifetime"
7 REPLIES 7
someone_4
Honored Contributor

Re: help with script break down ..

Opps here is the first script:

last | grep -v ftp |grep -v wtmp |awk ' /still logged/ { print $1 ".*still logged in"; next } { print $1 }' |sort -u | while read name
do
last | grep -v ftp | grep -v wtmp | grep -c "^$name" | read count
echo "$name \t$count"
done
Craig Rants
Honored Contributor

Re: help with script break down ..

The 5 comes from the grep -c which is count the lines out output, therefore when you removed the -c option in your addition, the 5 would disappear.

Hope this is what you were looking for,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
someone_4
Honored Contributor

Re: help with script break down ..

I thought it was the -c option but I never took that out. I only modified the fist last not the 2nd one.

Richard
Craig Rants
Honored Contributor

Re: help with script break down ..

If you are ever curious about what a script does internally, run it with

#!/bin/sh -x

then when you call it from the command line do

# ./script.sh > /tmp/output 2>&1

You'll get a lot of good information out of that logfile.

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Craig Rants
Honored Contributor

Re: help with script break down ..

This is what I would use instead,

for i in `last | grep -v ftp |grep -v wtmp | awk '/still logged/{print $1}' | so
rt -u`
do
last $i | wc -l | read count
echo $i $count
done

A little clean, easier to understand, and about twice as fast. Just me though.

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Craig Rants
Honored Contributor

Re: help with script break down ..

Opps, one correction

last $i | grep still | wc -l | read count

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Robin Wakefield
Honored Contributor

Re: help with script break down ..

Hi Richard,

The reason your script is behaving like this is because you're not grepping out blank lines. Therefore, your first $name is "", which is obviously matched on every line in the second part of your script. Just add an extra grep to fix it:

==================================
last | grep -v ^$ | grep -v ftp |grep -v wtmp | awk ' /still logged/ { print $1 ".*still logged in"; next } { print $1 }' |sort -u | while read name
do
last | grep -v ftp | grep -v wtmp | grep -c "^$name" | read count
echo "$name \t$count"
done
===================================

I would have written the script slightly differently, using uniq to get the count:

last | grep -v -e ^$ -e wtmp -e ftp |
awk ' /still logged/ { print $1 " still logged in"; next } { print $1 }' |
sort | uniq -c

Rgds, Robin.