Week06-ShellScripting

Week 6 Shell Scripting

What is a script?

  • In computing, the term "script" refers to a file containing a list of commands or instructions that can be executed by a certain program or scripting engine without needing prior compilation.

  • Essentially, a script is a program written in a scripting language.

What is a shell?

  • The term "shell" in computing has a rich history and multiple layers of meaning. At its core, a shell provides a user environment where commands can be entered, allowing users to interact with the operating system or a program.

  • That’s why we say python “shell”, because it serves as an interactive interface layer that facilitates direct communication between the user and the software.

  • The shell we’re dealing with today is any command-line interpreter used in Unix-like operating systems, including Linux and macOS, as well as shells available on Windows, like PowerShell.

What is a shell script?

  • A shell is a user environment where the user uses commands to interact with the operating system.

  • A script is a list of commands that can be executed.

  • When commands that can be run in a shell are strung together and executed in batch, we have a shell script.

Example: A Simple Backup Script

Here's an example of a bash script that creates a backup of Word files:

And here's the equivalent PowerShell script:

Running a shell script

While unix-like systems reads the shebang (#!) line to determine which interpreter should be used to execute a script, Windows Powershell relies on the extension name of the file for that decision. To run a shell script in a unix shell, first make it executable

Then invoke the script path

To run a shell script in powershell, we need to change the execution policy, as powershell restricts script execution by default.

Then invoke the script path

Shell scripts are used for:

  • Automation of development tasks (build, test, deploy etc);

  • Continuous integration and development (CI/CD);

  • System configuration and provisioning;

  • Database management;

  • Process control and management;

  • File manipulation and data processing;

  • And more…

Things we can do in a shell script

  • Print a message;

  • Define a variable;

  • Use a variable;

  • Get user input;

  • Use conditionals;

  • Use loops;

  • Run another script.

Define and use variables

Here's how to define and use variables in bash:

And here's the equivalent in PowerShell:

Get user input

Here's how to get user input in bash:

And here's the equivalent in PowerShell:

Conditionals

Here's how to use conditionals in bash:

And here's the equivalent in PowerShell:

For loops

Here's how to use for loops in bash:

And here's the equivalent in PowerShell:

While loops

Here's how to use while loops in bash:

And here's the equivalent in PowerShell:

Running another script

Here's how to run another script in bash:

And here's the equivalent in PowerShell:

Other things we can do in a shell script

  • Define a function

  • Call a function

  • Accept parameters

  • Run other programs

  • Redirect input and output

  • Access environment variables

Define and call a function

Here's how to define and call functions in bash:

And here's the equivalent in PowerShell:

Accept parameters

Here's how to accept parameters in bash:

And here's the equivalent in PowerShell:

The PATH

  • The PATH environment variable is a critical system variable that lists directories where the operating system looks for executable files.

  • When a command is entered in the shell, the directories in the PATH variable are searched in the order they are listed. Once the executable is found, the search stops, and the system attempts to execute the found file.

Adding Java to the PATH

For Windows Powershell:

For Unix shell:

Output redirection

Let’s start by saving the printed message to a file:

You should see the message “Hello world!” if you open out.txt.

To open a file with the default application, use start <file_path> in Windows Powershell, and open <file_path> in a Unix shell.

  • Output redirection allows the standard output (stdout) and/or standard error (stderr) streams of a command or program to be directed to a location other than the terminal or console.

  • This can be a file, another command (via piping), or even ignored (discarded).

  • In output redirection, > overwrites the existing content and >> appends after the existing content.

output redirection is used for:

  • Logging and debugging

    • Developers often redirect stderr to a separate file to isolate error messages from regular program output.

  • Data processing pipelines

    • Output from one command or program can be redirected to serve as input to another, forming a pipeline that processes data in stages.

  • Continuous integration and deployment(CI/CD)

    • In CI/CD pipelines, output from build processes, tests, or deployment scripts can be redirected to files or tools that monitor and analyze the process.

  • Testing and quality assurance

    • Output redirection can be used to capture the output of tests and compare it against expected outcomes or to generate reports.

Many applications keeps a running log when in execution. During development, developers look at the log in real time to get an idea of how the application is working and to debug errors.

When the application is running on production server, it is usually redirected into a log file so that when an error is reported, the developer can look into the log file and understand what happened at the time of the error.

Input redirection

Input redirection is used for:

  • Data Processing Pipelines

    • When combined with output redirection and piping, the output of one program becomes the input to another, allowing for complex data transformations and analyses.

  • Scripting and Automation

    • Scripts that automate system tasks or data processing can use input redirection to read from configuration files or data sources.

  • Streamlining Database Operations

    • Database utilities that interact with SQL databases often allow executing SQL commands from a file, making input redirection useful for automating database backups, restores, queries, and script executions.

Environment variables

  • Environment variables are key-value pairs stored in the operating system's environment where running processes can access them.

  • They play a vital role in software development, system administration, and the daily operation of systems by providing a way to influence the behavior of software and scripts without modifying code.

Environment variables are used for:

  • Configuration

    • Allow applications to behave differently in different execution environments (development, testing, production), without changing the application's source code.

  • Secret management

    • Store sensitive information, such as API keys, passwords, and other credentials, keeping them out of source code to enhance security.

  • System information

    • Provide information about the system, like the current user's home directory (HOME on Unix/Linux, USERPROFILE on Windows), or the system's path variable (PATH), which dictates where the operating system looks for executable files.

Reading environment variable: Unix shell:

Windows Powershell:

Setting environment variable: Unix shell:

Windows Powershell:

In your own shell, set the environment variable “user” to your name, then print the message “The current user is <your_name>” by reading that environment variable.

[!TIP] đź”— Practice

Last updated