 Prepared by: Eng. Maryam Adel Abdel-Hady

Slides:



Advertisements
Similar presentations
An Introduction to Programming By :- Vishal Hirani B.Tech II year (CSE)
Advertisements

ARDUINO CLUB Session 1: C & An Introduction to Linux.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
Information Networking Security and Assurance Lab National Chung Cheng University 1 What Linux is? Free Unix Like Open Source Network operating system.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Linux & Shell Scripting Small Group Lecture 4 How to Learn to Code Workshop group/ Erin.
Lab 8 Shell Script Reference:
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Scripting CBIS BASH Scripting Step 1 – Create the bash file. Usually a good idea to end it in.sh file1.sh Step 2 – Using CHMOD make the bash file.
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
An Introduction to Unix Shell Scripting
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Unix Tutorial for FreeSurfer Users. Helpful To Know FreeSurfer Tutorial Wiki:
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
Unix Tutorial for FreeSurfer Users. Helpful To Know FreeSurfer Tutorial Wiki:
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
Writing Scripts Hadi Otrok COEN 346.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
PTA Linux Series Copyright Professional Training Academy, CSIS, University of Limerick, 2006 © Workshop III - Part A Shell Commands Professional Training.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Linux+ Guide to Linux Certification, Second Edition
Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
 Prepared by: Eng. Maryam Adel Abdel-Hady
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
CIRC Winter Boot Camp 2017 Baowei Liu
Department of Computer Engineering
SUSE Linux Enterprise Desktop Administration
Prepared by: Eng. Maryam Adel Abdel-Hady
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Scripting March 1st, 2004 Class Meeting 7.
By Jonathan Rinfret CREATING A BASH SCRIPT By Jonathan Rinfret
The Linux Operating System
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
The Linux Command Line Chapter 24
Introduction to TouchDevelop
Lab 7 Shell Script Reference:
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
The Linux Command Line Chapter 24
Presentation transcript:

 Prepared by: Eng. Maryam Adel Abdel-Hady

Shell Script

 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

 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

 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

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

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 "

 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”

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

Functions

 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 command commandN return } function_name;

IF Command

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

 # 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

 # 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

 # 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

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

 For more about shell script o o t=PL9C76917D6B09D8E3 t=PL9C76917D6B09D8E3 o