Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,

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.
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
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
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
Linux+ Guide to Linux Certification, Second Edition
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Introduction to Unix (CA263) Decisions, Decisions By Tariq Ibn Aziz Dammam Community College.
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.
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 Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Introduction to UNIX / Linux - 11
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
Linux+ Guide to Linux Certification, Third Edition
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
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.
Shell Programming. An example u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg file2 –encrypts file1 into file2 u record this command with.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
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.
©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 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Compunet Corporation Introduction to Unix (CA263) Your Environment By Tariq Ibn Aziz Dammam Community College.
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
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.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Linux+ Guide to Linux Certification, Second Edition
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
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.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Pepper (Help from Dr. Robert Siegfried)
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Linux Shell Script Programming
Essential Shell Programming
Introduction to Bash Programming, part 3
Presentation transcript:

Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File, Environment variables Arithmetic, String, File, Environment variables Loop statements (for, while) Loop statements (for, while) Lists (AND, OR) Lists (AND, OR) Functions Functions “Small” differences among shells “Small” differences among shells Bash has line man page Bash has line man page

Variables variable format: variable format: variable = value (there should not be any spaces around the =) Example: Example:$number=7 $ echo The value of number is $number: The value of number is 7 $

Using Quotes Four types of quote characters Four types of quote characters ‘single quote character “double quote character \backslash character `back quote character Example (single quote): Example (single quote): $ cat city AustinDallas Ft. Worth San Antonio $ grep ‘San Antonio’ city San Antonio $

Using Quotes Example (double quote): Example (double quote): $ex1= “‘Dallas,’ A city in Texas” $echo $ex1 ‘Dallas,’ A city in Texas $ex2=‘ “San Antonio,” is also a city in Texas’ $echo $ex2 “San Antonio,” is also a city in Texas $

Using Quotes - (cont) Example (backslash): Example (backslash): $echo $ex2 “San Antonio,” is also a city in Texas $ $ echo \$ex2 $ex2$ which is the same as: $echo ‘$’ ex2 $ex2$

Using Quotes Example (back quote) Example (back quote) $ echo The date and time is: `date` The date and time is: Tue Oct 10 10:18:34 EDT 2006 $

Making Decisions - If if format: if format: if expression then commands elif commands else commands fi

Making Decisions - Test (cont) Relational operators Relational operators -eq -eq -ne -ne -lt -lt -gt -gt -le -le -ge -ge

Making Decisions - Test (cont) Test file operators returns true if: Test file operators returns true if: -d filefile is a directory -f filefile is an ordinary file, and exists -r filefile is readable by the process -s filefile isn’t empty -w file file is writable by the process -x filefile is executable -G filefile is owned by the group I belong to -O filefile is owned by user -u fileset_user_id bit is set -g fileset_group_id bit is set -

Making Decisions Test Examples Examples [ -f /usr/train1/file1 ] if file1 exists and is ordinary file [ -r /usr/train2/file1 ] if file1 exists and is readable by this process [ -s /usr/train3/file1 ] if file1 exists and is not empty

Making Decisions Test Test logical operators Test logical operators ! expressionlogical negation expression -a expressionlogical and expression -o expressionlogical or Examples: Examples: [ ! -f /usr/train1/file1 ] [ -f /usr/train1/file1 -a -r /usr/train1/file1 ] [ -n “$var1” -o -r /usr/train2/file1 ]

Looping Constructs For For format: For format: for variable in item1 item2..item3 docommand(s)done

User Environment During logon a distinct environment is created separate from other users During logon a distinct environment is created separate from other users

Local Variables Value cannot be changed by other subshells Value cannot be changed by other subshells Will not be passed to other subshells Will not be passed to other subshells

Local Variables - (cont) Example: Example: > cat test1 echo %$var1% $test1 % % % % var1 is equal to null > cat test2 var2=25 echo %$var2% >>test2 %25 % var2 is equal to 25

Exported Variables Export command format: Export command format: export vars Allows a value of a variable to be passed to other subshells Allows a value of a variable to be passed to other subshells Changing an exported variable in a subshell does not change the value in the parent shell Changing an exported variable in a subshell does not change the value in the parent shell

Exported Variables Example: Example: > cat test3 var3 = 2345 echo var3 = $var3 > test 3 var3 = 2345 $cat test4 echo var3 = $var3 > test4 var3 = var3 is a local variable so its value in test3 is In test4 it is null.

Exported Variables (cont) Another Example: Another Example: $ var3=2345 $ export var3 $cat test3 echo var3 = $var3 $test3 var3 = 2345 $test4 $ var3 is an exported variable so its value in test3 is In test4 it is also 2345

Exported Variables - (cont) Once an exported variable always an exported variable. (Unless you use unset to destroy it.) Once an exported variable always an exported variable. (Unless you use unset to destroy it.) Each subshell makes its own copy of the variable Each subshell makes its own copy of the variable Export with no arguments lists the variables that are exported to users shell Export with no arguments lists the variables that are exported to users shell

env Command env [option] [var = val] [command] env [option] [var = val] [command] displays current environment or modifies variables specified. displays current environment or modifies variables specified. Commands specified are executed in the new environment Commands specified are executed in the new environment

Subshells A new shell to execute a program A new shell to execute a program Has its own environment local vars Has its own environment local vars Can’t change variable in parent shell Can’t change variable in parent shell Subshell variables destroyed on exit Subshell variables destroyed on exit

PATH Directories search for program Directories search for program Setup during logon procedure Setup during logon procedure Directories are separated by colon (:) Directories are separated by colon (:) Message “not found” is returned upon unsuccessful search Message “not found” is returned upon unsuccessful search

.profile File provided by “Systems Administrator” provided by “Systems Administrator” Executed from the users home directory Executed from the users home directory Can use to customize user environment Can use to customize user environment Example: Example: > cat $HOME/.profile PATH=“/bin:/usr/bin::” export PATH >