160 likes | 294 Views
Lab 7. Shell Script. Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook http://www.freeos.com/guides/lsst/index.html. Entering VI. To call the VI editor and begin an editing session File-name does not exist anew file will be created
E N D
Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3A Beginner's handbook http://www.freeos.com/guides/lsst/index.html
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
Input modepressaori Operation Mode Command modepressESC Organization of VI There are two modes of operation in VI: • Command mode: where you tell the editor what you want it to do. • Text input mode: the part of the editor where you inter material (text, data, or program code) . 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 ↑↓ → ←
Vi Text Editor keys:Delete • x delete current character • X delete previous character(backspace) • dwdelete word forward • dddelete complete line • d0 delete from cursor to beginning of line • D$delete to the end of the line
Vi Text Editor keys: Ctrl+gShow file name Save and quit
Vi Text Editor keys: Undo and Redo • u Undo • Ctrl+rRedo • Run shell command For example: :!id :!ls :!ps
Shell script • 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. • 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 • Use any editor like vi or mcedit to write shell script. • After writing shell script set execute permission for your script syntax: chmod permissionyour-script-name
Getting started with Shell Programming (cont.) • Execute your script assyntax: bash your-script-namesh your-script-name./your-script-name
Getting started with Shell Programming (cont.) After saving the above script, you can run the script as follows:
Variables in Shell • In Linux, there are two types of variable: (1) System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS. (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: Syntax to define UDV variable name = value Syntax to print or access value of UDV $variablename Example: - To define variable called 'vech' having value Bus and print contains of variable 'vech' - To define variable called n having value 10 and print contains of variable ‘n'
Rules for Naming variable name • begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character • Don't put spaces on either side of the equal sign • Variables are case-sensitive, just like filename in Linux. • You can define NULL variable as follows: • Do not use ?,* etc, to name your variable names.
Shell Arithmetic Syntax:expr op1 math-operator op2 • 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
The Read Statement Use to get input (data from user) from keyboard and store (data) to variable Syntax: read variable1, variable2,...variableN Example: Run it as follows: