Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 7 Shell Script Reference:

Similar presentations


Presentation on theme: "Lab 7 Shell Script Reference:"— Presentation transcript:

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

2 Entering VI $ vi file-name
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

3 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) . Operation Mode Input mode press a or i Command mode press ESC a : insert after the current position(cursor position). i : insert before the current position(cursor position).

4 To change the current position(move the cursor):
Command mode Input 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. To change the current position(move the cursor): in the command mode use the arrow in the keyboard ↑↓ → ←

5 Vi Text Editor keys: 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

6 Vi Text Editor keys: Save and quit Ctrl+g Show file name ZZ or :wq
Then press ENTER :w Save only :q Quit (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

7 Vi Text Editor keys: Undo and Redo u Undo Ctrl+r Redo :!command
Run shell command :!command :sh To move to shell temporarely Then ctr+d to get back to VI For example: :!id :!ls :!ps

8 “Shell Script is series of command written in plain text file. “
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.

9 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 permission your-script-name chmod 755 test

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

11 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

12 Getting started with Shell Programming (cont.)
$ 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"

13 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' $ vech=Bus $ echo $vech $ n=10 $ echo $n

14 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. HOME vech $ no=10 $ no =10 wrong $ no= wrong $ no = 10 wrong $ no=10 $ No=11 $ vech= $ vech=""

15 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` $ 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

16 The Read Statement Use to get input (data from user) from keyboard and store (data) to variable Syntax: read variable1, variable2,...variableN Example: $ 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


Download ppt "Lab 7 Shell Script Reference:"

Similar presentations


Ads by Google