Script languages
NB.: This section will be updated frequently
Introduction: The script languages that have interpreters operating under Linux OS (we shall be concerned here with simplyMEPIS VERSION 6.5) and will be coverd, here, in this section are: perl, python, tcl, tk, ruby, rexx (regina) and haskell. In order to prepare scripts in different languages and have them run in the terminal (konsole) window, it is suggested to first set up the appropriate icons and folders on the desktop as follows:
  1. Right click the mouse (i.e. click the right btton on your mouse), then choose Create new and then click folder from the subcontext menu that will appear on the screen.
  2. Enter the folder name, example My scripts. Double-click the folder you have just created and start adding the links to applications.
  3. Right-click your mouse, then choose Create new and click Link to application. In the dialogue box that will appear (under the general tab), type the name of the script language, eg. my perl scripts. Click the Application tab, then go to the Work path field, click on the icon to the left of the field then click on the folder having you scripts (in our example would be perl scripts). Then go to the command field and type konsole, then click the OK button in the dialogue (tab) box.
  4. Now you have an icon in the scripts file (on your desktop) for perl ones, you can have additional ones for python, tcl, ruby,...etc.
  5. When you click this icon a shell session will start in the scripts directory that you want, eg. perl. You can get a list of files in the directory (by typing dir or ls at the prompt), then you can start your perl scripts by typing perl followed by a space and the file name (having the script) with .pl extension. If you have the sha-bang in your script (this special first line is #!/usr/bin/perl), then you run your script with ./ and the file name with its extension (with no space between ./ and the file name). The same applies to the other scripting languages mentioned here.
Script language Running the script Interpreter version
perl perl (or ./) hellop.pl v. 5.8.7
python python (or ./) hellopython.py v. 2.4.3
tcl tclsh (or ./) hellotcl.tcl 8.3 & 8.4
ruby ruby (or ./) hellor.rb v. 1.8.4
rexx rexx (./) hellor.rexx regina v.3.3.5.00
tk wish hellotk.tcl v. 8.3 & 8.4
haskell hugs or ghci then the filename with .hs extension. Evaluate the expression (or function) by typing its name, eg. hugs (or ghci) hellointer.hs, hit enter and type hellointer (to evaluate the expression or function) hugs v. 20050308 or ghci v. 6.4.1
Saying hello in different scripting languages:
Perl:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/perl
    print "Hello from ActivePerl!";
  3. Save the file as hellop.pl
  4. Run the script by typing ./hellop.pl
Python:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/python
    print "Hello from the Python"
  3. Save the file as hellopython.py
  4. Run the script by typing ./hellopython.py
tcl:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/tclsh
    puts stdout {Hello from TCL/TK};
  3. Save the file as hellotcl.tcl
  4. Run the script by typing ./hellotcl.tcl
Ruby:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/ruby
    print "Hello World\n"
  3. Save the file as hellor.rb
  4. Run the script by typing ./hellor.rb
Rexx:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/rexx
    say "Hello world from REXXX"
  3. Save the file as hellor.rexx
  4. Run the script by typing ./hellor.rexx
tk:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/wish
    #exec wish "$0" $@"
    button .hello -text "Hello from Tk" -command {
    puts stdout "Hello from Tk"; destroy .
    }
    pack .hello
  3. Save the file as hellotk.tcl
  4. Run the script by typing ./hellotk.tcl
A simple mathematical program (accepting user input, making simple calculatins and outputing results to screen):
Perl:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/perl
    print ("enter first number: \n");
    $noe=;
    print ("enter second number: \n");
    $el2=;
    $req=($noe)+($el2);
    $req1=($noe)*($el2);
    $req2=($noe)/($el2);
    $req3=($noe)-($el2);
    print ("the sum of the 2 numbers = $req\n");
    print ("the multiplication of the 2 numbers = $req1\n");
    print ("the division of the first number by the second = $req2\n");
    print ("the first number minus the second = $req3\n");
    format printtosc =
    ********************************************************************************
    The Result
    sum of the numbers =@>>>>.the multiplication of the numbers =@>>>>,the division of the first number by the second =@>>>>, the first number minus the second =@>>>>
    $req,$req1,$req2,$req3
    ********************************************************************************
    .
    $~="printtosc";
    write(STDOUT);
  3. Save the file as simpleperl.pl
  4. Run the script by typing ./simpleperl.pl
Python:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. #!/usr/bin/python
    print "enter first element"
    el1 = float(raw_input())
    print "enter second number"
    el2 = float(raw_input())
    print "the addition of 2 numbers = ",el1+el2
    print "the multiplication of 2 numbers = ",el1*el2
    print "the second number minus he first = ",el1-el2
    print "the division of the first by the second number = ",el1/el2
  3. Save the file as simplepython.py
  4. Run the script by typing ./simplepython.py
tcl:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/tclsh
    proc deltawye {} {
    puts "Enter the value of the first number eg. 3.0: "
    set el2 [gets stdin]
    puts "Enter the second number per above indicated format: "
    set el1 [gets stdin]
    set result0 [expr ($el2+$el1)]
    set result1 [expr ($el1*$el2)]
    set result2 [expr ($el2-$el1)]
    set result3 [expr ($el2/$el1)]
    puts "the sum of 2 numbers = $result0"
    puts "the multiplication of 2 numbers = $result1"
    puts "the second number minus the first = $result2"
    puts "dividing the first numbe by the second = $result3"
    }

    set choice ""
    while {$choice != "q"} {
    puts "Enter c to proceed & q to exit: "
    set choice [gets stdin]
    if {$choice == "c"} {deltawye}
    if {$choice == "q"} {exit}
    }
  3. Save the file as simpletcl.tcl
  4. Run the script by typing ./simpletcl.tcl
Ruby:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/ruby
    print "enter a number\n"
    a = STDIN.gets.chomp.to_f
    print "enter another number\n"
    b = STDIN.gets.chomp.to_f
    c = a+b
    d = a-b
    e = a*b
    f = a/b
    print "sum = ",c,"\n"
    print "second number minus the first = ",d,"\n"
    print "multiplication = ",e,"\n"
    print "first number divided by the second = ",f,"\n"
  3. Save the file as simpleruby..rb
  4. Run the script by typing ./simpleruby.rb
Rexx:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/rexx
    say "enter a number"
    pull a
    say "enter another number"
    pull b
    c = a+b
    d = a-b
    e = a*b
    f = a/b
    say "sum = " c
    say "second number minus the first = " d
    say "multiplication = " e
    say "first number divided by the second = " f
  3. Save the file as simplerrexx.rexx
  4. Run the script by typing ./simplerrexx.rexx
Finding the area of a square, a rectangle or a circle:
Perl:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
       #!/usr/bin/perl
    while ($q==0){
    print "enter 1 for area of a square,\n2 for area of a rectangle,\n3 for area of a circle and 4 to quit: \n\n";
    $choice=;
    print("\n");
    if($choice == 1) {
    print ("enter base length of square: \n");
    $noe=;
    $req=$noe*$noe;
    print ("the area = $req\n");
    
    format printtosc =
    ********************************************************************************
                                   The Result
    area =@>>>>
    $req
    ********************************************************************************
    .
    $~="printtosc";
    write(STDOUT);
    print ("to save result to data file, enter 1: ");
    $fchoice=;
    if ($fchoice==1) {
    print ("to append file enter 1 & to ovewrite/create, enter 2: ");
    $appover=;
    if ($appover==1){
    format printtofile =
    ==================================================================================
    area =@>>>>
    $req
    ==================================================================================
    .
    print ("enter file name: ");
    $filenam=;
    open(myfile,">>$filenam");
    select(myfile);
    $~="printtofile";
    write(myfile);
    close(myfile);
    select(STDOUT);
    }
    elsif ($appover==2){
    print ("enter file name: ");
    $filenam=;
    open(myfile,">$filenam");
    select(myfile);
    print ("the equivalent = $req\n");
    print("the $noe elements are: $el1, $el2 & $ el3");
    close(myfile);
    select(STDOUT);
    }
    }
    }
    elsif($choice == 2) {
    print ("enter base length of rectangle: \n");
    $noe=;
    print ("enter height of rectangle: \n");
    $noe1=;
    $req=$noe*$noe1;
    print ("the area = $req\n");
    }
    elsif($choice == 3) {
    print ("enter radius of circle: \n");
    $noe=;
    $req=$noe*$noe*3.14159;
    print ("the area = $req\n");
    }
    elsif($choice == 4) {last;}
    }
     
  3. Save the file as areasperl.pl
  4. Run the script by typing ./areasperl.pl
Python:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
      
    #!/usr/bin/python
    print "areas"
    choice =""
    while choice != "q":
    	print "enter choice: a for a square area, b a rectangle, c for a circle area and q to quit"
    	choice = raw_input()
    	if choice == "a":
    		print "a square area"
    		print "base length of square"
    		base = float(raw_input())
    		area=(base*base)
       		print "the area of square = ",area
        		print "overwrite enter o, append enter a"
        		appover = raw_input()
        		if appover == "o":
            		print "enter file name"
            		fnam = raw_input()
            		f=open(fnam, 'w')
            		f.write ('\nthis is to overwrite or create\n')
            		f.write (area)
            		f.close()
        		if appover == "a":
            		print "enter file name"
            		fnam = raw_input()
            		f=open(fnam, 'a')
            		f.write ('\nthis is to append\n')
            		f.write (area)
            		f.close()
    				
    	if choice == "b":
    		print "rectangle area"
                    print "base length of rectangle"
    		base = float(raw_input())
                    print "height of rectangle"
    		height = float(raw_input())
    		area=(height*base)
       		print "the area of rectangle = ",area
        	if choice == "q":
    		break
    
  3. Save the file as areaspy.py
  4. Run the script by typing ./areaspy.py
tcl:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
      
    #!/usr/bin/tclsh
    proc square {} {
    puts "base length of square eg 2.0: "
    set el1 [gets stdin]
    set res [expr ($el1*$el1)]
    puts "the area = $res"
    puts "to save to data file: to overwrite enter 1; to append enter 2 (press enter key to skip)"
    set overappe [gets stdin]
    if {$overappe == 1} {
    puts "overwrite, enter filename to create/overwrite"
    set filenam [gets stdin]
    set fileid [open $filenam w]
    puts $fileid "the base length & area are: $el1, $res"
    close $fileid
    }
    if {$overappe == 2} {
    puts "append, enter filename to append"
    set filenam [gets stdin]
    set fileid [open $filenam a]
    puts $fileid "the base length & area are: $el1, $res"
    close $fileid
    }
    }
    proc circle {} {
    puts "radius of circle eg 2.0: "
    set el1 [gets stdin]
    set res [expr ($el1*$el1*3.14159)]
    puts "the area = $res"
    puts "to save to data file: to overwrite enter 1; to append enter 2 (press enter key to skip)"
    set overappe [gets stdin]
    if {$overappe == 1} {
    puts "overwrite, enter filename to create/overwrite"
    set filenam [gets stdin]
    set fileid [open $filenam w]
    puts $fileid "the radius and area are: $el1, $res"
    close $fileid
    }
    if {$overappe == 2} {
    puts "append, enter filename to append"
    set filenam [gets stdin]
    set fileid [open $filenam a]
    puts $fileid "the radius and area are: $el1, $res"
    close $fileid
    }
    }
    
    proc rectangle {} {
    puts "base length of rectangle according to following format nn.nn eg. 3.0: "
    set el2 [gets stdin]
    puts "height of rectangle per above indicated format: "
    set el1 [gets stdin]
    set result0 [expr ($el2*$el1)]
    puts "the area = $result0"
    puts "to save to data file: to overwrite enter 1; to append enter 2 (press enter key to skip)"
    set overappe [gets stdin]
    if {$overappe == 1} {
    puts "overwrite, enter filename to create/overwrite"
    set filenam [gets stdin]
    set fileid [open $filenam w]
    puts $fileid "the height, base length and area are: $el1, $el2, $result0"
    close $fileid
    }
    if {$overappe == 2} {
    puts "append, enter filename to append"
    set filenam [gets stdin]
    set fileid [open $filenam a]
    puts $fileid "the height, base length and area are: $el1, $el2, $result0"
    close $fileid
    }
    }
    
    set choice ""
    while {$choice != "q"} {
    puts -nonewline "Enter a for area of a square, \nb for area of rectangle, \nc for area of circle & q to exit: "
    set choice [gets stdin]
    if {$choice == "a"} {square}
    if {$choice == "b"} {rectangle}
    if {$choice == "c"} {circle}
    if {$choice == "q"} {
    exit}
    } 
    
  3. Save the file as areastcl.tcl
  4. Run the script by typing ./areastcl.tcl
Ruby:
  1. Open a new document in any text editor (kwrite, kedit or kate).
  2. Type the following lines:
    #!/usr/bin/ruby
    quit = 0
    while quit == 0
    print "enter 1 for area of a square, 2 for area of rectangle, 3 for area of circle and 4 to quit\n"
    choice = STDIN.gets.chomp.to_i
    if choice == 1 then
    print "area of a square\n"
    print "enter base length of square\n"
    a = STDIN.gets.chomp.to_f
    c = a*a
    print "area of square = ",c,"\n"
    elsif choice == 2 then 
    print "area of a rectangle\n"
    elsif choice == 3 then 
    print "area of a circle\n"
    print "enter radius of circle\n"
    a = STDIN.gets.chomp.to_f
    c = a*a*3.14159
    print "area of circle = ",c,"\n"
    elsif choice == 4 then 
    quit = 1
    end
    end
    
  3. Save the file as areasruby.rb
  4. Run the script by typing ./areasruby.rb
  5. Rexx:
    1. Open a new document in any text editor (kwrite, kedit or kate).
    2. Type the following lines:
      #!/usr/bin/rexx
      quit = 0
      do while quit = 0
      say "enter 1 for area of a square, 2 for area of a rectangle, 3 area of a circle and 4 to quit"
      pull a
      if a = 1 then
      do
      say "enter base length of square"
      pull b
      c = b*b
      say "area of square = " c
      end
      if a = 2 then
      do
      say "area of a rectangle"
      say "enter base length of rectangle"
      pull b
      say "enter height of rectangle"
      pull b1
      c = b*b1
      say "area of rectangle = " c
      end
      if a = 3 then
      do
      say "area of a circle"
      say "enter radius of circle"
      pull b
      c = b*b*3.14159
      say "area of circle = " c
      end
      if a = 4 then 
      do
      quit = 1
      end
      end
      
    3. Save the file as areasrexx.rexx
    4. Run the script by typing ./areasrexx.rexx

    More programs and examples (related to electrical power systems): The above pages will be updated frequently.

    The Linux page


    Home of the Virtual Electrical Power Institute (VePi)