Tutorial of shell script

Slides:



Advertisements
Similar presentations
In the last Session… ls -l command seven fields nine permissions of a file ls -ld file ownership file permissions (three-tiered file protection system)
Advertisements

CS 497C – Introduction to UNIX Lecture 8: The vi/vim Editor Chin-Chih Chang
Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Vi Editor TA for ITIS3100: Xu Fei
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Guide To UNIX Using Linux Third Edition
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide to Linux Installation and Administration, 2e1 Chapter 6 Using the Shell and Text Files.
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
CSCI6303 – Principles of I.T. Fall  Student will become familiar with scripting in shell using Linux/Ubuntu  Student will write a script and execute.
Welcome to CSE  Name: Di Cao   Classroom: DL357  Class Time: T 8:30am - 9:18am  Office.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
Introduction to Shell Script Programming
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Guide To UNIX Using Linux Fourth Edition
Chapter 6: Shell Programming
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 7 Editing.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
UNIX/LINUX SHELLS.  “A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and.
Writing Scripts Hadi Otrok COEN 346.
Isecur1ty training center Presented by : Eng. Mohammad Khreesha.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Chapter Six Introduction to Shell Script Programming.
Vi editor Pronounced: `vee eye‘’. Agenda Describe the background of vi Editor Use vi editor to: create text files edit text files Our Goal is to create.
Unix Advanced Shells Chapter 10. Unix Shells u Command Line Interpreter –once logged in, login gives control to a shell –it prompts for input, then parses,
Linux+ Guide to Linux Certification, Second Edition
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
1 Week 8 Creating Simple Shell Scripts. 2 Chapter Objectives  In this chapter, you will :  Learn how to create Shell Scripts  Commenting / Making Portable.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Learning Unix/Linux Based on slides from: Eric Bishop.
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
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.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
By Jonathan Rinfret UNIX/LINUX By Jonathan Rinfret
Tutorial of Unix Command & shell scriptS 5027
CIRC Winter Boot Camp 2017 Baowei Liu
Lecture 7 Introduction to Shell Programming
SUSE Linux Enterprise Desktop Administration
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Some Linux Commands.
The Linux Operating System
Shell Script Assignment 1.
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
The Linux Command Line Chapter 12
LING 408/508: Computational Techniques for Linguists
Tutorial of Unix Command & shell scriptS 5027
The Linux Command Line Chapter 24
Linux Shell Script Programming
Week 1 – Lesson 2: Creating Shell Scripts, Linux Commands
Linux Operations and Administration
Lab 7 Shell Script Reference:
The Linux Command Line Chapter 12
The Linux Command Line Chapter 24
Presentation transcript:

Tutorial of shell script Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar/Hector Cen Spring 2018

Outline What is a shell script? What are they good for? Different types of shell Recommended text editors How to write and execute shell scripts? Variables Operations (Arithmetic, comparison) Flow control Loops I/O References

What is a shell script? A shell script is file containing a series of commands. The shell reads this file and executes the commands sequentially as if they were inputted manually.

What are they good for? Shell scripts are useful for a good number purposes: Operating systems boot and initialization Automation of maintenance tasks Setup and deployment of applications In general, to automate processes/tasks

Different types of shells Shell scripts are interpreted by another programs These programs establish differences as to how write the scripts to be interpreted Most commonly known shells are: sh: One of the first ones. Developed for UNIX at AT&T bash: the default shell for many Linux distributions. It has many improvements over sh. csh and tcsh: short for C-shell. C language style and approach. Not as popular as bash.

Recommended text editors gedit nano vim Notepad++ (Windows) Sublime Text (cross-platform)

Recommended text editors – (1) Vim: short for Vi Improved. Is a popular text- editor in unix-like environments Open a file: $ vim test.sh Command mode: Press esc to switch to command mode Insert mode: Press i to switch to insert mode

Recommended text editors – (2) Basic vim commands: :w – Write file :wq – Write file and quit :w! – Force write :n – Go to line n :%s/old/new/g – searches for old and replaces with new within the entire file (g) There’s much, much more…

Recommended text editors – (3) Caveat for text editors in Windows There are two types of line endings: CR LF: used in Windows LF: Unix (includes macOS) If working in Windows (Notepad++ ), line ending should be set to LF to be compatible with Unix/Linux.

How to write and execute shell scripts – Permissions Shell scripts need execution permission to be able to run: $ chmod u+x test.sh Permission granted for the user No execution permission granted

How to write and execute shell scripts – Execute scripts After permissions are granted, scripts can be executed $ ./test.sh

How to write and execute shell scripts – Variables (1) In shell scripts, variables are untyped. Good for scripting if used carefully. Consequences arise if not used properly Can be local, environmental Variables defined within the context of a function are local. Environmental variables affect the behavior of the shell An environment can have multiple variables Examples: PATH, SHELL, HOSTNAME

How to write and execute shell scripts – Variables (2) In shell scripts, variables can be defined by users: Syntax: variable_name=value value is assigned to the variable called variable_name. Example: course_code=CIS5027 Interpreter

How to write and execute shell scripts – Variables (3) How to access the value of a variable: Use $ followed by the variable name

How to write and execute shell scripts – Variables (4) Output of a command can also be saved in a variable:

How to write and execute shell scripts – Operations (1) Several operations can be performed in shell scripts Arithmetic Addition, subtraction, multiplication, division, mod Comparison equal, not equal, greater than, less than, etc…

How to write and execute shell scripts – Operations (2) Several operations can be performed in shell scripts Arithmetic

How to write and execute shell scripts – Operations (2) Several operations can be performed in shell scripts Comparison

How to write and execute shell scripts – Flow control Scripts need to do more than just executing commands sequentially Scripts can make decisions and perform different actions depending on true or false conditions

How to write and execute shell scripts – Loops (1) Scripts can execute a sequence of commands repeatedly, until a given condition is satisfied while loop Condition

How to write and execute shell scripts – Loops (2) Scripts can execute a sequence of commands repeatedly, until a given condition is satisfied for loop Condition

How to write and execute shell scripts – I/O (1) Scripts can also read input from the user Typically from the keyboard

How to write and execute shell scripts – I/O (2) Scripts can also read input in the form of arguments Arguments are passed to an script as follows: $ ./test.sh 560 To access arguments within the script, you can use $1, $2,…,$n, where $1 is the first argument, $2 the second one, and so on…

How to write and execute shell scripts – I/O (3) Loading contents from text files Three files File as input

How to write and execute shell scripts – I/O (4) Output will be:

References http://www.freeos.com/guides/lsst/ http://linuxcommand.org/lc3_writing_shell_scripts.php