Wednesday, October 29, 2008

FAQ Scripting Languages

Languages:
* Perl
* PHP
* Python
* Ruby
* Tcl
* ECMAScript
* Shell
* C

Common to all:

There are two ways to run a script, one by invoking the interpreter on the command line, and the other is to run it as a normal executable.

To mark a file as executable, use this command:

Code:
chmod +x 
To let the system know which interpreter to use, use the shebang ( #! ). It must be the first line of the script. It should point to the interpreter with the correct options.

To run a script, run it like a normal program, if the script isn't in your $PATH variable, specify the path.

The packages to be install, an example program, and the running of it are given here.

Code:
./
Will run a script or program in your current working directory.

Perl:

You don't need to install anything.

PHP Code:
#!/usr/bin/perl
#Save as "hw.pl"
print "Hello world!";
Mark it executable, and run.

PHP Code:
chmod +x hw.pl
./hw.pl
PHP:

You will need to install the php-cli package.

Code:
sudo aptitude install php5-cli
Example program:

PHP Code:
#!/usr/bin/php
// Save as hw.php
echo "Hello world\n";
?>
Mark it as executable and run.

Python:

Python is already installed.

Example program:

PHP Code:
#!/usr/bin/python
#Save as "hw.py"
print "Hello world!"
Mark it as executable and run.

Ruby:

Install with:
Code:
sudo aptitude install ruby
Example program:

PHP Code:
#!/usr/bin/ruby
# Save as "hw.rb"
puts "Hello world!";
Mark as executable and run.

Tcl:

Tcl is installed.

Example program:

PHP Code:
#! /usr/bin/tclsh
# Save as "hw.tcl"
puts "Hello World!"
Mark it as executable and run.

ECMAScript

Note: ECMAScript isn't normally used this way.

You will need to install spidermonkey, which is the ECMAScript interpreter used by Firefox.

Code:
sudo aptitude install spidermonkey-bin
Example program:

PHP Code:
#!/usr/bin/smjs
//Save as "hw.js"
print("Hello world");
Mark as executable and run.

Shell Scripts

Obviously, you have a shell installed.

Example program:

Code:
#!/bin/sh
# Save as "hw.sh"
echo "Hello world"
Mark as executable and run.

C:

Surprise! C is normally compiled, see FAQ: Compiling your first C or C++ programs if you want to learn how to use C. There is a very good C interpreter which you can use. It is also a compiler, and may be better than gcc in some cases.

You will need to install Tiny C Compiler (tcc), use:

Code:
sudo aptitude install tcc
Example program:

PHP Code:
#!/usr/bin/tcc -run
#include

int main(int argv, char ** argv)
{
printf("Hello World\n");
return
0;
}
Mark it as executable and run it.



Read more:

http://ubuntuforums.org/showthread.php?t=692720

No comments: