Saturday, September 13, 2008

Configuring Apache to run perl

Configuring Apache to run perl :

I have below RPM installed: (Default Fedora 9 Sulphur)
----------------------------------
httpd-2.2.8-3.i386
mod_perl-2.0.3-21.i386

In Fedora 9 perl specification :
===============================

$cat /etc/httpd/conf.d/perl.conf


*)
LoadModule perl_module modules/mod_perl.so

*)
Alias /perl /var/www/perl

< Directory /var/www/perl >
SetHandler perl-script
AddHandler cgi-script .cgi .pl
PerlResponseHandler ModPerl::Registry
PerlOptions +ParseHeaders
Options +ExecCGI
< /Directory >

*)
< Location /perl-status >
SetHandler perl-script
PerlResponseHandler Apache2::Status
Order deny,allow
#Deny from all
Allow from all
< /Location >

in my /etc/httpd/conf/httpd.conf i have below line
Include conf.d/*.conf


Now put your perl script in /var/www/perl

test.pl
========
#!/usr/bin/perl
Content-type: text/html;
print 'mod_perl rock';
[root@linux perl]#

Point your browser ==> http//hostname/test.pl

Should show mod_perl rock!

Sunday, September 07, 2008

rename file extentions ...

I wanted to rename all .txt file extensions as .html

$rename s/\.html/.txt/ *.html

Oh, also, this isn't a "standard Unix(TM) command, but it does come with most Perl installations that I know of and Perl is on most Unix machines that I know of.

Also i tried for loop :

for i in *.txt; do mv "$i" `basename $i`.html

But this renames a file file1.txt as file1.txt.html

anyone know how get avoid .html added after .txt ?

Try this :

for i in "${i%.txt}"; do mv "$i" "${i%.txt}".html; done

Exit Status - Shell script

Whenever a shell script is executed it checks the exit status
of command to verify if it executed sucessfully.

True = 0
False = Non Zero Value

You can check exit status value as below:

Eg:

#!/bin/bash

echo hello
echo $?
# Exit status 0 returned because command executed successfully.

lskdf
Unrecognized command.
echo $? # Non-zero exit status returned because command failed to execute.

$./script.sh

hello
0

./script.sh line 5 lskdf command not found.
127

Shell script if statement

Shell script using if statement to check if process is running:
================================================================

#!/bin/bash

sendmail=`(ps -ef |grep -v grep |grep sendmail)`
if [ -z "$sendmail" ];

then
echo "Sendmail is not running"

else
echo "Sendmail is running"

fi


WHat each line means:
---------------------
1) #!/bin/bash
This generates a Process ID for shell

2) PROCESS=`(ps -ef |grep -v grep |grep sendmail)`
Defining Variable Process:
# When we grep a process ps -ef |grep sendmail, the output result also shows "grep sendmail" line, in order to avoid that we use grep -v grep.


3) if [ -z "$sendmail" ];

-z option tells it should return a null value(no value)

Check for more : man bash

-n str True if string str is not a null string
-z str True if string str is a null string