Shell scripts – part 1 Cs 302. Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Using Linux Commands Lab 4 1. Create empty files To create an empty file $ touch apple banana grape grapefruit watermelon $ ls -l Using file-matching.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Information Networking Security and Assurance Lab National Chung Cheng University 1 What Linux is? Free Unix Like Open Source Network operating system.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
JavaScript, Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Using Linux Commands Lab 4. Using the Shell in Linux Commands Syntax  Options: could be added to the commands to change their behavior (-a, -la, --help)
Linux & Shell Scripting Small Group Lecture 4 How to Learn to Code Workshop group/ Erin.
Shell Script Examples.
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
An Introduction to Unix Shell Scripting
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Input, Output, and Processing
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Shell Advanced Features. Module 8 Shell Advanced Features ♦ Introduction In Linux systems, the shells are often referred to as command line interfaces.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Using Linux Commands Lab 4. Using the Shell in Linux Commands Syntax  Options: could be added to the commands to change their behavior (-a, -la, --help)
Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Linux+ Guide to Linux Certification, Second Edition
Configuration your environment Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT.
 Prepared by: Eng. Maryam Adel Abdel-Hady
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
Topics Designing a Program Input, Processing, and Output
CIRC Winter Boot Camp 2017 Baowei Liu
Prepared by: Eng. Maryam Adel Abdel-Hady
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Using Linux Commands Lab 3.
Introduction to C++ Programming
MSIS 655 Advanced Business Applications Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Lab 7 Shell Script Reference:
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

Shell scripts – part 1 Cs 302

Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell Script?  Shell script can take input from user, file and output them on screen.  Useful to create our own commands.  Save lots of time.  System Administration part can be also automated.

Getting started with Shell Programming (cont.)  How to write shell script? 1) Use any editor like vi / mcedit / nano or notepad to write shell script. 2) After writing your script in txt format, execute it by one of the following ways: $ bash your-script-name.txt $ sh your-script-name.txt $./your-script-name.txt 1) Note: sometimes you need to convert your script/text file from DOS/MAC format to UNIX format using the following command: $ dos2unix./ your-script-name.txt

Getting started with Shell Programming (cont.)  Hello world program:  # # My first shell script # echo “Hello world !"

Echo command  echo [OPTION] [STRING]  The option -n means : do not output the trailing newline  The option -e means Enable interpretation of the following backslash escaped characters in the strings: \b backspace \n new line \t horizontal tab

Echo command (cont.)  "Double Quotes“  variables substitution  'Single quotes‘  protects everything enclosed between two single quote marks.  It is used to turn off the special meaning of all characters ( NO substitution of variables and commands).  Back quote`  Used with commands only.  To execute command.

commandvariable Double “ ” $echo “My working directory is pwd” The output: My working directory is pwd $echo “the home is $HOME” The output: the home is /home/nora Double “ “ Back ` ` $echo “My working directory is `pwd`” The output: My working directory is /home/nora/Desktop - Single ‘ ‘ Back ` ` $echo ‘My working directory is `pwd`’ The output: My working directory is `pwd` - Single ‘ ‘ $echo ‘My working directory is pwd’ The output: My working directory is pwd $echo ‘the home is $HOME’ The output: the home is $HOME

Comment in script  # followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script. Syntax: # comment-text

Variables in shell  In Linux, there are two types of variable: System variables (SV) (1) System variables (SV) - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS. User defined variables (UDV) (2) User defined variables (UDV) - Created and maintained by user.  How to define and print User Defined Variables? Syntax to define UDV variable name=value (without spaces !) Syntax to print or access value of UDV/SV $variablename

Variables in shell (cont.)  Example: - To define variable called drink having value “tea”: drink=tea - To print it on screen : echo “$drink”

Class exercise (1)  Define variable called X and give it value 10.  Print it.

Rules for Naming variable name 1) begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character 2) Don't put spaces on either side of the equal sign no=10 no =10  wrong no= 10  wrong no = 10  wrong 1) Variables are case-sensitive, just like filename in Linux. 4)You can define NULL variable as follows: vech= vech="" 4)Do not use ?,* etc, to name your variable names.

Shell Arithmetic  Syntax : expr op1 math-operator op2 expr expr expr 10 / 2 expr 20 % 3 expr 10 \* 3 `` echo  will print 6+3 echo `expr 6 + 3`  will print 9 expr 6+3  will not work because no space between number and operator

Shell Arithmetic (cont.) x=20 y=5 expr $x / $y x=20 y=5 z=`expr $x / $y` echo $z define two variable x=20, y=5 and then to print division of x and y store division of x and y to variable called z

Class exercise (2)  Define two variable z=20, y=5 and then print the addition of them.

The read statement  Used to get input (data from user) from keyboard and store (data) to variable read variable1 variable2 variableN  Syntax: read variable1 variable2 variableN Example 1 : #Script to read your name from key-board # echo "Your first name please“: read fname echo "Hello $fname, Lets be friend" !

Class exercise (3)  Write a script that reads two numbers from key-board and calculate their sum