CIS 240 Introduction to UNIX Instructor: Sue Sampson.

Slides:



Advertisements
Similar presentations
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Advertisements

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.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
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.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
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.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
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.
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
LINUX System : Lecture 6 Shell Programming
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
CS465 - UNIX The Bourne Shell.
#!/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.
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.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
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.
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 Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
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)
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
LINUX System : Lecture 5 (English-Only Lecture)
Agenda Control Flow Statements Purpose test statement
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Shell Programming.
Linux Shell Script Programming
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

CIS 240 Introduction to UNIX Instructor: Sue Sampson

CIS240 – Advanced Shell Programming Basic Rules for Writing Expressions You must have at least one space before and one space after parens used to group expressions. You must have at least one space before and one space after brackets that substitute for the test command Some shells require you to enclose variable names in quotes when used in a test command You must have no spaces between variable/value in an assignment statement.

CIS240 – Advanced Shell Programming File Testing -d True if is a directory -f True if is a file -r True if is readable -s True if length != 0 -w True if is writable -x True if is executable

CIS240 – Advanced Shell Programming Test for Integers -eq True if equals -ge True if is greater than or equal to -gt True if is greater than -le True if is less than or equal to -lt True if is less than -ne True if is not equal to

CIS240 – Advanced Shell Programming Test for Strings True if is not empty = True if and are the same != True if and are not the same -n True if the length of is greater than zero -z True if the length of is zero

CIS240 – Advanced Shell Programming Comments start with a pound sign (#). Shell headers are comments at the beginning of a shell script that provide information to users and programmers. Adding a header and comments makes your shell script easier for others to use. The #!/bin/sh you see at the beginning of script examples in your book is more than a comment.  File must be located in an executable directory; echo $PATH  The current shell reads that line and uses the shell specified in the path to run the script  Script must be executable, chmod

CIS240 – Advanced Shell Programming Path Change There are two methods to add directories to the $PATH variable:  Type PATH=$PATH:  Add the path to your PATH statement in.bash_profile in your home directory. The PATH statement is about midway down in the file. Example: You have a script in a directory called /home/Sampson. You would enter the following command: PATH=$PATH:/home/Sampson Note: in RH9, your path statement includes a home/ /bin directory, but no directory exists… create the directory and run the scripts from there.

CIS240 – Advanced Shell Programming # Comment line… typically used to indicate shell #!/bin/sh if test [ -f “$1” ] then filename=“$1” set `ls –il $filename` inode=“$1” size=“$6” echo –e “Name\tInode\tSize” echo echo –e “$filename\t$inode\t$size” exit 0 fi

CIS240 – Advanced Shell Programming Grave Accents Backwards single quote (above the tab key on your keyboard) Used for command substitution Example: echo “The date and time is `date`.” The date and time is Tue Jan 27 09:48:45 CST 2004

CIS240 – Advanced Shell Programming expr Converts numbers in an expression from strings to integers. By default every variable is stored as a string. Evaluates the expression arguments, ‘args’, and sends the results to the standard output.

CIS240 – Advanced Shell Programming Integer Operators Comparison: = True if equals \>= True if is greater than or equal to \> True if is greater than \ True if is less than or equal to \ True if is less than != True if is not equal to \| Return first if not null, else return second \& Return first if neither null, else return 0 Arithmetic: +, -, \*, /, %Add, subtract, multiply, divide, remainder

CIS240 – Advanced Shell Programming Format: expr ` ` Example: #var1=10 #var1=`expr $var1 + 1` #echo $var1 # 11

CIS240 – Advanced Shell Programming Command Line Parameters $0 the name of the script being executed $1,$2,$3 input parameters $# the number of parameters $* all the parameters in a single string $$ the process identifier $? the value returned by the last command executed $! The process identifier of the last background process invoked

CIS240 – Advanced Shell Programming clarg1.sh if [ $1 ] then echo “The value of the first argument is $1” else echo “There is no argument 1” fi $ sh clarg1.sh $ There is no argument 1

CIS240 – Advanced Shell Programming clarg2.shx=0 for i in $1 $2 $3 $4 for ido x=`expr $x + $i` doneecho “The sum is $x”$ sh clarg2.sh $ The sum is 15 $The sum is 15

CIS240 – Advanced Shell Programming C Shell Variables Follow many of the same conventions as Bourne Shell names First character is upper or lower case a-z Next characters are upper or lower case letters, numbers, or underscores The maximum length depends on your version of UNIX

CIS240 – Advanced Shell Programming C Shell Environment Variables Are reserved words Use lower case letters Some are named differently than those in Bourne Shell Bourne ShellC Shell HOMEhome PATHpath PS1prompt PS2prompt2

CIS240 – Advanced Shell Programming Command Line Parameters C Shell recognizes the same convention as Bourne shell ($0, $1, $2 …) C Shell also recognizes the C programming language convention for command line input (argv) if ( $argv[1] ) then echo “The value of the first argument is $argv[1]” else echo “There is no argument 1” endif

CIS240 – Advanced Shell Programming Control Structures ifDecision making statement foreachCounting loop whileDecision loop switchSelection statement

CIS240 – Advanced Shell Programming Decision (if) Format: if ( ) then else endif if ( -f a_test_file ) then echo “file exists” else echo “file does not exist” endif

CIS240 – Advanced Shell Programming Counting Loop (foreach) Format: foreach end foreach students ( Phachoen Alfredo Timothy Momodou ) echo “$students” end

CIS240 – Advanced Shell Programming Selection (Switch) Format: switch ( ) case pattern1: breaksw case pattern2: breaksw default breaksw endsw

CIS240 – Advanced Shell Programming switch (“$guessword”) case “fred”: echo “guessword is fred” breaksw case “bill” | “bert”: echo “guessword is either bill or bert” breaksw default: echo “guessword was not matched” breaksw endsw

CIS240 – Advanced Shell Programming Running C Shell Scripts Create the shell script file Enter #!/bin/csh as the first line in the file Make the file executable Enter the entire path of the file as a command /home/sue/bin/test