LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-19-2011, 02:18 PM   #1
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
To reduce the bash script length!!!


Hi friends,

at last i finish my script to find the status of the URL , in this code i used all the status codes, because of this it take too much of length. with the help of wiki only i find all the status codes.

http://www.w3.org/Protocols/HTTP/HTRESP.html

Quote:
and my question is do we need all the status codes necessary in this script..

and is there any other way to reduce the code using any conditonal statements..
Code:
#!/bin/bash
timevar=`date +%d-%m-%Y_%H.%M.%S` #—– > Storing Date and Time in a Variable
get_contents=`cat /urls.txt`  #—- > Getting content of website from file. Note the file should not contain any http:// as its already been taken care of
######### Next Section Does all the processing #########
for i in $get_contents
do
statuscode=`curl –connect-timeout 30 -w “totaltime:%{time_total}\n” -s -I -L http://$i | awk '{for(i = 1; i<=NF; i++) if($i=="“totaltime:0.000n”“totaltime:0.000n”HTTP/1.1") print $(i+1);}'`
case $statuscode in
200) 
    echo "$timevar $i $statuscode okay" >> /log.txt;;

201) 
    echo "$timevar $i $statuscode created" >> /log.txt;;


202) 
    echo "$timevar $i $statuscode Accepted" >> /log.txt;;


203) 
    echo "$timevar $i $statuscode Non-Authoritative Information" >> /log.txt;;


204) 
    echo "$timevar $i $statuscode No Content" >> /log.txt;;


205) 
    echo "$timevar $i $statuscode Reset Content" >> /log.txt;;

206) 
    echo "$timevar $i $statuscode Partial Content" >> /log.txt;;

207) 
    echo "$timevar $i $statuscode Multi-Status" >> /log.txt;;


226) 
    echo "$timevar $i $statuscode IM Used" >> /log.txt;;


300) 
    echo "$timevar $i $statuscode Multiple choices" >> /log.txt;;


301) 
    echo "$timevar $i $statuscode Moved Permanently" >> /log.txt;;

302) 
    echo "$timevar $i $statuscode found" >> /log.txt;;

303) 
    echo "$timevar $i $statuscode See Other" >> /log.txt;;


304) 
    echo "$timevar $i $statuscode Not Modified" >> /log.txt;;


305) 
    echo "$timevar $i $statuscode Use Proxy" >> /log.txt;;


306) 
    echo "$timevar $i $statuscode UnUsed" >> /log.txt;;



307) 
    echo "$timevar $i $statuscode Temporary Redirect" >> /log.txt;;


400) 
    echo "$timevar $i $statuscode Bad Request" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com  ;;     

401) 
    echo "$timevar $i $statuscode Unauthorized" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com |  >> /root/log.txt;;

402) 
    echo "$timevar $i $statuscode Payment Required" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

403)
    echo "$timevar $i $statuscode Forbidden" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

404)
    echo "$timevar $i $statuscode Page Not Found" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;



405)
    echo "$timevar $i $statuscode Method Not Allowed" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


406)
    echo "$timevar $i $statuscode Not Acceptable" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


407)
    echo "$timevar $i $statuscode Proxy Authentication Required" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


408)
    echo "$timevar $i $statuscode Request Timeout" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


409)
    echo "$timevar $i $statuscode Conflict" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


410)
    echo "$timevar $i $statuscode Gone" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

411)
    echo "$timevar $i $statuscode Length Required" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


412)
    echo "$timevar $i $statuscode Precondition Failed" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

413)
    echo "$timevar $i $statuscode Request Entity Too Large" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

414)
    echo "$timevar $i $statuscode Request-URI Too Long" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

415)
    echo "$timevar $i $statuscode Unsupported Media Type" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

416)
    echo "$timevar $i $statuscode Requested Range Not Satisfiable" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


417)
    echo "$timevar $i $statuscode Expectation Failed" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


422)
    echo "$timevar $i $statuscode Unprocessable Entity" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


423)
    echo "$timevar $i $statuscode Locked" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

424)
    echo "$timevar $i $statuscode Failed Dependency" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


425)
    echo "$timevar $i $statuscode Unordered Collection" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


426)
    echo "$timevar $i $statuscode Upgrade Required" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

444)
    echo "$timevar $i $statuscode No Response" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

449)
    echo "$timevar $i $statuscode Retry With" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


450)
    echo "$timevar $i $statuscode Blocked by Windows Parental Controls" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


499)
    echo "$timevar $i $statuscode Client Closed Request " >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


500)
    echo "$timevar $i $statuscode Internal Server Error" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


501)
    echo "$timevar $i $statuscode Not Implemented" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


502)
    echo "$timevar $i $statuscode Bad Gateway" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

503)
    echo "$timevar $i $statuscode Service Unavailable" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

504)
    echo "$timevar $i $statuscode Gateway Timeout" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

505)
    echo "$timevar $i $statuscode HTTP Version Not Supported" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

506)
    echo "$timevar $i $statuscode Variant Also Negotiates" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

507)
    echo "$timevar $i $statuscode Insufficient Storage" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

509)
    echo "$timevar $i $statuscode Bandwidth Limit Exceeded" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;

510)
    echo "$timevar $i $statuscode Not Extended" >> /log.txt
    echo "Status $statuscode found" | mail -s "Check of $i failed" 07anis@gmail.com;;


esac
done

Please guide me...legends!!!

With Regards
Anish kumar.V
 
Old 03-19-2011, 04:10 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
What do you mean by "too long"? I've written much longer ones myself.

I don't see a whole lot that can be done with this, offhand, since there's only a single loop and test. Significant shortening can usually only be done in places where you can reliably repeat operations.

In this case I'd probably create a function or two for the code commands instead of writing each one out separately. Something like this:
Code:
logfunction() {
     # $1 = url ($i) , $2 = statuscode , $3 = code name
     echo "$timevar $1 $2 $3" >> /log.txt
}

mailfunction() {
     # $1 = url ($i) , $2 = statuscode
     echo "Status $2 found" | mail -s "Check of $1 failed" 07anis@gmail.com
}
Then use them in the case statement like this:
Code:
200) logfunction "$i" 200 "okay" ;;

400) logfunction "$i" 400 "Bad Request"
     mailfunction "$i" "400" ;;
But the case statement itself is pretty much necessary as-is, since you do have to test for every possible error code*. So you don't really save a whole lot of space in the end.

The only other real suggestions I have are:

a) $(..) is recommended instead of `..`

b) You can use get_contents=$(</urls.txt) instead of calling on the external cat command.


*Assuming you actually do need all of them. You asked this question, but only you can answer it. Do you?

Last edited by David the H.; 03-19-2011 at 04:18 PM. Reason: Added postscript
 
Old 03-19-2011, 04:46 PM   #3
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
Hi David,

Thanks for your suggestion


Code:
#!/bin/bash
timevar=$(date +%d-%m-%Y_%H.%M.%S) #—– > Storing Date and Time in a Variable
get_contents=`cat /urls.txt` #—- > Getting content of website from file. Note the file should not contain any http:// as its already been taken care of
######### Next Section Does all the processing #########
function _echo () 
{
    echo $1 $2 $3 $4 >> /log.txt
}
function _mail () 
{
    echo "Status $1 found" | mail -s "Check of $2 failed" 07anis@gmail.com
}
for i in $get_contents
do
statuscode=`curl –connect-timeout 30 -w “totaltime:%{time_total}\n” -s -I -L http://$i | awk '{for(i = 1; i<=NF; i++) if($i=="“totaltime:0.000n”“totaltime:0.000n”HTTP/1.1") print $(i+1);}'`

case $statuscode in
    200) _echo $timevar $i $statuscode "okay"; _mail $statuscode $i;;
    201) _echo $timevar $i $statuscode "created";;
    202) _echo $timevar $i $statuscode "Accepted";;
    203) _echo $timevar $i $statuscode "Non-Authoritative Information";;
    204) _echo $timevar $i $statuscode "No Content";;
    205) _echo $timevar $i $statuscode "Reset Content";;
    206) _echo $timevar $i $statuscode "Partial Content";;
    207) _echo $timevar $i $statuscode "Multi-Status";;
    226) _echo $timevar $i $statuscode "IM Used";;
    300) _echo $timevar $i $statuscode "Multiple choices";;
    301) _echo $timevar $i $statuscode "Moved Permanently";;
    302) _echo $timevar $i $statuscode "found";;
    303) _echo $timevar $i $statuscode "See Other";;
    304) _echo $timevar $i $statuscode "Not Modified";;
    305) _echo $timevar $i $statuscode "Use Proxy";;
    306) _echo $timevar $i $statuscode "UnUsed";;
    307) _echo $timevar $i $statuscode "Temporary Redirect";;
    400) _echo $timevar $i $statuscode "Bad Request"; _mail $statuscode $i;;
    401) _echo $timevar $i $statuscode "Unauthorized"; _mail $statuscode $i;;
    402) _echo $timevar $i $statuscode "Payment Required"; _mail $statuscode $i;;
    403) _echo $timevar $i $statuscode "Forbidden"; _mail $statuscode $i;;
    404) _echo $timevar $i $statuscode "Page Not Found"; _mail $statuscode $i;;
    405) _echo $timevar $i $statuscode "Method Not Allowed"; _mail $statuscode $i;;
    406) _echo $timevar $i $statuscode "Not Acceptable"; _mail $statuscode $i;;
    407) _echo $timevar $i $statuscode "Proxy Authentication Required"; _mail $statuscode $i;;
    408) _echo $timevar $i $statuscode "Request Timeout"; _mail $statuscode $i;;
    409) _echo $timevar $i $statuscode "Conflict"; _mail $statuscode $i;;
    410) _echo $timevar $i $statuscode "Gone"; _mail $statuscode $i;;
    411) _echo $timevar $i $statuscode "Length Required"; _mail $statuscode $i;;
    412) _echo $timevar $i $statuscode "Precondition Failed"; _mail $statuscode $i;;
    413) _echo $timevar $i $statuscode "Request Entity Too Large"; _mail $statuscode $i;;
    414) _echo $timevar $i $statuscode "Request-URI Too Long"; _mail $statuscode $i;;
    415) _echo $timevar $i $statuscode "Unsupported Media Type"; _mail $statuscode $i;;
    416) _echo $timevar $i $statuscode "Requested Range Not Satisfiable"; _mail $statuscode $i;;
    417) _echo $timevar $i $statuscode "Expectation Failed"; _mail $statuscode $i;;
    422) _echo $timevar $i $statuscode "Unprocessable Entity"; _mail $statuscode $i;;
    423) _echo $timevar $i $statuscode "Locked"; _mail $statuscode $i;;
    424) _echo $timevar $i $statuscode "Failed Dependency"; _mail $statuscode $i;;
    425) _echo $timevar $i $statuscode "Unordered Collection"; _mail $statuscode $i;;
    426) _echo $timevar $i $statuscode "Upgrade Required"; _mail $statuscode $i;;
    444) _echo $timevar $i $statuscode "No Response"; _mail $statuscode $i;;
    449) _echo $timevar $i $statuscode "Retry With"; _mail $statuscode $i;;
    450) _echo $timevar $i $statuscode "Blocked by Windows Parental Controls"; _mail $statuscode $i;;
    499) _echo $timevar $i $statuscode "Client Closed Request"; _mail $statuscode $i;;
    500) _echo $timevar $i $statuscode "Internal Server Error"; _mail $statuscode $i;;
    501) _echo $timevar $i $statuscode "Not Implemented"; _mail $statuscode $i;;
    502) _echo $timevar $i $statuscode "Bad Gateway"; _mail $statuscode $i;;
    503) _echo $timevar $i $statuscode "Service Unavailable"; _mail $statuscode $i;;
    504) _echo $timevar $i $statuscode "Gateway Timeout"; _mail $statuscode $i;;
    505) _echo $timevar $i $statuscode "HTTP Version Not Supported"; _mail $statuscode $i;;
    506) _echo $timevar $i $statuscode "Variant Also Negotiates"; _mail $statuscode $i;;
    507) _echo $timevar $i $statuscode "Insufficient Storage"; _mail $statuscode $i;;
    509) _echo $timevar $i $statuscode "Bandwidth Limit Exceeded"; _mail $statuscode $i;;
    510) _echo $timevar $i $statuscode "Not Extended"; _mail $statuscode $i;;
esac
done

I hope this looks fine :-)


With Regards
Anish Kumar.V
 
Old 03-19-2011, 05:02 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If it works and you're happy with it, then great.

But don't use both function and () when defining a function. Use one or the other. Bash can accept it, but other shells may throw up an error.

You should also change the get_contents line as I suggested. The line that sets statuscode can also use $(..).

Be sure to mark the thread as "solved" when it's been answered to your satisfaction.
 
Old 03-19-2011, 05:13 PM   #5
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
Hi David,

Thanks again, but i didnt get what you are trying to say to change in the function part in the code!!!

Quote:
But don't use both function and () when defining a function. Use one or the other. Bash can accept it, but other shells may throw up an error.

Code:
#!/bin/bash
timevar=$(date +%d-%m-%Y_%H.%M.%S) #—– > Storing Date and Time in a Variable
get_contents=$(cat /urls.txt) #—- > Getting content of website from file. Note the file should not contain any http:// as its already been taken care of
######### Next Section Does all the processing #########
function _echo () 
{
    echo $1 $2 $3 $4 >> /log.txt
}
function _mail () 
{
    echo "Status $1 found" | mail -s "Check of $2 failed" 07anis@gmail.com
}
for i in $get_contents
do
statuscode=$(curl –connect-timeout 30 -w “totaltime:%{time_total}\n” -s -I -L http://$i | awk '{for(i = 1; i<=NF; i++) if($i=="“totaltime:0.000n”“totaltime:0.000n”HTTP/1.1") print $(i+1);}')

case $statuscode in
    200) _echo $timevar $i $statuscode "okay";
    201) _echo $timevar $i $statuscode "created";;
    202) _echo $timevar $i $statuscode "Accepted";;
    203) _echo $timevar $i $statuscode "Non-Authoritative Information";;
    204) _echo $timevar $i $statuscode "No Content";;
    205) _echo $timevar $i $statuscode "Reset Content";;
    206) _echo $timevar $i $statuscode "Partial Content";;
    207) _echo $timevar $i $statuscode "Multi-Status";;
    226) _echo $timevar $i $statuscode "IM Used";;
    300) _echo $timevar $i $statuscode "Multiple choices";;
    301) _echo $timevar $i $statuscode "Moved Permanently";;
    302) _echo $timevar $i $statuscode "found";;
    303) _echo $timevar $i $statuscode "See Other";;
    304) _echo $timevar $i $statuscode "Not Modified";;
    305) _echo $timevar $i $statuscode "Use Proxy";;
    306) _echo $timevar $i $statuscode "UnUsed";;
    307) _echo $timevar $i $statuscode "Temporary Redirect";;
    400) _echo $timevar $i $statuscode "Bad Request"; _mail $statuscode $i;;
    401) _echo $timevar $i $statuscode "Unauthorized"; _mail $statuscode $i;;
    402) _echo $timevar $i $statuscode "Payment Required"; _mail $statuscode $i;;
    403) _echo $timevar $i $statuscode "Forbidden"; _mail $statuscode $i;;
    404) _echo $timevar $i $statuscode "Page Not Found"; _mail $statuscode $i;;
    405) _echo $timevar $i $statuscode "Method Not Allowed"; _mail $statuscode $i;;
    406) _echo $timevar $i $statuscode "Not Acceptable"; _mail $statuscode $i;;
    407) _echo $timevar $i $statuscode "Proxy Authentication Required"; _mail $statuscode $i;;
    408) _echo $timevar $i $statuscode "Request Timeout"; _mail $statuscode $i;;
    409) _echo $timevar $i $statuscode "Conflict"; _mail $statuscode $i;;
    410) _echo $timevar $i $statuscode "Gone"; _mail $statuscode $i;;
    411) _echo $timevar $i $statuscode "Length Required"; _mail $statuscode $i;;
    412) _echo $timevar $i $statuscode "Precondition Failed"; _mail $statuscode $i;;
    413) _echo $timevar $i $statuscode "Request Entity Too Large"; _mail $statuscode $i;;
    414) _echo $timevar $i $statuscode "Request-URI Too Long"; _mail $statuscode $i;;
    415) _echo $timevar $i $statuscode "Unsupported Media Type"; _mail $statuscode $i;;
    416) _echo $timevar $i $statuscode "Requested Range Not Satisfiable"; _mail $statuscode $i;;
    417) _echo $timevar $i $statuscode "Expectation Failed"; _mail $statuscode $i;;
    422) _echo $timevar $i $statuscode "Unprocessable Entity"; _mail $statuscode $i;;
    423) _echo $timevar $i $statuscode "Locked"; _mail $statuscode $i;;
    424) _echo $timevar $i $statuscode "Failed Dependency"; _mail $statuscode $i;;
    425) _echo $timevar $i $statuscode "Unordered Collection"; _mail $statuscode $i;;
    426) _echo $timevar $i $statuscode "Upgrade Required"; _mail $statuscode $i;;
    444) _echo $timevar $i $statuscode "No Response"; _mail $statuscode $i;;
    449) _echo $timevar $i $statuscode "Retry With"; _mail $statuscode $i;;
    450) _echo $timevar $i $statuscode "Blocked by Windows Parental Controls"; _mail $statuscode $i;;
    499) _echo $timevar $i $statuscode "Client Closed Request"; _mail $statuscode $i;;
    500) _echo $timevar $i $statuscode "Internal Server Error"; _mail $statuscode $i;;
    501) _echo $timevar $i $statuscode "Not Implemented"; _mail $statuscode $i;;
    502) _echo $timevar $i $statuscode "Bad Gateway"; _mail $statuscode $i;;
    503) _echo $timevar $i $statuscode "Service Unavailable"; _mail $statuscode $i;;
    504) _echo $timevar $i $statuscode "Gateway Timeout"; _mail $statuscode $i;;
    505) _echo $timevar $i $statuscode "HTTP Version Not Supported"; _mail $statuscode $i;;
    506) _echo $timevar $i $statuscode "Variant Also Negotiates"; _mail $statuscode $i;;
    507) _echo $timevar $i $statuscode "Insufficient Storage"; _mail $statuscode $i;;
    509) _echo $timevar $i $statuscode "Bandwidth Limit Exceeded"; _mail $statuscode $i;;
    510) _echo $timevar $i $statuscode "Not Extended"; _mail $statuscode $i;;
esac
done
 
Old 03-19-2011, 08:53 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
You could use an array:
Code:
#!/bin/bash
timevar=$(date +%d-%m-%Y_%H.%M.%S) #—– > Storing Date and Time in a Variable

#—- > Getting content of website from file. Note the file should not
#contain any http:// as its already been taken care of
get_contents=$(< /urls.txt)

######### Next Section Does all the processing #########
_echo () 
{
    echo $1 $2 $3 $4 # >> /log.txt
}
_mail () 
{
    echo "Status $1 found" | mail -s "Check of $2 failed" 07anis@gmail.com
}

status_descriptions=(
    [200]='okay' 'created' 'Accepted' 'Non-Authoritative Information'
    'No Content' 'Reset Content' 'Partial Content' 'Multi-Status'
    [226]='IM Used'
    [300]='Multiple choices' 'Moved Permanently' 'found' 'See Other'
    'Not Modified' 'Use Proxy' 'UnUsed' 'Temporary Redirect'
    [400]='Bad Request' 'Unauthorized' 'Payment Required' 'Forbidden'
    'Page Not Found' 'Method Not Allowed' 'Not Acceptable'
    'Proxy Authentication Required' 'Request Timeout' 'Conflict' 'Gone'
    'Length Required' 'Precondition Failed' 'Request Entity Too Large'
    'Request-URI Too Long' 'Unsupported Media Type'
    'Requested Range Not Satisfiable' 'Expectation Failed'
    [422]='Unprocessable Entity' 'Locked' 'Failed Dependency'
    'Unordered Collection' 'Upgrade Required'
    [444]='No Response'
    [449]='Retry With' 'Blocked by Windows Parental Controls'
    [499]='Client Closed Request' 'Internal Server Error' 'Not Implemented'
    'Bad Gateway' 'Service Unavailable' 'Gateway Timeout'
    'HTTP Version Not Supported' 'Variant Also Negotiates'
    'Insufficient Storage' 'Bandwidth Limit Exceeded' 'Not Extended')

for i in $get_contents
do
    statuscode=$(curl –connect-timeout 30 -w “totaltime:%{time_total}\n” \
        -s -I -L http://$i | \
        awk '{for(i = 1; i<=NF; i++)
         if($i=="“totaltime:0.000n”“totaltime:0.000n”HTTP/1.1") print $(i+1);}')

    _echo $timevar $i $statuscode "${status_descriptions[statuscode]}"
    if ((statuscode == 200 || statuscode >= 400)) ; then
        _mail $statuscode $i
    fi
done
Quote:
Quote:
But don't use both function and () when defining a function. Use one or the other. Bash can accept it, but other shells may throw up an error.
but i didnt get what you are trying to say to change in the function part in the code!!!
As in use either _echo () or function echo, but not function _echo ().
 
Old 03-19-2011, 10:22 PM   #7
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
Code:
#!/bin/bash
timevar=$(date +%d-%m-%Y_%H.%M.%S) #—– > Storing Date and Time in a Variable
get_contents=$(cat /urls.txt) #—- > Getting content of website from file. Note the file should not contain any http:// as its already been taken care of
######### Next Section Does all the processing #########
 _echo () 
{
    echo $1 $2 $3 $4 >> /log.txt
}
 _mail () 
{
    echo "Status $1 found" | mail -s "Check of $2 failed" 07anis@gmail.com
}
for i in $get_contents
do
statuscode=$(curl –connect-timeout 30 -w “totaltime:%{time_total}\n” -s -I -L http://$i | awk '{for(i = 1; i<=NF; i++) if($i=="“totaltime:0.000n”“totaltime:0.000n”HTTP/1.1") print $(i+1);}')

case $statuscode in
    200) _echo $timevar $i $statuscode "okay";;
    201) _echo $timevar $i $statuscode "created";;
    202) _echo $timevar $i $statuscode "Accepted";;
    203) _echo $timevar $i $statuscode "Non-Authoritative Information";;
    204) _echo $timevar $i $statuscode "No Content";;
    205) _echo $timevar $i $statuscode "Reset Content";;
    206) _echo $timevar $i $statuscode "Partial Content";;
    207) _echo $timevar $i $statuscode "Multi-Status";;
    226) _echo $timevar $i $statuscode "IM Used";;
    300) _echo $timevar $i $statuscode "Multiple choices";;
    301) _echo $timevar $i $statuscode "Moved Permanently";;
    302) _echo $timevar $i $statuscode "found";;
    303) _echo $timevar $i $statuscode "See Other";;
    304) _echo $timevar $i $statuscode "Not Modified";;
    305) _echo $timevar $i $statuscode "Use Proxy";;
    306) _echo $timevar $i $statuscode "UnUsed";;
    307) _echo $timevar $i $statuscode "Temporary Redirect";;
    400) _echo $timevar $i $statuscode "Bad Request"; _mail $statuscode $i;;
    401) _echo $timevar $i $statuscode "Unauthorized"; _mail $statuscode $i;;
    402) _echo $timevar $i $statuscode "Payment Required"; _mail $statuscode $i;;
    403) _echo $timevar $i $statuscode "Forbidden"; _mail $statuscode $i;;
    404) _echo $timevar $i $statuscode "Page Not Found"; _mail $statuscode $i;;
    405) _echo $timevar $i $statuscode "Method Not Allowed"; _mail $statuscode $i;;
    406) _echo $timevar $i $statuscode "Not Acceptable"; _mail $statuscode $i;;
    407) _echo $timevar $i $statuscode "Proxy Authentication Required"; _mail $statuscode $i;;
    408) _echo $timevar $i $statuscode "Request Timeout"; _mail $statuscode $i;;
    409) _echo $timevar $i $statuscode "Conflict"; _mail $statuscode $i;;
    410) _echo $timevar $i $statuscode "Gone"; _mail $statuscode $i;;
    411) _echo $timevar $i $statuscode "Length Required"; _mail $statuscode $i;;
    412) _echo $timevar $i $statuscode "Precondition Failed"; _mail $statuscode $i;;
    413) _echo $timevar $i $statuscode "Request Entity Too Large"; _mail $statuscode $i;;
    414) _echo $timevar $i $statuscode "Request-URI Too Long"; _mail $statuscode $i;;
    415) _echo $timevar $i $statuscode "Unsupported Media Type"; _mail $statuscode $i;;
    416) _echo $timevar $i $statuscode "Requested Range Not Satisfiable"; _mail $statuscode $i;;
    417) _echo $timevar $i $statuscode "Expectation Failed"; _mail $statuscode $i;;
    422) _echo $timevar $i $statuscode "Unprocessable Entity"; _mail $statuscode $i;;
    423) _echo $timevar $i $statuscode "Locked"; _mail $statuscode $i;;
    424) _echo $timevar $i $statuscode "Failed Dependency"; _mail $statuscode $i;;
    425) _echo $timevar $i $statuscode "Unordered Collection"; _mail $statuscode $i;;
    426) _echo $timevar $i $statuscode "Upgrade Required"; _mail $statuscode $i;;
    444) _echo $timevar $i $statuscode "No Response"; _mail $statuscode $i;;
    449) _echo $timevar $i $statuscode "Retry With"; _mail $statuscode $i;;
    450) _echo $timevar $i $statuscode "Blocked by Windows Parental Controls"; _mail $statuscode $i;;
    499) _echo $timevar $i $statuscode "Client Closed Request"; _mail $statuscode $i;;
    500) _echo $timevar $i $statuscode "Internal Server Error"; _mail $statuscode $i;;
    501) _echo $timevar $i $statuscode "Not Implemented"; _mail $statuscode $i;;
    502) _echo $timevar $i $statuscode "Bad Gateway"; _mail $statuscode $i;;
    503) _echo $timevar $i $statuscode "Service Unavailable"; _mail $statuscode $i;;
    504) _echo $timevar $i $statuscode "Gateway Timeout"; _mail $statuscode $i;;
    505) _echo $timevar $i $statuscode "HTTP Version Not Supported"; _mail $statuscode $i;;
    506) _echo $timevar $i $statuscode "Variant Also Negotiates"; _mail $statuscode $i;;
    507) _echo $timevar $i $statuscode "Insufficient Storage"; _mail $statuscode $i;;
    509) _echo $timevar $i $statuscode "Bandwidth Limit Exceeded"; _mail $statuscode $i;;
    510) _echo $timevar $i $statuscode "Not Extended"; _mail $statuscode $i;;
esac
done

Thanks for all your Replies....:-) spl thanks to David and ntubski tooo...:-)

With Regards
Anish Kumar.V
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: SQL-style map/reduce programming in bash LXer Syndicated Linux News 0 09-22-2010 10:42 PM
LXer: Second Call for votes for Constitutional amendment: reduce the length of DPL el LXer Syndicated Linux News 0 10-01-2007 06:40 AM
Bash script to check the input string length fjkum Programming 3 06-30-2007 09:43 PM
BASH length of file baks Programming 14 03-13-2007 10:52 PM
Zero-length bash .history file! revmyo Linux - Security 1 12-03-2005 09:09 PM


All times are GMT -5. The time now is 04:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration