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.

Slides:



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

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
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.
Lab 8 Shell Script Reference:
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
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.
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
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.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
LING 408/508: Programming for Linguists Lecture 5 September 9 th.
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.
Group, group, group One after the other: cmd1 ; cmd2 One or both: cmd1 && cmd2 Only one of them: cmd1 || cmd2 Cuddling (there):( cmd1 ; cmd2 ) Cuddling.
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]
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
CIRC Winter Boot Camp 2017 Baowei Liu
Shell Control Structures
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Scripting March 1st, 2004 Class Meeting 7.
CIRC Summer School 2017 Baowei Liu
Lecture 9 Shell Programming – Command substitution
Presented by, Mr. Satish Pise
Shell Script Assignment 1.
Agenda Control Flow Statements Purpose test statement
LING 408/508: Computational Techniques for Linguists
UNIX Reference Sheets CSE 2031 Fall 2010.
Essential Shell Programming
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
Shell Control Structures
Essential Shell Programming
CST8177 Scripting 2: What?.
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

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 a simple shell script file: $ vi test.sh or $ cat > test.sh To execute a shell script file: $ sh filename Or $./filename

Basic Operators Arithematic Operators : [+, -, \*, /, %, =,!=,==.] Relational Operators : [-eq,-ne,-gt,-lt,-ge,-le] Boolean Operators : [-o,-a,!] String Operators : [ -z, -n, =, !=, str ] File Test operators : [ -b,-c,-d,-e,-f,-r,-w,-x, -s]

Shell Decision Statement If..else statements if [ condition(s) ] then command(s) fi case...esac Statement case word in pattern1) Statement(s) to be executed if pattern1 matches ;; esac

Iterative/Looping Statements While Loop while [ condition ] do Command (s) done Until Loop until [ condition ] do command (s) done For Loop for var in word1 word2... wordN do Command(s) done

String Handling and Command line arguments String handling with test command  test str  test –n str  test –z str String handling with expr command  Length of the string : n=`expr "$str" : ‘.*’` ;  Extracting a sub-string: ` expr “$str” : ‘……\(..\)’` Command line arguments The command-line arguments $1, $2, $3...$9 are also called positional parameters $0 points to actual command/shell script/program Eg: echo “Program name is $0” echo “First argument is $1” echo “Second argument is $2” While executing $ sh test.sh arg1 arg2 Output: Program name is test First argument is arg1 Second argument is arg2