Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook"— Presentation transcript:

1 Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook http://www.freeos.com/guides/lsst/index.html

2 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.

3 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

4 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

5 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

6 $ 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.)

7 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

8 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=""

9 Shell Arithmetic Syntax : expr op1 math-operator op2 expr 1 + 3 expr 2 - 1 expr 10 / 2 expr 20 % 3 expr 10 \* 3 `` $ echo 6 + 3  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

10 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 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: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook"

Similar presentations


Ads by Google