Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

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)
Shell Script Assignment 1.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
A Guide to Unix Using Linux Fourth Edition
Editing with vi Or more fun than you thought you’d have without a mouse Prof. Chris GauthierDickey.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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
Chapter 5 Editing Text Files
Information Networking Security and Assurance Lab National Chung Cheng University 1 What Linux is? Free Unix Like Open Source Network operating system.
CS 202 Computer Science II Lab Fall 2009 September 3.
1 Using Editors Editors let you create and edit ASCII files UNIX normally includes two editors: vi and Emacs Vi and Emacs are screen editors: they display.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/LINUX and Shell programming Page:1 Lesson 3: Vi- editor By Simi By Simi.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
1 Unix Editors (ee, ed, ex, vi, vim) and Compilers (g77, gcc) Speaker: Li-Wen Chen Date:
Chapter 3 Mastering Editors
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
The UNIX development environment CS 400/600 – Data Structures.
Agenda Using vi Editor Starting vi Session Command / Input Modes Entering Text Editing Text Saving vi Session Aborting Editing Session.
Introduction to Vim Robbie CSCI2100 Data Structures Tutorial 3.
VIM  This is the text editor you will use on the workstation.  You can also edit the text files under windows environment and upload it to the workstation.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Editing, vi and Configuration Files Introduction to Linux June 16, 2009 Papeete, French Polynesia Hervey Allen.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Text editors Why should I use an editor ? It is very important to able to use at least one text mode editor a text mode editor is so useful on remote machines.
Unix Session IV.
VI EDITOR University of Mississippi. Vi Editor What is Vi ? ▫Vi is a screen based editor. ▫The screen of your terminal will act as a window into the file.
1 © 2012 John Urrutia. All rights reserved. Chapter 6 The vi Editor.
Getting Started with Linux Linux System Administration Editors.
Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz.
Lesson 4-Mastering the Visual Editor. Overview Introducing the visual editor. Working in an existing file with vi. Understanding the visual editor. Navigating.
Week 8: Linux / ubuntu Dr. I. H. Shah. / CSCS 301 Fall 2009.
Chapter Three The UNIX Editors.
Writing Scripts Hadi Otrok COEN 346.
Linux Class #03. File Access Permissions Types of users in Linux: Local User (u) Group User (g) Other User (o) Each User can have 3 types of permissions:
Isecur1ty training center Presented by : Eng. Mohammad Khreesha.
Introduction to Programming Using C An Introduction to Operating Systems.
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
PacNOG 6: Nadi, Fiji Editing, vi & Configuration Files Hervey Allen Network Startup Resource Center.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Using vi Editor Starting vi Session Command / Input Modes Entering Text Editing Text Saving Edited File Aborting Editing Session.
Agenda Using vi Editor Starting vi Session Command / Input Modes
Editing, vi and Configuration Files Unix/IP Preparation Course July 19, 2009 Eugene, Oregon, USA
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.
PTA Linux Series Copyright Professional Training Academy, CSIS, University of Limerick, 2006 © Workshop III - Part A Shell Commands Professional Training.
Editing, vi and Configuration Files Unix/Linux Preparation Course June 27, 2010 Pago Pago, American Samoa
Getting Started with Linux Linux System Administration Editors.
 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
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.
CS:414 introduction to Unix and Linux
Department of Computer Engineering
SUSE Linux Enterprise Desktop Administration
Prepared by: Eng. Maryam Adel Abdel-Hady
Guide To UNIX Using Linux Third Edition
Shell Script Assignment 1.
Basic operations in Matlab
Vi Editor.
Vim.
IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
Tutorial of shell script
Chapter 2 Basic vi Editor.
Lab 7 Shell Script Reference:
Presentation transcript:

Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook

Entering VI  To call the VI editor and begin an editing session  File-name does not exist  anew file will be created  File-name exists  edit the existing file $ vi file-name

Organization of VI There are two modes of operation in VI:  Command mode  Command mode: where you tell the editor what you want it to do.  Text input mode  Text input mode: the part of the editor where you inter material (text, data, or program code). Operation Mode Input modepress a or i Command modepress ESC a : insert after the current position(cursor position). i : insert before the current position(cursor position).

To change the current position(move the cursor): in the command mode use the arrow in the keyboard ↑↓ → ← Command modeInput mode You can do the following: -Change current position(move the cursor). -Delete. -Show file name. -Save and quit. -Undo & Redo. -Run shell command. Enter(type or write): - text, data, code.

Vi Text Editor keys: self study ) ) Delete x delete current character X delete previous character(backspace) dw delete word forward dd delete complete line d0 delete from cursor to beginning of line D$ delete to the end of the line

Vi Text Editor keys: Ctrl+g Show file name Save and quit ZZ or :wqSave and quit Then press ENTER :wSave only :qQuit (nothing added to save) used when there are NO unsaved changes. If you need to save, vi warns you: E37: no write since last change(add ! To override) :q!Quit and don’t save

Vi Text Editor keys: Undo and Redo u Undo Ctrl+r Redo :!command  Run shell command For example: :!id :!ls :!ps

Shell script What is Shell Script What is Shell Script “Shell Script is series of command written in plain text file. “ Why to Write Shell Script? 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.  To automate some task of day today life.  System Administration part can be also automated.

Getting started with Shell Programming How to write shell script How to write shell script 1) Use any editor like vi or mcedit to write shell script. 2) After writing shell script set execute permission for your script syntax: chmod permission your-script-name chmod 755 test

Getting started with Shell Programming (cont.) 3) Execute your script as syntax: bash your-script-name sh your-script-name./your-script-name $ bash test $ sh test $./ test

Getting started with Shell Programming (cont.) $ vi first # # My first shell script # clear echo "Knowledge is Power" After saving the above script, you can run the script as follows: $ chmod 755 first $./first

$ vi first Start vi editor # # My first shell script # # followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script. Syntax: # comment-text clear clear the screen echo "Knowledge is Power" To print message or value of variables on screen, we use echo command, general form of echo command is as follows syntax: echo "Message" Getting started with Shell Programming (cont.)

Variables in Shell In Linux, there are two types of variable: In Linux, there are two types of variable: System variables (1) System variables - 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. This type of variable defined in lower letters. How to define and print User Defined Variables: How to define and print User Defined Variables: Syntax to define UDV variable name = value Syntax to print or access value of UDV $variablename Example: define'vech'Bus print - To define variable called 'vech' having value Bus and print contains of variable 'vech' definen10print - To define variable called n having value 10 and print contains of variable ‘n' vech=Bus echo $vech n=10 echo $n

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 3) Variables are case-sensitive, just like filename in Linux. 4)You can define NULL variable as follows: 5)Do not use ?,* etc, to name your variable names. HOME vech no=10 no =10  wrong no= 10  wrong no = 10  wrong no=10 No=11 vech= vech=""

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 define two variable x=20, y=5 and then to print division of x and y x=20 y=5 expr $x / $y store division of x and y to variable called z x=20 y=5 z=`expr $x / $y` echo $z

The Read Statement Use to get input (data from user) from keyboard and store (data) to variable read variable1 variable2 variableN Syntax: read variable1 variable2 variableN Example1: $ vi sayH # #Script to read your name from key-board # echo "Your first name please“: read fname echo "Hello $fname, Lets be friend " ! Run it as follows: $ chmod 755 sayH $./sayH

$ vi summation # #Script to read two numbers from key-board and calculate their sum # echo “Enter two numbers“: read x y echo “The sum $x and $y is `expr $x + $y`” $ chmod 755 summation $./summation Run it as follows: Example2: