Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Shell scripts – part 1 Cs 302

2 Shell scripts  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.  System Administration part can be also automated.

3 Getting started with Shell Programming (cont.)  How to write shell script? 1) Use any editor like vi / mcedit / nano or notepad to write shell script. 2) After writing your script in txt format, execute it by one of the following ways: $ bash your-script-name.txt $ sh your-script-name.txt $./your-script-name.txt 1) Note: sometimes you need to convert your script/text file from DOS/MAC format to UNIX format using the following command: $ dos2unix./ your-script-name.txt

4 Getting started with Shell Programming (cont.)  Hello world program:  # # My first shell script # echo “Hello world !"

5 Echo command  echo [OPTION] [STRING]  The option -n means : do not output the trailing newline  The option -e means Enable interpretation of the following backslash escaped characters in the strings: \b backspace \n new line \t horizontal tab

6 Echo command (cont.)  "Double Quotes“  variables substitution  'Single quotes‘  protects everything enclosed between two single quote marks.  It is used to turn off the special meaning of all characters ( NO substitution of variables and commands).  Back quote`  Used with commands only.  To execute command.

7 commandvariable Double “ ” $echo “My working directory is pwd” The output: My working directory is pwd $echo “the home is $HOME” The output: the home is /home/nora Double “ “ Back ` ` $echo “My working directory is `pwd`” The output: My working directory is /home/nora/Desktop - Single ‘ ‘ Back ` ` $echo ‘My working directory is `pwd`’ The output: My working directory is `pwd` - Single ‘ ‘ $echo ‘My working directory is pwd’ The output: My working directory is pwd $echo ‘the home is $HOME’ The output: the home is $HOME

8 Comment in script  # followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script. Syntax: # comment-text

9 Variables in shell  In Linux, there are two types of variable: System variables (SV) (1) System variables (SV) - 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.  How to define and print User Defined Variables? Syntax to define UDV variable name=value (without spaces !) Syntax to print or access value of UDV/SV $variablename

10 Variables in shell (cont.)  Example: - To define variable called drink having value “tea”: drink=tea - To print it on screen : echo “$drink”

11 Class exercise (1)  Define variable called X and give it value 10.  Print it.

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

13 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`  will print 9 expr 6+3  will not work because no space between number and operator

14 Shell Arithmetic (cont.) x=20 y=5 expr $x / $y x=20 y=5 z=`expr $x / $y` echo $z 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

15 Class exercise (2)  Define two variable z=20, y=5 and then print the addition of them.

16 The read statement  Used to get input (data from user) from keyboard and store (data) to variable read variable1 variable2 variableN  Syntax: read variable1 variable2 variableN Example 1 : #Script to read your name from key-board # echo "Your first name please“: read fname echo "Hello $fname, Lets be friend" !

17 Class exercise (3)  Write a script that reads two numbers from key-board and calculate their sum


Download ppt "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."

Similar presentations


Ads by Google