Technical Report Introduction To Shell Programming: What Is A Shell in Linux ?
Technical Report Introduction To Shell Programming: What Is A Shell in Linux ?
TECHNICAL REPORT
INTRODUCTION TO SHELL PROGRAMMING
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
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.
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
[] Square Match a single character that is among the list off character found
Brackets inside the brackets.
Abhinav Arora 09
# 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.
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
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
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