Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array variables 1

Status
Not open for further replies.

nogs

Technical User
Aug 3, 2001
89
GB
I have a number of working scripts in place which out put to files, resulting in multiple files (examples)all over the place -example script below. Instead of this I would like to output the variables to an array - how do I do this and then access each individual variable, also if the array is empty I want a message to say that.

#!/bin/sh

finger -i|grep minutes > minutes
#filter users for selected conditions
awk '$7 > 45 { print $1 ; }' minutes > newmins

#Log all 45+ minutes idle
for USERS in `cat newmins`
do

w -h $USERS | grep -v root > temp
awk '{ printf ("%-32s %-14s %-32s\n", $1,$4,$7); }' temp >> $LOGFILE
done

exit

NOGSs-)
 
Hi,

Your example script can be rewritten without using files and arrays :

#!/bin/sh

finger -i | awk '/minutes/ && $7 > 45 { print $1 ; }' | while read USERS
do
w -h $USERS | awk '! /root/ {printf ("%-32s %-14s %-32s\n", $1,$4,$7); }>> $LOGFILE
done

exit
Jean Pierre.
 
Aigles
I havnt tested this as yet - before I do how does it work?

You are outputting the command variables through | to
Will this work with different variables being written to \ in the same script?

Nogss-)
 
Hi,

\\ is just for continuation lines
finger -i | awk ...
is the same as :
finger -i | awk ...

| is a pipe. The stdout ouf the left part becomes the std in of the right part :
finger -i | awk ...
The result of finger is the input of awk.


finger -i | awk '/minutes/ && $7 > 45 { print $1 ; }'

select all lines from 'finger -i' which contains the word 'minutes' and where field 7 is greater than 45, and print the user name.

finger -i | awk '/minutes/ && $7 > 45 { print $1 ; }' | while read USERS
do
. . . . . .
done


This list of usernames becomes the input of the while loop.
The condition part of while reads this input line by line in the USERS variable. When there is no more username to read, getline return a status not equal to 0 and the loop is done.

w -h $USERS | awk '! /root/ {printf ("%-32s %-14s %-32s\n", $1,$4,$7); }>> $LOGFILE

For each username, the result of the w command is read by awk which prints the informations.

Jean Pierre.
 
Hi aigles
Thanks for the help that second post made it all seem very clear and will help me with my other scripts.

Is there any way I can add a bit to the script which if the output of

awk '/minutes/ && $7 > 45 { print $1 ; }' |
= 0 that I can output to the logfile a message saying that there were no users logged on idle?

Thanks in advance
Nogs
 
You can add a counter that is incremented into the while loop and tested after.


#!/bin/sh
counter=0
finger -i | awk '/minutes/ && $7 > 45 { print $1 ; }' | while read USERS
do
let counter=counter+1
w -h $USERS | awk '! /root/ {printf ("%-32s %-14s %-32s\n", $1,$4,$7); }>> $LOGFILE
done
[ $counter -eq 0 ] && echo "No user found" >> $LOGFILE
exit


The statement

[ $counter -eq 0 ] && echo "No user found" >> $LOGFILE

is a short hand for :

if [ $counter -eq 0 ]
then
echo "No user found" >> $LOGFILE
fi
Jean Pierre.
 
Thanks a lot Aigles [medal]

Nogs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top