- Wrong shebang path (the first line that starts with #!). This line tells which command/program to use for execution of the script.
- Incorrect permissions
Wednesday, May 31, 2006
Linux : crontab does not run perl scripts ?
Monday, May 22, 2006
Away for quite some time
I hope to be back for posting relevant content soon.
Friday, May 12, 2006
PHP : Website Security
PHP files that do database inserts or some other server-side tasks,
should not be accessible via a web browser. All files under Document
Root of a website are accessible by a web-browser. Document Root is
the directory on the server's filesystem. This directory contains the
files/web pages of the website.
To avoid direct access of your php files, you can keep them in a
directory that is not inside Document Root. These files can then be
included into your templates. For web applications, a configuration
file can be included in all files using a relative path. This
configuration file may contain variables like database connection
details and other configuration variables. Include for database
connection too, should reside outside the Document Root.
Variables of help
$_SERVER['DOCUMENT_ROOT']
This followed by relative path of your conf or include files can be
used on most servers.
On shared hosting server, Document Root is not defined for each
website (virtual host), so this variable shows the default value. In
such cases, you can use
dirname(__FILE__)
This gives you the value of the directory containing the file.
Thursday, May 04, 2006
Regular Expressions : Removing blank lines of a file in vi
Following is an ex editor command.
:1,$g/^$/d
Notes :
- ex editor commands start with a colon.
- 1,$ stands for first line to last line of the file
- g for global (look for the pattern following everywhere; do not stop after the first match)
- pattern to look for is placed inside two slashes (//)
- ^ is for start of line and $ for end of line. Here, there's nothing between the two so the pattern is used for an empty line
- d in the end is the command to delete matching lines.
:1,$g/^ *$/d
Note that there's an asterisk (*) after a space. An asterisk matches the character preceeding it zero or more times.
e.g.
/a*/ matches a,aa,aaa,any number of times.
/\d*/ mathes an integer. \d is for a digit.
Linux : music compositions with sox
I'd like to take it further and add functionality which will allow merging of wav files with sox command that comes with linux distributions. This will allow composer to download his composition in wave format and hear it ! This will require reading the text file and mergin the sound files in the same order. This should be possible with a script (perl/php ?).
I'll soon be collecting .wav recordings of all the basic tabla bols (aksharas) and give it a try. Your comments or ideas are welcome.
Tuesday, May 02, 2006
Linux : date command
date
# This prints the current date. Something of this form - "Tue Jan 31 12:16:53 IST 2006"
date +%d
# prints current day of the month
Following examples illustrate getting time related information of a day/date other than the current date
date --date="3 days ago"
# prints date that was 3 days ago
Try these too !
date --date="last Sunday"
date --date="next Sunday"
date --date="Jan 26, 2004" +%a
# prints Day of the week on Jan 26, 2004
Setting the system date. You need to have admin privileges to use --set option.
date --set='+10 minutes'
# Takes system clock forwad by 10 minutes.
Friday, April 28, 2006
Perl : What are Regular Expressions, by the way ?
In this post, I just want to give beginners an idea. The topic 'Regular Expressions' is quite big. Unix commands - grep and sed were the first to popularize the concept. Many programming language support regular expressions for string manipulation. Perl has a powerful regex engine. The support/use of regular expressions gives Perl very powerful text-processing capabilities.
man perlre gives you access to a man page dedicated to regular expressions and their use in Perl. Beginners, mastering regular expressions is the key to excelling in Perl programming !
Enjoy regular expressions :-)
Thursday, April 27, 2006
Perl : Executing one line codes on the command line
example.
Command :
perl -e ' $\=" "; for (1 .. 9) { print; } '
Output :
1 2 3 4 5 6 7 8 9
Another frequently used switch is -n. This switch assumes 'while(<>){...}' around your code. Useful for processing all lines of the input coming through pipes. Example follows.
Command :
ls -l | perl -ne ' print if(/^d/); '
Here, output of ls -l (long listing) is piped to the perl command. -n makes the code loop over all the lines of the input that come from the previous command. The regular expression checks if the lines start with the letter d. Thus the code prints details of only the directories contained in the working directory. The command is equivalent to
ls -l | perl -e ' while(<>){ print if(/^d/); } '
(without -n switch)
-a is autosplit mode with -n or -p (splits $_ into @F ). See how it works
ls -l | perl -ane ' print $F[0]."\t".$F[7]."\n"; '
This prints the 0th and the 7th fields of the long directory listing (output of ls -l).
You can learn more about switches with this command
perl -h
To see version, built and other license related info,
perl -v
Wednesday, April 26, 2006
Perl : Sorting a list/array
Code :
@fruit = ("banana","mango","apple");
@sorted_fruit = sort @fruit;
$,=" "; # $, is the output field separator (a predefined variable). This is used here to put spaces after printing every element of the array.
$\="\n"; # $\ is the output record separator. Used to add a newline to print in this example.
print @sorted_fruit;
print reverse @sorted_fruit; # reverses the order in the list
Output :
apple banana mango
mango banana apple
This Blog : Conventions I use
Hi !
This post does not have any code :-) Relief.
Wanted to make you aware of the conventions I have been following.
You'll observe that green color is for the code. Comments are preceded
by a '#' sign. This is because codes of perl, shell scripts and php
use # for commenting. You'll find commented code in brown (well
mostly; I may miss out on a few occasions). I'll also use the same
brown color for [key]words worth noting. Your suggestions in this
regard are welcome.
Feel free to share this blog with your friends. I believe this blog is
shaping into a good learning resource. If you wish to have posts on a
particular subject, let me know. I'll be more than happy to help you
as much as I can.
You'll see some more Perl code snippets in the coming posts.
Thanks for your patience.
Perl : 'Hello World !' and some more
print "hello world !";
and run it with
perl hello.pl
That's it ! It shows the output on the console.
Some more code with comments follows. You may save this in another file and run it with perl command.
Comments begin with a #.
Code :
$name = "ketan404"; ## a scalar. Note the $ sign. It's good to use my while variable declarations but we shall cover that la ter.
@fruits = ("apple","banana","mango"); ## this is an array. On the right side of the '=' sign is a list. Note the @ sign.
%fruit_prices = ("apple"=>4,"banana"=>1,"mango"=>10); ## this is called a hash. Names of the fruits are its keys and thier p rices the values.
## printing these
print $name."\n"; ## print the scalar and concatenate a newline to it. A dot (.) is used for concatenation.
print @fruits; ## works ! print can also take an array as an argument.
print @fruits."\n"; ## Oops ! You appended a newline to it ? Using @fruits in scalar context gives the number of elements in the array and thus prints 3 with a newline. You'll understand these things as you make progress :-)
## printing keys and values of a hash
foreach $k (keys %fruit_prices){
print $k."=>".$fruit_prices{$k}."\n";
} ## works as expected
## keys function returns an array of keys of a hash.
## foreach or for are used for traversing an array
Tuesday, April 25, 2006
PHP : Object Oriented Programming in PHP
Code :
<?php
class web_developer{
var $skills; # class variables
var $sal;
function web_developer($sal){
$this->sal=$sal;
}
function getSal(){
return $this->sal;
}
function addSkill($technology,$yrs){
$this->skills[$technology]=$yrs;
}
function getSkills(){
return $this->skills;
}
} # class ends here
$x=new web_developer(2000); # creating a web_developer (an object) with associated salary !
echo $x->getSal()."<br />";# retrieving a property (sal) of the object
$x->addSkill('php',5); # adding his skill-set and no. of years of experience
$x->addSkill('perl',4);
$x_skills=$x->getSkills();# retrieving the property (skills) of the object
foreach ($x_skills as $k => $v){
echo "$k - $v<br />";
}
?>
Output in a browser:
2000
php - 5
perl - 4
New on this Blog : Subscription by Email
Hello readers !
You might have noticed that I have added a form on the right for email subscriptions. Don't be afraid. I'm not going to fill your inboxes with emails :-) If you find the content on this blog useful and would like to be notified about the new posts, consider filling out the form. I shall send you email notification manually from my gmail account since I don't have an account with any mass-emailing website.
Enjoy Perl for some time now !
Perl : map() with your subroutines
Code:
## subroutine that returns double of a number (or whatever passed to it as an argument :-)
sub double_it{
2*$_;
}
@nums = (1 .. 10); # array containing numbers 1 to 10
@doubles = map(&double_it, @nums); ## using map to create @doubles with subroutine double_it
$\=" "; # $\ is the built-in variable to specify the output record separator.
for(@doubles){
print;
}
Output:
2 4 6 8 10 12 14 16 18 20
Perl : map function
Code:
@numarray = (97 .. 122);
@charray = map(chr, @numarray);
print @charray;
Output:
abcdefghijklmnopqrstuvwxyz
Explanation:
- .. is a range operator. @numarray contains numbers from 97 to 122
- map function applies function char on each element of @numarray. The output is stored in @charray.
- chr function returns the character represented by that NUMBER in the character set. For example, "chr(65)" is "A".
- For more information on built-in perl functions, do 'man perlfunc'.
Sunday, April 23, 2006
PHP : Showing an image randomly from a collection of images
<?php
############### settings #############
$path = "/image_gallery/";
#######################################
$dir = $_SERVER['DOCUMENT_ROOT'].$path;
$ig = Array();
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if(ereg("\.jpg$",$file))
array_push($ig,$file);
}
closedir($dh);
}
}
$num = mt_rand(0,count($ig)-1);
echo "<img src=\"".$ig[$num]."\">";
?>
Notes :
- The code opens a directory specified by $dir and puts all jpg filenames in an array $ig.
- mt_rand function returns an integer between 0 and count of $ig i.e. number of jpg images in the directory.
Tuesday, April 18, 2006
CSS : Chaging cursor styles
- auto
- crosshair
- default
- help
- move
- pointer
- progress
- text
- wait
Move mouse over the lines below to see how the cursors look !
Cursor auto
When the value of cursor is set to auto, browser determines what style to show depending upon the context. i.e. if it sees
a link, it shows a pointer (a hand), default otherwise.
Cursor crosshair
Cursor help
Cursor default
Cursor move
Cursor pointer
Cursor progress
Cursor text
Cursor wait
Changing cursor styles with CSS adds great functionality to web pages. This can also be done in JavaScript. For Rich Web Applications (that use XMLHttpRequest for speed and better interaction), it's a good idea to change the cursor style to 'wait' after sending request to the server so that user expects some response from the server to be seen somewhere in the web page.
Following line sets cursor style to wait.
document.body.style.cursor="wait";
After loading completely the response from the server, the style may again be set to default.
Monday, April 17, 2006
Ajax - The key to Rich Web Applications
Typically (in most cases), getting a response from server involves refreshing/reloading the page you are viewing or going to a new page. With ajax, you can do it without refreshing. This is ajax in short :-) The natural question that should arise is 'HOW?'. Modern browsers come with support for an object called XMLHttpRequest. This object is used in ajax applications that require making an http request to the server and loading the response without refreshing the page.
While creation of an online account you have to provide a username. And if somebody has already taken it, you come to know about it only after submission of the form. This can now be avoided ! You can send an http request to the server with the username filled in by the new user as soon as the user leaves the text-field (onBlur event of javascript). The response can be shown next to the textfield so that user notices it immediately (as soon as it appears there).
So why should you use ajax ?
1. With ajax, you don't have to refresh your page for server responses.
2. You can get only the required server reponse and show in the desired place on your web-page. This reduces the byte-transfer considerably. There's no need to transfer all the html that show your headers, footers, images and navigation.
The methods and properties of this object are explained below.
var request = new XMLHttpRequest(); // creates an object of XMLHttpRequest
open() - new request to server.
send() - Sends a request to the server.
abort() - aborts current request
readyState - provides the current HTML ready state.
responseText - the text that the server sends back to respond to a request.
More information on the subject can be found on this link.
Some examples -
1. www.gmail.com - most of us know what it is :-)
2. http://gollum.easycp.de/en/ - a wikipedia browser
3. http://a-i-studio.com/cmd/ - a WebShell !
Sunday, April 16, 2006
Bash : sample loops (shell scripting)
--------------------
#!/bin/bash
for i in `seq 1 10`
do
let i=i*5
echo $i
done
--------------------
# seq outputs a sequence of numbers (1 to 10)
# let is a bash built-in command that allows arithmetic operations to be performed and assigned to a variable as done above
---------------
for i in $(ls)
do
echo $i
done
---------------
j=0
while [ $j -lt 10 ]
do
echo $j
let j=j+1
done
----------------
# same example with until
# note the -eq switch in until loop
--------------------
j=0
until [ $j -eq 10 ]
do
echo $j
let j=j+1
done
---------------------
Wednesday, April 12, 2006
Shell scripting
A shell script is a file containing unix/linux commands. Shell scripts are written to avoid repetitive work of issuing the same set of commands over and again. Shell grammar allows us to put conditional and looping constructs (as commands) making it easy to write scripts to do complex tasks.
Following is a simple shell script that shows date, operating system info and list of files in a working directory.
----------------------------
## simple.sh (filename)
echo "a simple shell script"
date
uname -a
ls
----------------------------
Lines starting with a # are comments.
To execute this script,
bash simple.sh
or
sh simple.sh
Shell scripts can be used like any other operating system commands. For this they need to be placed in a directory which the shell searches for commands. You can see these directories by
echo $PATH
You can update the envronment variable PATH to add your own directory to it. Say you want to add ~/bin (directory bin in your home), issue the following commands (bash specific).
PATH=$PATH:$HOME/bin
export PATH
The scripts/programs in this directory need to have execute permission if you want to use them as commands. Execute permission can be added by using chmod command
chmod +x scriptfile
I'll post some examples of small shell scripts in next few posts.