0% found this document useful (0 votes)
100 views

Technical Report Introduction To Shell Programming: What Is A Shell in Linux ?

The document discusses shell programming in Linux. It defines a shell as the command interpreter that executes other programs and provides an interface between the user and operating system. There are two types of shells - command line shells accessed via terminals and graphical shells that manipulate GUI programs. Shell programming involves writing scripts using shell commands, variables, control flow statements, and other elements to add functionality to the shell. Common shells are Bourne, C, Korn, and Bash shells. The document outlines various shell scripting concepts like variables, control statements, loops, meta characters, and positional parameters.

Uploaded by

Abhinav arora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Technical Report Introduction To Shell Programming: What Is A Shell in Linux ?

The document discusses shell programming in Linux. It defines a shell as the command interpreter that executes other programs and provides an interface between the user and operating system. There are two types of shells - command line shells accessed via terminals and graphical shells that manipulate GUI programs. Shell programming involves writing scripts using shell commands, variables, control flow statements, and other elements to add functionality to the shell. Common shells are Bourne, C, Korn, and Bash shells. The document outlines various shell scripting concepts like variables, control statements, loops, meta characters, and positional parameters.

Uploaded by

Abhinav arora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Abhinav Arora 09

TECHNICAL REPORT
INTRODUCTION TO SHELL PROGRAMMING

WHAT IS A SHELL IN LINUX ?


The shell is the command interpretor in an operating system such as Unix or GNU/Linux, it is a
program that executes other programs. It provides a computer user an interface to the
Unix/GNU Linux system so that the user can run different commands or utilities/tools with
some input data.
When the shell has finished executing a program, it sends an output to the user on the screen,
which is the standard output device. For this reason, it is referred to as the “command
interpretor”.

FIG: SHELL IN LINUX

Basically there are 2 categories of shells in Linux OS:


1. Command line Shell: Shell can be accessed by using the command line interfaces
present in the operating systems .
Eg: - In Linux OS the shell can be accessed by the terminal and in Windows OS it can be
accessed by command prompt.
2. Graphic Shell: These Shells manipulate the programs according to the Graphical User
Interface (GUI).
It allows operations such as:
 Opening window
 Closing window
 Resizing window

ABOUT SHELL PROGRAMMING:


Abhinav Arora 09

Shell programming is a type of programming done on the Linux shell. As the shell is the
interface between user and the kernel it requires certain commands so that it helps the user to
interact with the kernel, this interaction is done by scripting on the shell. The shell
programming is done on the Terminal present in Ubuntu. The program we write on the shell is
also known as SHELL SCRIPT. Shell script has one main advantage that it can store long and
repetitive commands into simple script.
Basic steps to write shell script are:
1. Create a file using a editor. Name script file with extension .sh.
2. Start the script with #! /bin/sh .
3. Write some code.
4. Save the script file as filename.sh
5. For executing the script type bash filename.sh
Shell scripting is very essential to add some new functionality to the shell of the OS. A shell
script can contain the following elements in it: -
1. Shell Keywords – if, else, break etc.
2. Shell commands – cd, ls, echo, pwd, touch etc.
3. Functions
4. Control flow – if..then..else, case and shell loops etc. A basic shell script can be seen
below: -
#!/bin/sh a=10 b=20
if [ $a == $b ] then
echo "a is equal to b" else
echo "a is not equal to b"
fi

TYPES OF SHELLS IN LINUX


Basically there are 2 major types of shells in Linux OS that are: -
 Bourne shell − If you are using a Bourne-type shell, the $ character is the default
prompt.
 C shell − If you are using a C-type shell, the % character is the default prompt. Sub
categories of these to shells are: -

The Bourne Shell has the following sub-categories:


1. Bourne shell (sh)
2. Korn shell (ksh)
3. Bourne Again shell (bash)
4. POSIX shell (sh)
Abhinav Arora 09

The different C-type shells follow:


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

SHELL VARIABLES
Like in other languages the variables are the unallocated memory locations that can be
assigned manual values or user can give its input. Same is the case in the Linux shell the shell
variables are the pointer to a memory location that contains the data. Shell variables can
contain alphabets, numbers & ‘ _ ’ sign.
Eg: VAR_1
Variable name should always be in uppercase letters. Declaration of variable can be done by:
variable_name=variable_value
By this the variable can get its value.
To access variable $ sign can be used before the variable as shown below.

Read only variables: When a variable is marked as read only that means the vale of that
variable gets permanent and cannot be changed.
Variable can get their values deleted from the shell by giving unset command.
unset variable_name
You cannot use the unset command to unset variables that are marked read only.
TYPES OF VARIABLES
There are 3 types of variables:
1. Local Variables − A local variable is a variable that is present within the current
instance of the shell. It is not available to programs that are started by the shell. They are
set at the command prompt.
2. Environment Variables − An environment variable is available to any child process of
the shell. Some programs need environment variables in order to function correctly.
Usually, a shell script defines only those environment variables that are needed by the
programs that it runs.
Abhinav Arora 09

3. Shell Variables − A shell variable is a special variable that is set by the shell and is
required by the shell in order to function correctly. Some of these variables are
environment variables whereas others are local variables.

SHELL KEYWORD
Shell keywords can be defined as the words whose meaning is predefined in the shell.
Let us take example of keyword echo. Echo keyword is used to print in Linux shell
programming.

Other examples of keywords are: read, set, unset, else, while, done, etc.

CONTROL STATEMENTS IN SHELL SCRIPTING


As the name tells us the control statements are those that control the flow of a particular
program. Like in c/c++ the control statements are if, else if, else, switch,loops & break . Linux
shell scripting also has similar type of control statements.
In shell scripting if….elif…fi is basically used in place of if, else if .
 Basic structure:
If(condition)
<command>
elif(condition)
<command>
fi
<command>
fi is basically the else statement that ends the loop.
 Example:
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
Abhinav Arora 09

LOOP STATEMENTS:
Basic 2 loop statements that are used in the shell scripts are: -
1. For loop
2. While loop
“done” command is used to end the loop.

 While loop
It works according to the command given by the user. If the command becomes false the
loop terminates.
Syntax:-
while command
do
Statement to be executed
done
Example:
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done
Abhinav Arora 09

 For loop
For loop works on a particular number of items. It repeats a set of commands for every
item in a list.
Syntax:
for var in word1 word2 ...wordn
do
Statement to be executed
Done
Example:
for i in 1 2 3 4 5
do
echo "Looping ... number $i"
done

To alter the flow of loop statements, two commands are used:


 break
 continue

SHELL META CHARACTERS


Shell meta characters or special characters are some of the characters used in shell scripting
that have a unique meaning.

? Question Matches any single character. The character must exist.


Mark

* Asterisk Matches zero or more (any number of) characters.

[] Square Match a single character that is among the list off character found
Brackets inside the brackets.
Abhinav Arora 09

$ Dollar Indicates that what follows is a variable name or an integer math


Sign problem or a command to be performed. The resolution of the $ will
be placed at this location.

# Pound The comment characters. The shell will ignore anything that follows.
Sign Comments are used by programmers to document their work.
Comments are part of a well written script.

' Single Shell will ignore all special character that are inside the single quotes
Quote and will pass the contents as a single argument to the command or
shell.

" Double Shell will ignore most special characters that are inside the double
Quote quotes and will pass the contents as a single argument to the
command or shell. Noted exceptions are dollar signs ($), back quotes
(`) and backslashes (\).

\ Back Escapes the character that follows so that it either adds or removes
Slash the special meaning of the next character. Also if it is the last character
on the line it is used to extend the line.

` Back Escapes the character that follows so that it either adds or removes
Quote the special meaning of the next character. Also if it is the last character
on the line it is used to extend the line.

POSITIONAL PARAMETERS
A positional parameter is an argument specified on the command line, used to launch the
current process in a shell. Positional parameter values are stored in a special set of variables
maintained by the shell.
Bash shell uses positional parameters.
Positional parameters are numbered and are referred to with a preceding ``$'': $1, $2, $3, and
so on.

COMMANDS IN LINUX OPERATING SYSTEM


ENVIRONMENTAL VARIABLES
1. HOME - The home directory of the current user.
2. EDITOR - The default file editor to be used. This is the editor that will be used when you
type edit in your terminal.
3. SHELL - The path of the current user's shell, such as bash or zsh.
4. LOGNAME - The name of the current user.
5. PATH - A list of directories to be searched when executing commands. When you run a
command the system will search those directories in this order and use the first found
executable.
6. LANG - The current locales settings.
7. TERM - The current terminal emulation.
Abhinav Arora 09

8. MAIL - Location of where the current user's mail is stored.

FILE SYSTEM COMMANDS


1. touch: Create a new file or update its timestamp.
Syntax: touch [OPTION]…[FILE]
2. cat: Concatenate files and print to stdout.
Syntax: cat [OPTION]…[FILE]
3. cp: Copy files
Syntax: cp [OPTION]source destination
4. mv: Move files or rename files
Syntax: mv [OPTION]source destination
5. rm: Remove files and directories
Syntax: rm [OPTION]…[FILE]
6. mkdir: Make directory
Syntax: mkdir [OPTION] directory
7. rmdir: Remove a directory
Syntax: rmdir [OPTION] directory
8. ls: list contents of directory
Syntax: ls [OPTION] directory
Abhinav Arora 09

INPUT OUTPUT COMMANDS


1. echo: command in linux is used to display line of text/string that are passed as an
argument .
Syntax: echo [option] [string]
2. read: read command in Linux system is used to read from a file descriptor
Syntax: read [user input]
3. I/O Redirections: Redirection can be defined as changing the way from where
commands read input to where commands sends output. You can redirect input and
output of a command.
 >: standard output
 < : standard input
 >> : standard error
Abhinav Arora 09

GENERIC COMMANDS
1. help: help you to learn about any built-in command.
Syntax: help [ command]
2. cd: helps to change directory.
Syntax: cd [option] [directory]
3. whatis: Open a small manual of a command.
Syntax: whatis keywords
4. apropos: Open manual of commands related to a particular command.
Syntax: apropos keywords
Abhinav Arora 09
Abhinav Arora 09

NETWORK COMMANDS
1. ping: To check connectivity between two nodes.
2. Dig: Query DNS related information.
3. Whois: Will tell you about the website's whois.
4. Wget: Downloads the file.
Abhinav Arora 09

PROCESS BASED COMMANDS


1. ps: ps command is used to provide information about the currently running processes,
including their process identification numbers (PIDs).
Syntax: ps [options]
2. ps aux: gives details of modules is given. Collectively the options "aux" print all the
running process in system regardless from where they have been executed.
a = show processes for all users
u = display the process's user/owner
x = also show processes not attached to a terminal
3. kill: send a signal to one or more processes (usually to "kill" a process)
Syntax: kill pid
4. Killall Command: Kill processes by name. Instead of specifying a process by its PID, you
can specify the name of the process.
Example: Kill all the firefox processes
$ killall -9 firefox

SYSTEM INFORMATION COMMANDS


Abhinav Arora 09

1. date: command is used to display the system date and time. date command is also used
to set date and time of the system.
Syntax:
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
2. uptime: It is used to find out how long the system is active (running). This command
returns set of values that involve, the current time, and the amount of time system is in
running state, number of users currently logged into, and the load time for the past 1, 5
and 15 minutes respectively.
Syntax:
uptime [-options]
3. whoami: It is basically the concatenation of the strings “who”,”am”,”i” as whoami.
It displays the username of the current user when this command is invoked.
Syntax: whoami google.com
4. free: provides information about the total amount of physical and swap memory as well
as the free and used memory and swap space in the system.
Syntax:
free [OPTIONS]
du command, short for disk usage, is used to estimate file space usage.
5. du: The du command can be used to track the files and directories which are consuming
excessive amount of space on hard disk drive.
Syntax:
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F
6. df: The df command is a command line utility for reporting file system disk space usage.
It can be used to show the free space on a Unix or Linux computer and to understand the
filesystems that have been mounted.
Syntax:
df [OPTION]... [FILE]...
If no file name is given, it displays the space available on all currently mounted file
systems.
7. Uname: This command return the name of operating system. Also can be said as gives
the information of the kernel information.
Syntax: $uname
Abhinav Arora 09
Abhinav Arora 09

COMPRESSION FILE COMMANDS


1. Extract a single file
To extract a single file out of an archive just add the file name after the command like
this
Syntax:
$ tar -xf abc.tar.gz
2. Compress
The tar command can be told to put selected files in an archive or an entire directory.
$ tar -cf abc.tar
Xyz.docx using compression to xyz.docx.tar
xyz.docx.tar using extraction to xyz.docx

PERMISSION COMMAND
Abhinav Arora 09

There are two parts to the file control mechanism, namely Classes and Permissions. Classes
determines who can access the file while the Permissions determines the kind of action the
user can do to the file.
There are three classes: owner, group, others.
 The owner is the usually the creator of the files/folders. In linux, files or folders that you
created in your home directory are usually owned by you, unless you change the
ownership.
 The Group contains a group of users who share the same permissions and user privilege.
 Others mean the general public.
As for permissions, there are 3 type of actions that you can perform on a file/folder. You can
read, write or execute.
 Read – You can only view the file, but cannot modify the content of the file. When
applied on Folder, you can only view the files in the folder, but you can’t delete from or
add files into the folder.
Read is equivalent to ‘4’.
 Write – You can edit and modify the file. For Folders, you can delete and add files into
the folder.
Write is equivalent to ‘2’.
 Execute – Execute is mainly used when you need to run the file (commonly used when
you need to run a script).
Execute is equivalent to ‘1’

Chmod:
Supports octal values
r-4 read
w-2 write
x-1 execute

REFERENCES:
Abhinav Arora 09

i. https://www.geeksforgeeks.org/introduction-linux-shell-shell-scripting/
ii. https://www.guru99.com/introduction-to-shell-scripting.html
iii. https://www.tecmint.com/linux-io-input-output-redirection-operators/
iv. https://www.livefirelabs.com/unix_tip_trick_shell_script/unix_operating_system_funda
mentals/unix-special-characters.htm
v. https://www.geeksforgeeks.org/looping-statements-shell-script/
vi. https://www.tutorialspoint.com/unix/if-elif-statement.htm

You might also like