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!

Exiting if conditions arent met

Status
Not open for further replies.

nogs

Technical User
Aug 3, 2001
89
GB
On a script Im running I want it to exit if two commands have no output.
These are the commands;

#LOG ALL USERS IDLE FOR 1 HOUR PLUS;
finger -i | awk '/hour/ && $7 > 0 { print $1 ; }' > /dev/null 2>&1

#LOG ALL USERS IDLE FOR 45 MINUTES PLUS;
finger -i | awk '/minutes/ && $7 > 45 { print $1 ; }' > /dev/null 2>&1

If neither have any output then exit.
If either or both have outout then carry on.
Whats the best way of doing this?

Nogs
 
You are removing both stdout and stderr. Try to just close stderr (done below) and moniter stdout with a variable to hold lines of output. As such:

OUTLINES=`
(
#LOG ALL USERS IDLE FOR 1 HOUR PLUS;
finger -i | awk '/hour/ && $7 > 0 { print $1 ; }' 2> /dev/null

#LOG ALL USERS IDLE FOR 45 MINUTES PLUS;
finger -i | awk '/minutes/ && $7 > 45 { print $1 ; }' 2> /dev/null) | wc -l`

if [ ${OUTLINES} -eq 0 ]; then
exit 0
fi

Good luck,
ND
 
use &&

if first runs ok it then runs the second

ls && ps would produce

file2 file2

PID TTY S TIME CMD
2270329 pts/1 I + 0:00.39 -ksh (ksh)
2271159 pts/2 S 0:00.10 -ksh (ksh)

but

dfdfdfdf && ps

would produce an error because the first failed the second wouldn't run.

you can also use || (run second only if first fails)

ls || ps

would only run the ls command

dfdfdfdf || ps

would only run the ps command

you could create 2 scripts, one containing your finger command and a sleep and the other that runs the first again

script1 && script 2

if script1 fails script2 runs


Mike

--
| Mike Nixon
| Unix Admin
| ----------------------------
 
Hi Nogs,

If you just want to check if there is no user idle for more tan 45 minutes :

finger -fw | awk '$3~/[:d]$/ || $#~/^[0-9]+$/ && $3>45 {exit 1}' | exit 0

If you need the list of users :

IDLE_USERS=$(finger -fw | awk '$3~/[:d]$/ || $#~/^[0-9]+$/ && $3>45')
[ -z "$IDLE_USERS" ] && exit 0 Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top