Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Prepared by: Eng. Maryam Adel Abdel-Hady

Similar presentations


Presentation on theme: " Prepared by: Eng. Maryam Adel Abdel-Hady"— Presentation transcript:

1  Prepared by: Eng. Maryam Adel Abdel-Hady eng_maryamadel@yahoo.com

2 Shell Script

3  With the thousands of commands available for the command line user, how can you remember them all? The answer is, you don't. The real power of the computer is its ability to do the work for you. To get it to do that, we use the power of the shell to automate things. We write scripts  Scripts are collections of commands that are stored in a file. The shell can read this file and act on the commands as if they were typed at the keyboard.  A shell script is a file that contains ASCII text. To create a shell script, you use a text editor. A text editor is a program, like a word processor, that reads and writes ASCII text files. o Text editor such as gedit, kwrite and emacs

4  To successfully write a shell script, you have to do three things: o Write a script o Give the shell permission to execute it o Put it somewhere the shell can find it 1. Write a script: o Open your text editor for example: “gedit” by writing: $ gedit o Write in the file : #!/bin/bash # My first script echo "Hello World!“ o Save the file 2. Give the shell permission to execute it: o The next thing we have to do is give the shell permission to execute your script. This is done with the ”chmod” command as follows: $ chmod +x my_script 1. Run your script file: $./my_script

5  In Linux (Shell), there are two types of variable: 1. System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS. Ex. “ BASH for shell name, HOME for our home directory, SHELL for our shell name” You can write $ echo $SHELL  /bin/bash 2. User defined variables (UDV) - Created and maintained by user. This type of variable defined in lower letters. How to create a variable? variable name=value To create a variable, put a line in your script that contains the name of the variable followed immediately by an equal sign ("="). No spaces are allowed. After the equal sign, assign the information you wish to store. Note that no spaces are allowed on either side of the equal sign. Ex: n=4  To define variable called n having value 4

6  Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character. For e.g. Valid shell variable are as follows HOME SYSTEM_VERSION vech no  Don't put spaces on either side of the equal sign when assigning value to variable. For e.g. In following variable declaration there will be no error no=10 But there will be problem for any of the following variable declaration: no =10  Variables are case-sensitive, just like filename in Linux.  You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition) For e.g. $ vech= $ vech="" o Try to print it's value by issuing following command echo $vech  Do not use ?,* etc, to name your variable names.

7 Use to get input (data from user) from keyboard and store (data) to variable.  Syntax: read variable1, variable2,...variableN ----------------- Ex: echo "Your first name please:" read fname echo "Hello $fname "

8  Program that read two numbers and print the summation and the average of them  Note: o If you need to edit the script file in the terminal screen You can write nano my_script  where my_script is the script file To save your editing click  “ctrl + x “  then “ y”  then “enter”

9  Program that read your name, your age, your Address and print “ your name is ……., your age is …… and your address is ……..”

10 Functions

11  Function is series of instruction/commands. Function performs particular activity in shell.  Why to write function? o l Saves lot of time. o l Avoids rewriting of same code again and again o l Program is easier to write. o l Program maintains is very easy -------------------------- function-name ( ) { command1 command2........ commandN return } function_name;

12

13 IF Command

14 #!/bin/bash number=1 if test $number -eq 1 then echo "Number equals 1" else echo "Number does not equal 1“ fi

15

16  # Script to see whether argument is positive o #!/bin/bash o echo “ please enter number” o read n o if test $n -gt 0 o then o echo "$n number is positive" o fi

17  # Script to see whether argument is positive or negative o #!/bin/bash o echo “ please enter number” o read n o if test $n -gt 0 o then o echo "$n number is positive" o else o echo "$n number is negative" o fi

18  # Script to choose from list and print a statement according to you choice o ch=0 o echo "1. Unix (Sun Os)" o echo "2. Linux (Red Hat)" o echo -n "Select your os choice [1 or 2]? " o read ch o if test $ch -eq 1 o then o echo "You Pick up Unix (Sun Os)" o else if test $ch -eq 2 o then o echo "You Pick up Linux (Red Hat)“ o else o echo "What you don't like Unix/Linux OS." o fi

19  Write a function that accept 2 numbers and prints the largest, the summation and the average of them.

20  For more about shell script o http://linuxcommand.org/lc3_learning_the_shell.php http://linuxcommand.org/lc3_learning_the_shell.php o http://www.youtube.com/watch?v=QGvvJO5UIs4&feature=share&lis t=PL9C76917D6B09D8E3 http://www.youtube.com/watch?v=QGvvJO5UIs4&feature=share&lis t=PL9C76917D6B09D8E3 o http://www.mediafire.com/view/?qhuqxxxm7puejm5 http://www.mediafire.com/view/?qhuqxxxm7puejm5


Download ppt " Prepared by: Eng. Maryam Adel Abdel-Hady"

Similar presentations


Ads by Google