Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX What is a Shell Script


May 23, 2021 UNIX Getting started


Table of contents


What is a shell script

The shell is the user's interface to the Unix-operated system. I t receives input from the user and then executes the program based on that input. When the program is finished, the results are displayed on the monitor.

Shell is the operating environment in which instructions, programs, and shell scripts are run. J ust as there can be many kinds of operating systems, there are many kinds of shells. E ach shell has its own specific set of instructions and functions.

Shell prompt

The prompt $ called the command prompt. W hen the command prompt is displayed, the user can type the command.

Shell reads input information from the user's input device after the user presses Enter, and it learns the command that the user wants to execute by looking at the first word the user enters. A word is usually a space and tab split word, even if the character is not divided into strings.

Here is an example of a date instruction that displays the current date and time on the display:

    $date
    Thu Jun 25 08:30:19 MST 2009

Users can also customize their favorite command prompts by changing the environment variable PS1.

The shell type

There are two main shells in the Unix system:

  • Bourne shell: If the user uses the bourne shell, the default command prompt $ .
  • C shell: If the user uses the bourne shell, the default command prompt is % .

Bourne shell also has several seed categories:

  • Bourne shell ( sh)
  • Korn shell ( ksh)
  • Bourne Again shell ( bash)
  • POSIX shell ( sh)

The different types of C shells are as follows:

  • C shell ( csh)
  • TENEX/TOPS C shell ( tcsh)

The original UNIX Shell was written by Stephen R. Bourne in the mid-1970s. A t the time, he worked at the AT&T Bell Labs in New Jersey.

The Bourne shell was the first shell to appear in the Unix system, so it is called the standard "shell".

Bourne shells are typically the /bin/sh Unix. For this reason, this shell is also chosen to script on different versions of Unix.

In this tutorial, we'll cover most of the concepts in the Bourne shell.

Shell script

The main form of a shell script is a series of commands that are executed sequentially. A good style shell will be annotated accordingly.

Shell scripts have conditional statements (A is greater than B), circular statements, read files and store data, read variables, and store data, and of course, shell scripts also include functions.

Shell scripts and functions are translated languages, so they are not compiled.

In the later sections, we'll try to write some scripts. They are simple text files with commands written.

An example of a script

Let's say we create a test.sh called a script. N ote that the suffix name of all scripts must .sh. A ssuming that the user has added some commands to it before, here's how to start the script. Examples are as follows:

    #!/bin/sh

This command tells the system that behind it is the bourne shell which should be read as shebang, because the hashtag is called hash, ! It's called bang

In order to create a script that contains these instructions, the user needs to type the shebang line first, and then the instructions:

    #!/bin/bash
    pwd
    ls

Shell comments

You can add comments to the script as if:

    #!/bin/bash

    # Author : Zara Ali
    # Copyright (c) Tutorialspoint.com
    # Script follows here:
    pwd
    ls

Now that the user has saved the above, they can do it:

    $chmod +x test.sh

The script is executed as follows:

    $./test.sh

This outputs the following results:

    /home/amrood
    index.htm  unix-basic_utilities.htm  unix-directories.htm  
    test.shunix-communication.htmunix-environment.htm

Note: If you want to execute a script in the current directory, you need to do so in the program_name

Extended shell script:

Shell scripts have several constructs that tell the shell environment what to do and when to do it. Of course, most scripts are much more complex than above.

After all, Shell is a true programming language that can have variables, control structures, and so on. No matter how complex the script, it is still just a list of commands executed sequentially.

The following script uses the read command to enter from the keyboard and assign it to the variable PERSON, and finally print STDOUT.

    #!/bin/sh

    # Author : Zara Ali
    # Copyright (c) Tutorialspoint.com
    # Script follows here:

    echo "What is your name?"
    read PERSON
    echo "Hello, $PERSON"

Here's an example of running the script:

    $./test.sh
    What is your name?
    Zara Ali
    Hello, Zara Ali
    $