Korn Shell Scripting, Linux Features, and Editor Basics

Korn Shell Relational Expressions

In Korn shell (and other Bourne-like shells), relational expressions are typically evaluated inside the [[ ... ]] (conditional) construct or using the test (or [) command. The operators use mnemonics for numerical comparison.

1. Numerical Comparisons in Korn Shell

(a) c ≥ d

To check if the numerical value of variable c is greater than or equal to the numerical value of variable d:

[[ $c -ge $d ]]

The -ge operator stands for “greater than or equal to”.

(b) c < d

To check if the numerical value of variable c is less than the numerical value of variable d:

[[ $c -lt $d ]]

The -lt operator stands for “less than”.

The echo Command

The echo command is a fundamental utility in Linux (and other Unix-like systems) used to display a line of text or string passed as an argument to the standard output.

  • Purpose: It’s most commonly used in shell scripts to display messages, provide output, or print the value of shell variables.
  • Basic Syntax: echo [options] [string]
  • Example: If you type echo Hello World, the shell prints Hello World on the terminal.
  • Key Feature (-e): With the -e option, echo can interpret backslash-escaped sequences, such as:
    • \n: Newline (line break)
    • \t: Horizontal tab
    • \c: Suppress trailing newline (only print the text, do not add a line break at the end)

Constants in Shell Programming

A constant in shell programming is a variable whose value is intended to remain unchanged throughout the execution of the script.

Declaring Constants

Shell scripting (like Bash, Korn shell) does not have a built-in keyword like const (as in C++ or JavaScript) to strictly enforce immutability. Instead, constants are typically implemented using conventions and the readonly command.

Using the readonly Command (The Best Way)

The readonly command makes a variable read-only, meaning its value cannot be changed or unset later in the script. This is the closest equivalent to a true constant.

readonly PI_VALUE="3.14159"
readonly SERVER_NAME="PROD-WEB-01"

If a script attempts to modify a readonly variable, the shell will issue an error.

Naming Convention (Best Practice)

By convention, shell programmers often name “constant” variables using all uppercase letters and underscores (e.g., MAX_ATTEMPTS). This signals to anyone reading the code that the variable’s value should not be modified, even if not technically marked readonly.

MAX_USERS=50
SCRIPT_VERSION="1.0"

Salient Features of the Linux Operating System

Linux is renowned for its stability, flexibility, and open-source nature. Here are four salient features:

  • Open Source: The source code is freely available to everyone. Users can inspect, modify, and redistribute the code, fostering a massive community and rapid development.
  • Multi-user and Multi-tasking:
    • Multi-user: Multiple users can access the same system resources (like CPU, memory, peripherals) simultaneously.
    • Multi-tasking: The system can run multiple applications or processes at the same time efficiently.
  • Security: Linux is highly secure, featuring a robust permission system (file and user permissions) and being less susceptible to common viruses and malware than some other OSes.
  • Portability: Linux can run on a vast range of hardware architectures, from small embedded systems and smartphones to large supercomputers. The core of Linux, the kernel, can be compiled for different platforms.

Why vi is a Visual Editor in Linux

The vi editor (or its popular clone, Vim – Vi Improved) is called a visual editor because, unlike older, non-visual editors (like ed or ex), it displays the text of the file on the screen while you edit it.

Non-Visual Editors

These older editors worked on a line-by-line basis. To view, you might have to type a command like p (print) for a line or a range, and to edit, you’d specify the line number and the change.

Visual Editors (vi)

When you open a file in vi, the contents of the file take up the entire terminal screen, and the cursor moves around the text as you type commands, giving you a visual, real-time representation of the file you are working on. This direct, full-screen interaction is what qualifies it as a “visual” editor.

Would you like a quick cheat sheet on how to save and exit from the vi editor?

Linux Installation Requirements and Shell Types

(a) Hardware Requirements for Installing Linux

The hardware requirements for installing Linux vary significantly based on the specific distribution (distro) and the desired Desktop Environment (GUI). Linux is highly scalable and can run on both very old and powerful modern hardware.

ComponentLightweight Distros (e.g., Puppy, Lubuntu, Xubuntu)Mainstream Distros (e.g., Ubuntu, Fedora, Mint)
Processor (CPU)Pentium 4 / 1 GHz single-core or newer (32-bit often supported)2 GHz Dual-Core or better (64-bit architecture is standard)
RAM (Memory)512 MB to 1 GB minimum2 GB minimum, 4 GB or more recommended for smooth desktop use
Storage (Disk)5 GB to 10 GB minimum20 GB minimum, 50 GB or more recommended for updates and applications
GraphicsBasic graphics card capable of 800×600 resolution.Graphics card capable of 1024×768 resolution and basic OpenGL support.

Key Takeaways:

  • Linux Kernel: The core Linux kernel itself has very modest requirements and can run on extremely minimal hardware (e.g., in embedded systems).
  • The Desktop Environment (GUI): The main difference in resource usage comes from the desktop environment (e.g., GNOME, KDE Plasma, XFCE). Resource-heavy GUIs like GNOME require more RAM and CPU power than lightweight options like LXQt or XFCE.
  • Installation Media: A bootable USB drive or DVD is typically required for installation.

(b) Shells in the Linux Operating System

A shell in the Linux operating system is a command-line interpreter that acts as the primary interface between the user and the Linux kernel. When a user logs in or opens a terminal, the kernel starts an instance of the shell.

1. What a Shell Does

  • Command Interpretation: It reads commands typed by the user (e.g., ls, cd, echo) or commands found in a script file.
  • Execution: It parses these human-readable commands and translates them into instructions that the kernel can understand and execute.
  • Scripting: It provides a rich programming language environment (shell scripting) that includes variables, functions, conditional statements (if/then), and loops (for, while) to automate tasks.

2. Major Types of Linux Shells

Linux supports several “flavors” of shells, generally categorized by their origin and syntax:

Shell NameFull NameOrigin/SyntaxKey Feature
shBourne ShellDeveloped by Stephen Bourne (1970s).The original and foundational Unix shell; its syntax is the basis for most modern shells.
bashBourne-Again SHellThe default shell on most modern Linux distributions (e.g., Ubuntu, Fedora).Highly popular. Backward compatible with sh but adds enhancements like command-line editing, history, and job control.
csh / tcshC Shell / TENEX C ShellSyntax is designed to resemble the C programming language.Focuses on interactive use and command history, but its scripting is generally less powerful than bash or ksh.
kshKorn ShellAn intermediate between sh and csh, developed by David Korn.Combines the scripting power of sh with the interactive features of csh; highly robust for complex scripts.
zshZ ShellBuilt on ksh and incorporates features from bash and tcsh.Gaining popularity for its highly advanced interactive features such as programmable command completion and advanced customization.

The most common shell encountered in modern Linux is bash.

Linux System Characteristics: Communication and Multitasking

Here is an explanation of two key Linux characteristics:

(a) Communications

Linux is inherently designed for robust communication both within the system (between processes) and externally (across networks).

Inter-Process Communication (IPC)

This refers to mechanisms that allow separate running programs (processes) to share information. Key IPC features in Linux include:

  • Pipes and FIFOs (Named Pipes): Simple, unidirectional data flow between related or unrelated processes.
  • Signals: A basic, asynchronous notification mechanism (e.g., SIGTERM to request a process terminate).
  • Shared Memory: Allows multiple processes to access the same region of memory, offering the fastest form of IPC.
  • Message Queues: Allows processes to exchange data packets in an organized, queued manner.
  • Semaphores: Used for synchronization to manage access to shared resources, preventing simultaneous modifications.

Networking (External Communication)

Linux has networking capabilities built directly into the kernel, making it a powerful server operating system.

  • TCP/IP Protocol Stack: Linux fully supports the entire TCP/IP protocol suite, enabling it to connect to the internet and other networks using protocols like HTTP, FTP, SSH, and more.
  • Sockets: The primary means for network communication. A socket acts as an endpoint for sending and receiving data across a network (or even locally, using Unix domain sockets).

(b) Multitasking in Linux

Multitasking is the ability of an operating system to run multiple processes or programs concurrently. Linux employs a highly efficient form of multitasking called Preemptive Multitasking.

1. Preemptive Multitasking

In preemptive multitasking, the Linux kernel (specifically the scheduler):

  • Controls Time: The kernel is in full control of how long a process runs. It allocates small slices of CPU time (called time slices) to each running process.
  • Preemption: The kernel can interrupt (preempt) a running process at any time, even if it’s not finished, to switch the CPU over to another process that needs to run. This ensures that no single process can hog the CPU, which is crucial for system responsiveness.

2. Implementation: The Scheduler

The scheduler component of the Linux kernel is responsible for deciding which process runs next and for how long.

  • Fairness: Modern Linux schedulers (like the Completely Fair Scheduler – CFS) aim to provide fair allocation of CPU time, ensuring that all runnable processes get a nearly equal share of the processor’s resources over time.
  • Priorities: Processes can be assigned different priorities (using the nice value), allowing the scheduler to give more CPU time to critical or interactive tasks and less time to background or low-priority jobs.

This mechanism gives the illusion that all programs are running simultaneously, leading to a highly responsive and stable user experience.