Please help to debug a small shell script (maybe AWK problem)?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help to debug a small shell script (maybe AWK problem)?
# 1  
Old 10-15-2008
Please help to debug a small shell script (maybe AWK problem)?

Hi Buddies,
The following is shell scripts which was borrowed from linux box for load average check. it runs good.
(this structure is simple, when load average is too high, it will send alert to user)


#!/usr/bin/ksh

# Set threshold for 1, 5 and 15 minture load avarage
# configured for 12-processors system

max_1=48
max_5=36
max_15=24

# Set the string which appears before the load average in the uptime command
load_text='load average: '

#Email list
mail_to='abc@abc.com'

alert=n

up=`uptime`

# Parse out the current load average from the uptime command

load_1=`echo $up | /usr/xpg4/bin/awk -F "$load_text" '{ print $2 }' | cut -d, -f1 | cut -d. -f1`

load_5=`echo $up | /usr/xpg4/bin/awk -F "$load_text" '{ print $2 }' | cut -d, -f2 | cut -d. -f1`

load_15=`echo $up | /usr/xpg4/bin/awk -F "$load_text" '{ print $2 }' | cut -d, -f3 | cut -d. -f1`

# Set alert=y if any of the average are above their thresholds
if [ $load_1 -ge $max_1 ]
then
alert=y
elif [ $load_5 -ge $max_5 ]
then
alert=y
elif [ $load_15 -ge $max_15 ]
then
alert=y
fi

# Send mail if the alert threshold was reached
if [ ! $alert = n ]
then
mail -s "High load on procede02" $mail_to < `uptime`
fi

---
Now we use it in solaris 9, get some errors like

$./monitor_load_average.sh
./monitor_load_average.sh[43]: 9:32am up 3 day(s), 6:30, 9 users, load average: 0.32, 0.58, 0.55: cannot open


Can any expert give me a hand to debug it? (why have [43]..? why can not open? ..I think this is a syntax problem in AWK..)


Thank you very much in advance

Jerry
# 2  
Old 10-15-2008
Jerry-

Just from an initial peek at your script, I would put double quotes around ALL variables in the if statement and the elif statements.

Try that and see what happens.


HTH

Tony
# 3  
Old 10-15-2008
While I agree with the double quotes as a good general practice, it won't solve your problem:

mail -s "High load on procede02" $mail_to < `uptime`

You can't do that. You are trying to use the output of uptime as a file to be opened. You don't have a file by that name, so you get the "can not open".

And [43] means the shell was at line 43 on the script when it decided it had a problem. Sometimes the error is earlier in the file, but never later.
# 4  
Old 10-15-2008
Hammer & Screwdriver I think the issue is with your mail command

My first thought is that the < `uptime` in your mail command is giving you a problem. I think mail is expecting a file and not the output of a command.
Perhaps store uptime to a file, and then use that filename in place of the `uptime`.
# 5  
Old 10-15-2008
fixed

Hi, all friends,
You all are right.
it is fixed by creating file instead of command.
thank you all very much
Jerry
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to debug awk script?

how can i view what variables are stored upon the excution of an awk script. something equivalent to shell eg. sh -vx "script_file" many thanks in advance! (1 Reply)
Discussion started by: Apollo
1 Replies

2. Shell Programming and Scripting

Cant debug shell script

Hi I am relatively new in shell scripting Below is the code which i developed but for some reason, it keeps giving me error: /apps/bss/BatchProg/at1/batch/scripts/ksh/TBATLC02.ksh: syntax error at line 41 : `then' unmatched #!/usr/bin/ksh... (4 Replies)
Discussion started by: scripting_newbe
4 Replies

3. Shell Programming and Scripting

Problem with the script, help me debug

Hi, When i run the script ./script.sh sun, this give me no output, it should give me the list of file. If i run the script without the argument it should send me echo inside usage(). What is the problem? please help -Adsi #!/bin/sh ROOT_PATH=/net/icebox/vol/local_images/spins... (2 Replies)
Discussion started by: asirohi
2 Replies

4. UNIX for Dummies Questions & Answers

A small AWK problem

I have a file tmp.out with contents: 2008-08-09 05:11:01 2008-08-09 08:52:59 2008-08-11 12:08:34 2008-08-11 12:15:40 I want the output to be: 3|0|1|71|2008-08-09 05:11:01|2008-08-30 11:19:28 4|0|1|71|2008-08-09 08:52:59|2008-08-30 11:19:28 5|0|1|71|2008-08-11 12:08:34|2008-08-30 11:19:28... (6 Replies)
Discussion started by: ChicagoBlues
6 Replies

5. Shell Programming and Scripting

anyone!! debug this small script..thanks in advance..

x="PermitRootLogin no" cd /etc/ssh y=`cat sshd_config |grep "PermitRootLogin"` if ] then print "Sorry, Remote Root SSH login already disabled." exit else print "Welcome to Remote SSH Login disable script." fi output: + x=PermitRootLogin no + cd /etc/ssh + + cat sshd_config... (2 Replies)
Discussion started by: solaix14
2 Replies

6. UNIX for Advanced & Expert Users

How can debug our UNIX shell script?

Hi all, i have two simple questions here... 1. i want to know that how to debug the UNIX shell script? 2. is there any way to handling the exception in UNIX shell script like oracle exception handling? Please provide me those details it would be great help/ Thanks and Regards, MPS... (3 Replies)
Discussion started by: psiva_arul
3 Replies

7. Shell Programming and Scripting

Very small Shell Script Help...

The following Script takes each extension and determine what category it belongs and then moves it into a directory based on the extension. (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and 7.ksh, 2.ksh, 59.ksh together) and moves them to their respective directories viz.... (2 Replies)
Discussion started by: marconi
2 Replies

8. Shell Programming and Scripting

what is problem with this small shell script.. case statement related

Hi All, this small script is written to recognize user input character.. it is in small case .. upeer case or is a number... but when i input first capital letter say A.. it always gives small character.... what is the problem. #!/bin/bash echo "Enter the character" read a case $a in )... (2 Replies)
Discussion started by: johnray31
2 Replies

9. Shell Programming and Scripting

How to debug the awk script

Hi, How can I debug an awk script? I know that set -x can be used to debug a script. But this will not suite for awk scripts. Can anyone help me? Thanks in advance, Chella (2 Replies)
Discussion started by: chella
2 Replies

10. Shell Programming and Scripting

Debug an Awk Script

I would like to extract the following fields from the text file attached. I copied the contents from a pdf file and pasted them into the text file so I can use awk to extract them. The layout is as listed below. name1,name2,name3,name4,Title,designation,nationality,dob, national ... (1 Reply)
Discussion started by: mboro
1 Replies
Login or Register to Ask a Question