Unix
• Unix is also an operating system like Linux. It is an
commercial OS.
• It consists of three parts: Kernal, Shell and Programs.
• Most of the Unix and Linux commands are similar in nature.
• Users communicate with the kernel through a program
known as the shell.
• The shell is a command line interpreter; it translates
commands entered by the user and converts them into a
language that is understood by the kernel.
Linux
• Linux is an open-source operating system like other operating
systems such as Microsoft Windows, Apple Mac OS, iOS,
Google android, etc.
• Linux is around us since the mid-90s.
• It can be used from wristwatches to supercomputers.
• It is everywhere in our phones, laptops, PCs, cars and even in
refrigerators.
• It is very much famous among developers and normal
computer users.
Language used to develop Unix & Linux
• The very first version of Unix was written in Assembly
Language in the year 1969 and it was not until 1972 that
Unix was rewritten in C (K&R C to be precise), with some
parts still in Assembly.
• The reason for the persistent Assembly code was not the
inability of C but the inability of machines and compilers
of that time; compilers were bad at optimization and
even a mini-second gained by using Assembly meant a
thing.
Why use Linux?
• Linux may be a perfect operating system if you want to
get rid of viruses, malware, slowdowns, crashes, costly
repairs, and many more.
• Further, it provides various advantages over other
operating systems, and we don't have to pay for it.
• Favorable choice of Developers
• A flexible operating system
• A shell script is a computer program designed to be run by
the Unix/Linux shell which could be one of the following:
• The Bourne Shell
• The C Shell
• The Korn Shell
• The GNU Bourne-Again Shell
• A shell is a command-line interpreter and typical
operations performed by shell scripts include file
manipulation, program execution, and printing text.
• The shell is, after all, a real programming language,
complete with variables, control structures, and so
forth.
• No matter how complicated a script gets, it is still just a
list of commands executed sequentially.
# Script follows here:
echo "What is your name?"
read PERSON
echo "Hello, $PERSON"
sample run of the script −
$./test.sh
What is your name?
Zara Ali
Hello, Zara Ali
$
What is Shells?
• A Shell provides you with an interface to the Unix system.
• It gathers input from you and executes programs based on that
input.
• When a program finishes executing, it displays that program's
output.
• Shell is an environment in which we can run our commands,
programs, and shell scripts.
• There are different flavors of a shell, just as there are different
flavors of operating systems.
• Each flavor of shell has its own set of recognized commands and
functions.
Using Shell Variables
• Variable Names
• The name of a variable can contain only letters (a to z or A
to Z), numbers ( 0 to 9) or the underscore character ( _).
• By convention, Unix shell variables will have their names in
UPPERCASE
• NAME="Zara Ali"
• echo $NAME
• The above script will produce the following value −
• Zara Ali
Special Variables
#!/bin/sh
echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"
sample run for the above script −
$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2
Using Shell Arrays
• Shell supports a different type of variable called an array
variable. This can hold multiple values at the same time.
• Arrays provide a method of grouping a set of variables.
Instead of creating a new name for each variable that is
required, you can use a single array variable that stores
all the other variables.
• All the naming rules discussed for Shell Variables would
be applicable while naming arrays.
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"
$./test.sh
First Index: Zara
Second Index: Qadir
#!/bin/sh
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"
$./test.sh
First Method: Zara Qadir Mahnaz Ayan Daisy
Second Method: Zara Qadir Mahnaz
Shell Basic Operators
• There are various operators supported by each shell.
• Basic operators
• Arithmetic Operators
• Relational Operators
• Boolean Operators
• String Operators
• File Test Operators
#!/bin/sh
val=`expr 2 + 2`
echo "Total value : $val"
Total value : 4