blog
    Coding for Administrators
    24 October 18

    Coding for Administrators

    Posted byAndrew Mallett
    facebooktwitterlinkedin
    news-featured

    As an administrator myself I am well aware of the challenges that you will face trying to decide where and how to start coding. We are more than aware of the need to code:

    • Accelerating repetitive tasks
    • Ensuring accuracy of the task and it being repeatedly correct
    • Delegating administrative tasks
    • Sharing of tools
    • Running commands across many systems simultaneously

    Linux is a great platform on which to begin writing code, therefore the need for developers and administrators in Linux is high. Linux distributions generally come with all the tools needed to start writing code. This is true regardless of whether you look at Shell scripting, Python, PERL, Java or C, your distribution will include the tools that you need at no additional cost.

    However, if you don’t code already where do you start? There are so many languages that you can use in Linux, but which should you choose?

    • What will still be relevant in a year’s time?
    • Do I need additional packages installed to run a program or script?
    • Is this program or script portable?
    • Is it easy to learn?
    • What is the speed of writing?
    • What kind of support is offered?

    With these questions all acting as hurdles, it is here that many administrators stop. I’m here to help you make the correct choice based on what I have already learned and experienced in over 20 years of Linux and Unix administration.

     

    BASH Scripts

    One of the quickest mechanisms to get you started with automation is a BASK shell script. You will already be familiar with running commands at the command line, making this the most natural next step for administrators. BASH is available on almost every Unix and Linux system and includes OS X.

    To print the ubiquitous “hello world” in a shell script we would just need two lines in a text file:

    #!/bin/bash
    echo “Hello World”

    We can achieve a little more in production scripts, I demonstrate a script that prompts for LDAP group information in the Ubuntu 18.04 Server: Installing and Managing openLDAP Directories course from INE.

     

    Python

    Python is another scripting language that extends past shell scripting. With BASH scripting, you are limited to running shell commands, repeating what you do at the command prompt. With Python, you are able to bypass commands having direct access to many APIs (Application Programming Interface). This gives you the power to create your own bespoke applications using only the resources that you need.

    Python has the added benefit of being described as “elegant code,” meaning the unnecessary clutter of braces and semi-colons is removed, and as with all great works of art, the white-space is significant. Whereas in most languages, indenting of code-blocks is optional, in Python it is required.

    The simple hello world statement written in Python would be:

    #!/usr/bin/python
    print(“Hello World”)

    Both Python and BASH are scripting languages that begin in a similar way. We highlight the path to the interpreter that must execute the script. This introduces an inherent performance issue. Compiled code, on the other hand, does not need an interpreter and is already optimized for the hardware it was intended to run. We look at C programming next as an example of compiled code.

     

    C Programming

    C Programming goes back to the 1970’s. With this in mind, you may feel that it is not something you should be learning now, however, I disagree. For starters, most Linux and Unix programs are written in C. The Linux kernel is entirely written in C. This shows the longevity of the language, and in a time where development languages come and go at a frightening pace, it is reassuring to know that this language remains.

    Although it is not the easiest of languages to learn, the concept of strings does not exist in C, learning C will help you learn almost any language. Think of the concept of learning programming properly in the first instance rather than learning shortcuts to programming.

    To say hello in C it takes a little more typing, but not that much more than we have seen previously:

    #include<stdio.h>
    int main (void) {
              puts("Hello World");
    }

    We create C programs as text files and then compile the code into an executable. This should improve the efficiency of the program as it is not reliant on the BASH shell or programs that the script calls. Everything in C is written direct to the APIs exposing resources on your system.

    To compile a C program in Linux we would use:

    $ gcc -Wall -o hello hello.c

    The command will create a program called hello by reading in the source file of hello.c.

    Being a more mature language, C has a huge support base, including books, courses and web pages to help you learn. Learning C will also assist you in learning any other language, which is one of the reasons I am still very keen on promoting C.

    In summary, there are many ways that you can start to program in Linux. The biggest task is “to start.” Once you have begun your programming journey, you will have been bitten by the bug and will not want to stop. Start with what you can achieve easily and quickly. This is often in the familiarity of BASH scripts. Once you have conquered that, start moving forward with Python scripts and show yourself how much more you can achieve. Finally, prove yourself as a developer by creating your own programs in C.

     

     

    Hey! Don’t miss anything - subscribe to our newsletter!

    © 2022 INE. All Rights Reserved. All logos, trademarks and registered trademarks are the property of their respective owners.
    instagram Logofacebook Logotwitter Logolinkedin Logoyoutube Logo