SlideShare a Scribd company logo
Shell Scripting
Chitrakshi Jaiswal
• Basics ………..…3
• First script ______________________________ ………….…4
• Variables and Comments ………….…5
• System defined Variables __________________ ………….…6
• User defined variables ____________________ ………….…7
• Use of Backslash character ________________ ………….…9
• Backtick Symbol _________________________ ………….…11
• Read User Input ………….…12
• Use of “-p” (Read as it is) __________________ ………….…13
• For multiple Variables ____________________ ………….…14
• Hide input _____________________________ ………….…15
2
PART-1
Basics
Q. What is shell script and how to check shells of your system ?
Ans. A shell script is a text file that contains a sequence of commands for
a Linux-based operating system. It's called a shell script because it
combines into a "script" in a single file a sequence of commands that
would otherwise have to be presented to the system from a keyboard
one at a time.
To check shell (operating system's command interpreter ) use following
command “cat /etc/shells “
3
First Script…
Let’s start our first script with “echo” command……..
Task : To print “my world”
Step 1: vi test1.sh (“test1.sh” is my script name)
Step 2: Write following commands in your file
#! /bin/bash (For Interpreter -Specify shell to execute program)
echo “my world” (Here, “my world” is desired o/p; save and exit this
file)
*Always give execution permission to your SCRIPTS
• chmod +x test1.sh
Output: Now, Run your script
sh test1.sh ---------->
4
Variables and comments
• Comments : Use # to make anything commented. Generally it uses to
explain about your commands or to give details about your script.
• Variables : Which store some data in itself. Here, we have 2 types of
variables.
a) System defined
b) User defined
5
a. System defined Variables are created and maintained by your Linux
system, they are pre-defined in your system.
• Generally these variables are defined in CAPITAL LETTERS.
• We can tap into these environment variables from within your scripts by
using the environment variable’s name preceded by a dollar sign. This is
demonstrated in the following script 
6
b. User defined Variables are case sensitive, so the variable Var1 is different from
the variable var1.
• Values are assigned to user variables using an equal sign. (There is no space
b/w var and def. var)
• Here are a few examples of assigning values to user variables:
i. var1=10
ii. var2=-57
iii. var3=testing
iv. var4=“still more testing”
• The shell script automatically determines the data type used for the variable
value.
7
Example 1:
(Just like system variables, user variables can be referenced using the
dollar sign)
Output : (Each time the variable is referenced, it produces the value
currently assigned to it.)
8
Example 2: (Look at what happens in this example)
Output :
• That is obviously not what was intended. Whenever the script sees a dollar
sign within quotes, it assumes you’re referencing a variable. In this example
the script attempted to display the variable $1 (which was not defined),
and then the number 5. To display an actual dollar sign, you must
precede it with a backslash character.
9
• The backslash allowed the shell script to interpret the dollar sign as
an actual dollar sign, and not a variable.
Example 3:
Output :
10
Example 4: (Use of Backtick symbol (`) in shell variables)
The shell runs the command within the backticks and assigns the output to
the variable testing
Output :
Note : In bash you can also use the alternative $(…) syntax in place of
backtick (`),which has the advantage of being re-entrant.
11
Read User Input
• read command is used for getting user input.
Example 1:
Output : (Here, cj is the user input)
12
When we want input at same place  Use “-p”
Example 2:
Output : (Compare this with Example1)
13
For Multiple variables/User’s inputs 
Example 3 :
Output :
14
When we don’t want to show input  Use “-s”
Example 4 :
Here, I have used an extra echo
which makes output like this. (w/o echo
o/p ‘ll be like this)
Output : (Here, Input for Password is not visible)
15
PART-2
To be continued……….
16
• Array
• Input - Output redirection
• Pipes
• Control structures (if)
• Loops
Ad

Recommended

Basics of C porgramming
Basics of C porgramming
baabtra.com - No. 1 supplier of quality freshers
 
C language (Part 2)
C language (Part 2)
Dr. SURBHI SAROHA
 
Cross platform native development in f#
Cross platform native development in f#
David Kay
 
Variables and User Input
Variables and User Input
primeteacher32
 
Class 8 - Database Programming
Class 8 - Database Programming
Ahmed Swilam
 
Php & my sql
Php & my sql
Norhisyam Dasuki
 
Data types in php
Data types in php
ilakkiya
 
03 ohp slides 1
03 ohp slides 1
Anne Lee
 
Operators used in vb.net
Operators used in vb.net
Jaya Kumari
 
Bn1017 a demo rdbms
Bn1017 a demo rdbms
conline training
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)
harman kaur
 
Lesson 4 constant
Lesson 4 constant
MLG College of Learning, Inc
 
Subroutines
Subroutines
primeteacher32
 
Mca ii dfs u-2 array records and pointer
Mca ii dfs u-2 array records and pointer
Rai University
 
Subroutines in perl
Subroutines in perl
sana mateen
 
Unix ppt
Unix ppt
ashish kumar
 
Lesson 2 php data types
Lesson 2 php data types
MLG College of Learning, Inc
 
Conditionalstatement
Conditionalstatement
RaginiJain21
 
PerlTesting
PerlTesting
Aureliano Bombarely
 
Php1
Php1
Keennary Pungyera
 
Storage classes
Storage classes
Shanmughaneethi Velu
 
Generic programming in java
Generic programming in java
anshu_atri
 
testing add
testing add
alind tiwari
 
Subroutines
Subroutines
Krasimir Berov (Красимир Беров)
 
C Language Part 1
C Language Part 1
Thapar Institute
 
Introduction to C#
Introduction to C#
Raghuveer Guthikonda
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
Rasan Samarasinghe
 
c & c++ logic building concepts practice.pptx
c & c++ logic building concepts practice.pptx
rawatsatish0327
 
JAVA programming language made easy.pptx
JAVA programming language made easy.pptx
Sunila31
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
AbrehamKassa
 

More Related Content

What's hot (16)

Operators used in vb.net
Operators used in vb.net
Jaya Kumari
 
Bn1017 a demo rdbms
Bn1017 a demo rdbms
conline training
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)
harman kaur
 
Lesson 4 constant
Lesson 4 constant
MLG College of Learning, Inc
 
Subroutines
Subroutines
primeteacher32
 
Mca ii dfs u-2 array records and pointer
Mca ii dfs u-2 array records and pointer
Rai University
 
Subroutines in perl
Subroutines in perl
sana mateen
 
Unix ppt
Unix ppt
ashish kumar
 
Lesson 2 php data types
Lesson 2 php data types
MLG College of Learning, Inc
 
Conditionalstatement
Conditionalstatement
RaginiJain21
 
PerlTesting
PerlTesting
Aureliano Bombarely
 
Php1
Php1
Keennary Pungyera
 
Storage classes
Storage classes
Shanmughaneethi Velu
 
Generic programming in java
Generic programming in java
anshu_atri
 
testing add
testing add
alind tiwari
 
Subroutines
Subroutines
Krasimir Berov (Красимир Беров)
 

Similar to Shell scripting - Basic (PART -1) (20)

C Language Part 1
C Language Part 1
Thapar Institute
 
Introduction to C#
Introduction to C#
Raghuveer Guthikonda
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
Rasan Samarasinghe
 
c & c++ logic building concepts practice.pptx
c & c++ logic building concepts practice.pptx
rawatsatish0327
 
JAVA programming language made easy.pptx
JAVA programming language made easy.pptx
Sunila31
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
AbrehamKassa
 
PL SQL.pptx in computer language in database
PL SQL.pptx in computer language in database
ironman82715
 
PPT 19.pptx
PPT 19.pptx
DrRavneetSingh
 
Escape Sequences and Variables
Escape Sequences and Variables
yarkhosh
 
Chapter 2: Introduction to Bash Scripting
Chapter 2: Introduction to Bash Scripting
azzamhadeel89
 
JS-Slides-2_74526582652945_73562876535.ppt
JS-Slides-2_74526582652945_73562876535.ppt
goddemon7675
 
UNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
 
C programming session7
C programming session7
Keroles karam khalil
 
C programming session7
C programming session7
Keroles karam khalil
 
qb unit2 solve eem201.pdf
qb unit2 solve eem201.pdf
Yashsharma304389
 
C Language (All Concept)
C Language (All Concept)
sachindane
 
basics dart.pdf
basics dart.pdf
ssuser0ca68e
 
Unit 1: Primitive Types - Variables and Datatypes
Unit 1: Primitive Types - Variables and Datatypes
agautham211
 
Shell scripting
Shell scripting
Mufaddal Haidermota
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
Rasan Samarasinghe
 
c & c++ logic building concepts practice.pptx
c & c++ logic building concepts practice.pptx
rawatsatish0327
 
JAVA programming language made easy.pptx
JAVA programming language made easy.pptx
Sunila31
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
AbrehamKassa
 
PL SQL.pptx in computer language in database
PL SQL.pptx in computer language in database
ironman82715
 
Escape Sequences and Variables
Escape Sequences and Variables
yarkhosh
 
Chapter 2: Introduction to Bash Scripting
Chapter 2: Introduction to Bash Scripting
azzamhadeel89
 
JS-Slides-2_74526582652945_73562876535.ppt
JS-Slides-2_74526582652945_73562876535.ppt
goddemon7675
 
UNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
 
C Language (All Concept)
C Language (All Concept)
sachindane
 
Unit 1: Primitive Types - Variables and Datatypes
Unit 1: Primitive Types - Variables and Datatypes
agautham211
 
Ad

Recently uploaded (20)

Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
ieijjournal
 
Cadastral Maps
Cadastral Maps
Google
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
Low Power SI Class E Power Amplifier and Rf Switch for Health Care
ieijjournal
 
Cadastral Maps
Cadastral Maps
Google
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Ad

Shell scripting - Basic (PART -1)

  • 2. • Basics ………..…3 • First script ______________________________ ………….…4 • Variables and Comments ………….…5 • System defined Variables __________________ ………….…6 • User defined variables ____________________ ………….…7 • Use of Backslash character ________________ ………….…9 • Backtick Symbol _________________________ ………….…11 • Read User Input ………….…12 • Use of “-p” (Read as it is) __________________ ………….…13 • For multiple Variables ____________________ ………….…14 • Hide input _____________________________ ………….…15 2 PART-1
  • 3. Basics Q. What is shell script and how to check shells of your system ? Ans. A shell script is a text file that contains a sequence of commands for a Linux-based operating system. It's called a shell script because it combines into a "script" in a single file a sequence of commands that would otherwise have to be presented to the system from a keyboard one at a time. To check shell (operating system's command interpreter ) use following command “cat /etc/shells “ 3
  • 4. First Script… Let’s start our first script with “echo” command…….. Task : To print “my world” Step 1: vi test1.sh (“test1.sh” is my script name) Step 2: Write following commands in your file #! /bin/bash (For Interpreter -Specify shell to execute program) echo “my world” (Here, “my world” is desired o/p; save and exit this file) *Always give execution permission to your SCRIPTS • chmod +x test1.sh Output: Now, Run your script sh test1.sh ----------> 4
  • 5. Variables and comments • Comments : Use # to make anything commented. Generally it uses to explain about your commands or to give details about your script. • Variables : Which store some data in itself. Here, we have 2 types of variables. a) System defined b) User defined 5
  • 6. a. System defined Variables are created and maintained by your Linux system, they are pre-defined in your system. • Generally these variables are defined in CAPITAL LETTERS. • We can tap into these environment variables from within your scripts by using the environment variable’s name preceded by a dollar sign. This is demonstrated in the following script  6
  • 7. b. User defined Variables are case sensitive, so the variable Var1 is different from the variable var1. • Values are assigned to user variables using an equal sign. (There is no space b/w var and def. var) • Here are a few examples of assigning values to user variables: i. var1=10 ii. var2=-57 iii. var3=testing iv. var4=“still more testing” • The shell script automatically determines the data type used for the variable value. 7
  • 8. Example 1: (Just like system variables, user variables can be referenced using the dollar sign) Output : (Each time the variable is referenced, it produces the value currently assigned to it.) 8
  • 9. Example 2: (Look at what happens in this example) Output : • That is obviously not what was intended. Whenever the script sees a dollar sign within quotes, it assumes you’re referencing a variable. In this example the script attempted to display the variable $1 (which was not defined), and then the number 5. To display an actual dollar sign, you must precede it with a backslash character. 9
  • 10. • The backslash allowed the shell script to interpret the dollar sign as an actual dollar sign, and not a variable. Example 3: Output : 10
  • 11. Example 4: (Use of Backtick symbol (`) in shell variables) The shell runs the command within the backticks and assigns the output to the variable testing Output : Note : In bash you can also use the alternative $(…) syntax in place of backtick (`),which has the advantage of being re-entrant. 11
  • 12. Read User Input • read command is used for getting user input. Example 1: Output : (Here, cj is the user input) 12
  • 13. When we want input at same place  Use “-p” Example 2: Output : (Compare this with Example1) 13
  • 14. For Multiple variables/User’s inputs  Example 3 : Output : 14
  • 15. When we don’t want to show input  Use “-s” Example 4 : Here, I have used an extra echo which makes output like this. (w/o echo o/p ‘ll be like this) Output : (Here, Input for Password is not visible) 15
  • 16. PART-2 To be continued………. 16 • Array • Input - Output redirection • Pipes • Control structures (if) • Loops