[Go to site: main page, start]

Beginner's Guide to Shell Scripting

0% found this document useful (0 votes)
122 views6 pages

Uploaded by

Yash Bhise
  • Introductory Overview
  • Details on Shell Types
  • Writing Shell Scripts
  • Handling Variables

Shell Scripting Tutorial: How to Create

Shell Script in Linux/Unix


Shell Scripting
Shell Scripting is an open-source computer program designed to be run by
the Unix/Linux shell. Shell Scripting is a program to write a series of
commands for the shell to execute. It can combine lengthy and repetitive
sequences of commands into a single and simple script that can be stored
and executed anytime which, reduces programming efforts.

This Shell Scripting tutorial helps to learn a basic understanding of the


Linux/Unix shell scripting program to advanced concepts of Shell Scripting.
This Shell Script tutorial designed for beginners and professionals who want
to learn What is Shell Scripting? How shell scripting works, types of shell, and
more.

What is Shell?
Shell is a UNIX term for an interface between a user and an operating system
service. Shell provides users with an interface and accepts human-readable
commands into the system and executes those commands which can run
automatically and give the program’s output in a shell script.

An Operating is made of many components, but its two prime components are

 Kernel
 Shell
Componen
ts of Shell Program
A Kernel is at the nucleus of a computer. It makes the communication
between the hardware and software possible. While the Kernel is the
innermost part of an operating system, a shell is the outermost one.

A shell in a Linux operating system takes input from you in the form of
commands, processes it, and then gives an output. It is the interface through
which a user works on the programs, commands, and scripts. A shell is
accessed by a terminal which runs it.

When you run the terminal, the Shell issues a command prompt (usually
$), where you can type your input, which is then executed when you hit the
Enter key. The output or the result is thereafter displayed on the terminal.

The Shell wraps around the delicate interior of an Operating system protecting
it from accidental damage. Hence the name Shell.
This Unix/Linux Shell Script tutorial helps understand shell scripting basics to
advanced levels.

In this Shell Script tutorial, you will learn-

 What is Shell Scripting?


 What is a Shell?
 Types of Shell
 How to Write Shell Script in Linux/Unix
 Adding shell comments
 What are Shell Variables?
Types of Shell
There are two main shells in Linux:

1. The Bourne Shell: The prompt for this shell is $ and its derivatives are
listed below:

 POSIX shell also is known as sh


 Korn Shell also knew as sh
 Bourne Again SHell also knew as bash (most popular)

2. The C shell: The prompt for this shell is %, and its subcategories are:

 C shell also is known as csh


 Tops C shell also is known as tcsh

We will discuss bash shell based shell scripting in this tutorial.

How to Write Shell Script in Linux/Unix


Shell Scripts are written using text editors. On your Linux system, open a text
editor program, open a new file to begin typing a shell script or shell
programming, then give the shell permission to execute your shell script and
put your script at the location from where the shell can find it.

Let us understand the steps in creating a Shell Script:

1. Create a file using a vi editor(or any other editor). Name script file
with extension .sh
2. Start the script with #! /bin/sh
3. Write some code.
4. Save the script file as [Link]
5. For executing the script type bash [Link]

“#!” is an operator called shebang which directs the script to the interpreter
location. So, if we use”#! /bin/sh” the script gets directed to the bourne-shell.

Let’s create a small script –


#!/bin/sh
ls
Let’s see the steps to create Shell Script Programs in Linux/Unix –
Steps to Create Shell Script in Linux/Unix
Command ‘ls’ is executed when we execute the scrip [Link] file.

Adding shell comments


Commenting is important in any program. In Shell programming, the syntax to
add a comment is
#comment
Let understand this with an example.
What are Shell Variables?
As discussed earlier, Variables store data in the form of characters and
numbers. Similarly, Shell variables are used to store information and they can
by the shell only.

For example, the following creates a shell variable and then prints it:
variable ="Hello"
echo $variable
Below is a small script which will use a variable.
#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"
Let’s understand, the steps to create and execute the script
As you see, the program picked the value of the variable ‘name’ as Joy and
‘remark’ as excellent.

This is a simple script. You can develop advanced scripts which contain
conditional statements, loops, and functions. Shell scripting will make your life
easy and Linux administration a breeze.

Common questions

Powered by AI

The terminal is crucial as it provides access to the shell, the command-line interface for interacting with the operating system. It is the medium through which users enter commands, which are then interpreted and executed by the shell. The terminal displays the command prompt, facilitates command execution, and shows output, thereby serving as both the input and communication interface for users working with shell scripts .

The kernel acts as the core of the operating system, facilitating communication between hardware and software, whereas the shell serves as the user interface that allows users to input commands and execute programs, effectively providing a bridge between the user and the kernel .

Commenting is important in shell scripting as it allows programmers to add explanations or annotations to their code, which aids in understanding and maintaining the script. Comments are added using the '#' symbol followed by the comment text .

Shell variables store data such as characters and numbers and are only interpretable by the shell. They can be utilized in scripts to store and manipulate information. For example, a variable can be created using 'variable="Hello"', and its value can be accessed using 'echo $variable'. A simple script can prompt the user for input, store it in a variable, and then use that variable in subsequent operations .

The Bourne Shell, indicated by the '$' prompt, includes derivatives like the POSIX shell (sh), Korn Shell (ksh), and Bourne Again SHell (bash), which is particularly popular. The C Shell, indicated by the '%' prompt, includes the C shell (csh) and the Tops C shell (tcsh). Each of these shells has unique features and capabilities suited to different scripting needs .

A 'shebang' is an operator '#!' placed at the start of a shell script to indicate which interpreter should be used to execute the script. For example, '#! /bin/sh' directs the script to the bourne-shell interpreter. It is crucial for ensuring the script runs under the correct shell .

Shell scripting can reduce programming efforts by allowing lengthy and repetitive command sequences to be condensed into a single, executable script, thereby automating routine tasks. This automation helps system administrators efficiently manage system operations, perform routine backups, and automate monitoring tasks, making Linux administration more streamlined and less error-prone .

To create and execute a shell script in a Linux environment using bash, follow these steps: 1) Create a file using a text editor with a .sh extension. 2) Begin the script with '#! /bin/sh' to direct it to the interpreter. 3) Write the code within the file. 4) Save the script as filename.sh. 5) Use the command 'bash filename.sh' to execute the script .

A simple shell script using a conditional statement might look like: ``` #!/bin/bash echo "Enter a number:" read number if [ $number -gt 10 ]; then echo "The number is greater than 10." else echo "The number is 10 or less." fi ``` This script prompts the user for a number, stores it in a variable, and uses an 'if' statement to check if the number is greater than 10. It then prints a message based on the condition's outcome, utilizing conditional logic to perform different actions .

User inputs are taken by the shell script through commands inputted in the terminal. During execution, the shell processes these inputs and executes the corresponding script commands. The output is displayed on the terminal, giving users immediate feedback and results from their inputs. Variables can be used to store input data, which allows scripts to interact dynamically with users .

Shell Scripting Tutorial: How to Create 
Shell Script in Linux/Unix
Shell Scripting
Shell Scripting is an open-source compute
Componen
ts of Shell Program
A Kernel is at the nucleus of a computer. It makes the communication 
between the hardware and s
Types of Shell
There are two main shells in Linux:
1. The Bourne Shell: The prompt for this shell is $ and its derivatives ar
Steps to Create Shell Script in Linux/Unix
Command ‘ls’ is executed when we execute the scrip sample.sh file.
Adding shell co
What are Shell Variables?
As discussed earlier, Variables store data in the form of characters and 
numbers. Similarly, Shell
As you see, the program picked the value of the variable ‘name’ as Joy and 
‘remark’ as excellent.
This is a simple script. Y

You might also like