1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Scripts 4 A shell usually interprets a single line of input, but we can also create a file containing a number of lines of commands to be interpreted.
Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
CSCI 330 T HE UNIX S YSTEM C Shell Programming. S TEPS TO C REATE S HELL P ROGRAMS Specify shell to execute program Script must begin with #! (pronounced.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Chapter 6: Shell Programming
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
LINUX System : Lecture 6 Shell Programming
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths Corporate Information and Computing Services The University.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1. 2 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Control Statements and More
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
1 Writing Shell Scripts Professor Ching-Chi Hsu 1998 年 4 月.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Foreground and background processes
Shell Control Structures
Chapter 9 Shell Programming
LINUX System : Lecture 5 (English-Only Lecture)
UNIX Reference Sheets CSE 2031 Fall 2010.
Shell Programming.
Linux Shell Script Programming
Shell Control Structures
Shell Control Structures
Presentation transcript:

1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)

2 Shell logic structures The basic logic structures needed for program development are:  Sequential structure  Decision structures  Looping structures

3 Sequential structure Sequential structure states that commands will be executed in the order in which they appear in the program The only break in this sequence comes when a branch instruction changes the flow of execution

4 Decision structures The if statement is the primary decision-making control structure if (expression) simple-command or if (expression) then... [else...] endif

5 Decision structures, cont bash if test1; then commands1; [elif test2; then commands2;] [else commands3;] fi

6 Decision structures, cont. Example #!/bin/csh if ($#argv != 2) then echo $0 needs two parameters! echo You are inputting $#argv parameters. else set par1 = $argv[1] set par2 = $argv[2] endif The set built-in command can be used to display all variables set in the shell

7 Decision structures, cont. Another example: #! /bin/csh # number is positive, zero or negative echo "enter a number:" set number = $< if ( $number < 0 ) then echo "negative" else if ( $number == 0 ) then echo " zero " else echo " positive " endif

8 Decision structures, cont. The switch logic structure simplifies the selection of a match when you have a list of choices It allows your program to perform one of many actions, depending upon the value of a variable

9 Decision structures, cont. switch ( var ) case string1: command_set_1 breaksw case string2: command_set_2 breaksw default command_set_3 endsw

10 Decision structures, cont. #!/bin/csh if ($#argv == 0 ) then echo "No arguments supplied...exiting" else switch ($argv[1]) case [yY]: echo " Argument one is yes. " breaksw case [nN]: echo " Argument one is no. " breaksw default: echo " Argument one is neither yes nor no. " breaksw endsw endif

11 Decision structures, cont. bash case word in pattern {|pattern}*) commands;; … esac

12 Decision structures, cont. Example case $reply in “1”) date;; “2”|”3”) pwd;; *) echo illegal choice ;; esac

13 Looping structures while ( expr ) command_set end foreach var ( worddlist ) command_set end

14 Looping structures, cont. bash for name in word {word}* do commands done while test do commands done until test do commands done

15 Looping structures, cont. Example: #!/bin/csh foreach person (Omar Khaled Ali Faycal) echo Hello $person end Output: Hello Omar Hello Khaled Hello Ali Hello Faycal

16 Looping structures, cont. Other example: Adding integers from 1 to 10 #!/bin/csh set i=1 set sum=0 while ($i <= 10) echo Adding $i into the sum. set sum=`expr $sum + $i` set i=`expr $i + 1` end echo The sum is $sum.

17 Examples 1. #! /bin/csh -f set num_args = ${#argv} echo "The number of arguments in argv is: $num_args” set n = 0 while ( $n <= $num_args ) echo "Argument $n is $argv[$n]" set n = ($n + 1) end 2. if (-f ln.exe && -f raz.exe) echo "yes" if (-f ln.exe || -f raz.exe) echo "no"

18 Examples, cont. 3. #!/bin/sh # make a directory mkdir /home/omar/mydocs # copy all doc files cp *.docs /home/omar/mydocs # delete all docs files rm *.docs

19 Examples, cont. 4. #!/bin/sh if grep ‘omar\>’ data.file > /dev/null 2>&1 then echo “omar is in file” else echo “No, omar is not in file” fi /dev/null: the system dustbin

20 Special shell variables $#: the number of arguments passed to the script $*: shows in a single string all the arguments passed to the script $$: the current PID of the script running $!: the PID of the last process that was put in the background the same as $#, but when used with quotes, returns each argument in quotes $?: shows the exit status of the last command. 0 is no errors, any other value is an error.

21 Special shell variables, cont. echo “this is the script name: $0” echo “this is the first parameter: $1” echo “this is the second parameter: $2” echo “this is the third parameter: $3” echo “this is the fourth parameter: $4” echo “the number of arguments passed: $#” echo “show all arguments: $*” echo “show me my process ID: $$” echo “show me the arguments in quotes: echo “did my script go with any errors: $?”

22 Examples, cont. 5. #!/bin/sh #is the EDITOR set? if [-z $EDITOR]; then echo “Your editor environment is not set” echo “I will assume you want to use vi … OK” echo –n “Do you wish to change it now? [y..n]:” read ANS # check for an upper or lower case ‘y’ if [“$ANS”=“y”] || [“$ANS”=“Y”]; then echo “enter your editor type:” read EDITOR if [-z $EDITOR] || [“$EDITOR”=“”]; then echo “No, edito entered, using vi as default” EDITOR=vi export EDITOR fi # got a value use it for EDITOR EDITOR=$EDITOR export EDITOR echo “setting $EDITOR” fi else #user echo “Using vi as the default editor” EDITOR=vi export EDITOR fi

23 Examples, cont. 6. #!/bin/sh BAK=“.bak” for loop in ‘ls ‘ do echo “copying $loop to $loop$BAK” cp $loop $loop$BAK Done 7. #!/bin/sh for files in ‘ls LPSO*’ do cat $files | tr “[a-z]” “[A-Z]” >$files.UC done