Popular Scripting Languages on Linux and Windows

Scripting is an art created by “system administrators.” This art requires knowledge of the system itself, system administration command syntax, programming and algorithm knowledge, and at least one scripting programming language. There are many options for system administrators to write scripts, each scripting language has its own unique syntax and advantages. There is little similarity between scripting languages, but they are not as hard to read as alien languages. Scripting languages can be used both for system administration and web development. There are server-side and client-side scripting languages. Client-side scripting languages are mostly used to display content in the browser and interact with users. Server-side scripting languages are usually used for system administration or web services.

In this article, we will only focus on those scripting languages used for system administration. Let’s get started!

Things you should know about scripts:

• Scripts should be executable as standalone commands, or they can be invoked using the scripting language binary at the terminal command-line interface.

• If it is an executable script, the following special line should be written at the beginning:

#!/path/of/the/cli

For example bash

#!/bin/bash

Or perl

#!/usr/bin/perl

The ‘#!’ symbol represents the program that should be called to execute the script.

Bash

In the Linux and UNIX world, bash scripts are the most numerous. As we mentioned in many articles, bash is also the most beloved.

Basically, bash is an interpreted command-line shell. It can be used for programming and satisfies most basic programming needs. You do not need to declare variables before using them, nor do you need to know their types. Its downside is that there are no other libraries available for bash. The only tools you can use are /bin/bash (or /bin/sh). When you declare a variable, you do not need to prefix it with $, but when using it you need to prefix it with $ to indicate it is a “variable.”

Bash is very easy to write, and Linux, UNIX, and Windows with cgywin installed all come with bash. If you are a system administrator but cannot write bash scripts, you had better start learning right away. The article 10 Best Books for System Administrators lists many good references. Below is a simple bash script.

·········10········20········30········40········50········60········
01.#!/bin/bash
02. 
03.USER=$1
04.echo "Adding User $USER in group users..."
05./usr/sbin/useradd -g users $USER
06.if [ $? == 0 ]; then
07./usr/bin/passwd $USER
08.else
09.echo "Sorry, User addition failed"
10.fi

Perl

Basically, perl can be regarded as a (Turing-) complete programming language on UNIX and Windows systems. Perl stands for Practical Extraction and Report Language. Perl is a general-purpose programming language. Initially developed for text processing, it is now widely used in many areas including system administration, web development, network programming, and even GUI development.

Its advantages are ease of use and support for both procedural and object-oriented programming. Perl is modular and makes it easy to call third-party modules. Although designed as a Turing-complete programming language, perl has been used for writing system administration scripts since its debut in 1993. Perl supports both compilation and interpreted execution, so perl is safer than bash scripts. When an error occurs, bash scripts do not know about it; however, if any error occurs during compilation before execution, perl will refuse to start executing.

A small perl script will give you a taste of the perl language style. Content after # is a comment. This script creates an array of 1000 random numbers, each containing 16 digits:

·········10········20········30········40········50········60········
01.#!/usr/bin/perl
02. 
03.my @numbers;
04.srand (time);
05. 
06.for ($i=0; $i<1000; $i++)
07.{
08.$a=int 10000000000*rand();
09.$b=int 10000000000*rand();
10.$c = $a . $b;
11. 
12.push @numbers,substr($c,0,16)."/n";
13.}
14.print @numbers;

PHP

Everyone knows that PHP is “usually” used for website development, but it can also perform system administration tasks just like perl. That is why many system administrators use PHP for cron jobs and other scripting work. Although similar in functionality to perl, PHP is designed to generate standard HTML output. Usually it runs embedded within a web server program. The command-line version of PHP supports scripting.

Similar to the perl example above, a PHP script with the same functionality is as follows:

·········10········20········30········40········50········60········
01.#!/usr/bin/php
02. 
03.function make_seed()
04.{
05. 
06.list($usec, $sec) = explode(' ', microtime());
07. 
08.return (float) $sec + ((float) $usec * 100000);
09.}
10. 
11.srand (make_seed());
12. 
13.for ($i=0; $i<1000; $i++)
14.{
15.$a=rand(1000000000,9999999999);
16.$b=rand(1000000000,9999999999);
17.$c = $a . $b;
18. 
19.$numbers[]=substr($c,0,16);
20.}
21.for ($i=0; $i<1000;$i++)
22. 
23.echo "$numbers[$i]/n";

Python

Obviously, we are talking about a scripting language, not some animal in the Amazon jungle. Python is a general-purpose, high-level programming language that emphasizes code readability. Python’s syntax is very concise and expressive. Similar to perl, python also has many extension libraries. Although python has the capability for functional programming, it is mostly regarded as an object-oriented language. In web development, python is used to develop the mod_wsgi module for the Apache project. Nowadays, most Linux and UNIX distributions include python, and many system tools are developed using python as a scripting language. Python also supports writing code for GUI environments.

Below is a simple python code snippet:

·········10········20········30········40········50········60········
01.health = 10
02.trolls = 0
03.damage = 3
04. 
05.while health >0:        #!= 0:
06.trolls += 1
07.health = health - damage
08.print " " ,"but takes", damage, "damage points./n"
09.print " ", trolls, "trolls."

sed

sed is a Unix text parsing tool that provides a programming language for text parsing and transformation. sed stands for Stream Editor and is basically a blazing-fast text editor. sed does not provide any interactive environment for file editing. It reads content line by line, performs the operations specified by the command line, i.e., the sed script, and then outputs the results. You can use sed to quickly perform text operations on files. Recently perl has been used to handle similar tasks, but there are still some situations that require expert system administrators to use sed to quickly solve problems. Below is a simple example:

sed -e 's/foo/bar/g' myfile.txt

This command will find all occurrences of foo in the file myfile.txt, replace them with bar, and then output the results to the screen. The main command of sed is ‘s/foo/bar/g’, which is also supported in interactive editing mode in vi or vim.

Besides the ones introduced in this article, there are many other scripting languages such as Ruby, VBScript, Java Script, JScript, Tcl, AppleScript, and Falcon. This article aims to introduce the scripting languages commonly used in system administration, and I hope you will become familiar with them and improve your work efficiency.

Leave a Comment

Your email address will not be published.