Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

2006-Jan-231 Shell Scripts Jacob Morzinski
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
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
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.
Lecture 2: More UNIX, Bash Shell Scripting Programming Tools And Environments.
Shell Script Examples.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
An Introduction to Unix Shell Scripting
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 8 Shell.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
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.
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.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
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.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
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.
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.
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 Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Lecture 2: Advanced UNIX, shell scripts Programming Tools And Environments.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
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.
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]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
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.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
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.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Lecture 9 Shell Programming – Command substitution
CSE 303 Concepts and Tools for Software Development
Pepper (Help from Dr. Robert Siegfried)
John Carelli, Instructor Kutztown University
LING 408/508: Computational Techniques for Linguists
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Presentation transcript:

Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments

Objective Objective: To introduce students to the concept of a shell, effective use of the shell, the shell as programming language, and shell scripts. We will use the bash shell for the examples and details provided. Shell syntax and commands history and command completion job control variables programming constructs scripts

What does the shell do? Executes commands Supports scripting (is a programming language)‏ Startup files

Shell Variables strings use assignment dereference

Example Shell Variables sh / ksh / bash PWD current working directory PATH list of places to look for commands HOME home directory of user MAIL where your is stored TERM what kind of terminal you have HISTFILE where your command history is saved

Subshells Creation Export

set command (shell builtin)‏ Print out a list of all the shell variables Set shell options noclobber – Keeps mv, cp, redirection from deleting an existing file -o vi (or -o emacs ) – sets command-line editing mode ignoreeof – ctrl-D won't exit the shell

Metacharacters Shell metacharacters are characters that are handled specially by the shell. Below is an (incomplete) list of shell metacharacters >, >>, <, <<, |Command redirection, pipe *, [], ?,{}File specification ;, &, ||, &&, ()Command Sequencing

Metacharacters (cont)‏ ", 'Grouping Text Double quotes and single quotes indicated that the enclosed text is to be treated as a unit, not as a group of words. #Commenting Rest of line after these characters is ignored \ (backslash) – the escape character Indicates that the character immediately following the backslash is to be treated literally. To remove a file named “#bogus” you can use the backslash rm \#bogus

Variable Substitution (cont)‏ Dereference Quoting/escaping ${} Concatenation

Command Substitution Command substitution allows the output (stdout) of a command to replace the command name. There are two forms: $(command)‏ `command`

Command Execution There are four formats for combining commands into one line: sequenced, grouped, chained, and conditional. ;, newline ()‏ | &&, ||

Grouped Commands If we apply the same operation to the group, we can group commands. Commands are grouped by placing them into parentheses. Example: echo “Month” > file; cal >> file (echo “Month” ; cal ) > file

Shell Syntax – wildcards (globbing)‏ Wildcards, and pathname expansion * # match any string (including empty)‏ ? # match any single character [set] # match characters listed in set (can be range)‏ [!set] # mach any character not given in set Examples ls *.c ls *.? ls *.[Hh][Tt][Ll] ls [a-z]

Shell Syntax – redirection and pipes File redirection and pipes < # redirect input from specified source > # redirect output to specified source >> # redirect output and append to specified source | # pipe the output from one command to the input to the next Examples grep word < /usr/dict/words ls > listing ls >> listing ls -l| wc -l

What Is A Script? Uses Why Why not?

Very Simple Script #!/bin/sh echo Hello World

Example: Simple Script Let’s create a shell script to give us information about the system. We create the script using a text editor. Let’s call the file “status” #!/bin/bash uptime users Be sure to insert an “enter” after the last line. Exit the editor

Running a Script Explicit sha-bang - #! exit checking return value

Conditional Expressions - test test, [] return value

File operators Unary file operators -eTrue if file exists -fExists, regular file -ddirectory -xexecutable -rreadable -wTrue if file exists and is writable -dTrue if file is a directory -OTrue if I own the file Binary -nt -ot -ef

string operators Unary -zTrue if string is of zero length -nTrue if string is not of zero length -lReturns length of string E.g. if [ -z "$myVar" ] ; then echo "\$myVar has null length" fi

[] – string operators binary  == != = E.g. if [ "$myVar" == "duh" ] ; then echo "I don't have a clue" ; fi if [ "abc" < "ABC" ] ; then echo "\"abc\" comes before \"ABC\"" fi

[] – arithmetic operators Binary operators: -lt -gt -le -ge -eq -ne E.g. if [ 2 –le 3 ] ; then ;echo "cool!" ; fi x=5 if [ "$x" –ne 12 ] ; then echo "Still cool" ; fi

[] – Logical Operators Logical expression tools  ! expressionLogical not (I.e., changes sense of expression)‏  e1 -o e2True if e1 or e2 is true.  e1 -a e2True if both expressions are true.  \( expression \)Works like normal parentheses for expressions; use spaces around the expression. Examples: test -e bin -a -d /bin is true [ -e ~/.bashrc -a ! -d ~/.bashrc ] is true.

[[ test ]] Bash added [[]] for more C-like usage: if [[ -e ~/.bashrc && ! –d ~/.bashrc ]] then echo "Let's parse that puppy" fi if [[ -z "$myFile" || ! –r $myFile ]] … Why sometimes quote $myFile, sometimes not (it's usually a good idea to do so)?

Arithmetic Expressions Bash usually treats variables as strings. You can change that by using the arithmetic expansion syntax: (( arithmeticExpr ))‏ (()) shorthand for the let builtin statement $ x=1 $ x=x+1 # "x+1" is just a string echo $x x+1

Arithmetic Expression (cont)‏ $ x=1 $ x=$x+1 # still just a string $ echo $x 1+1  Closer, but still not right. $ x=1 $ (( x=x+1 )) $ echo $x 2  Finally!

Sample: Basic conditional example if.. then #!/bin/bash if [ "$1" = "foo" ] ; then echo expression evaluated as true fi

Sample: Basic conditional example if.. then... else #!/bin/bash if [ "$1" = "foo" ] then echo 'First argument is "foo"' else echo 'First arg is not "foo"' fi

Sample: Conditionals with variables #!/bin/bash T1="foo" T2="bar" if [ "$T1" = "$T2" ] ; then echo expression evaluated as true else echo expression evaluated as false fi

Checking return value of a command if diff "$fileA" "$fileB" \ &> /dev/null then echo "Files are identical" else echo "Files are different" fi

Case Statement case $opt in a ) echo "option a";; b ) echo "option b";; c ) echo "option c";; \? ) echo \ 'usage: alice [-a] [-b] [-c] args...' exit 1;; esac

Special Variables $# the number of arguments $* all arguments all arguments (quoted individually)‏ $? return value of last command executed $$ process id of shell $HOME, $IFS, $PATH, $PS1, $PS2

Scripts and Arguments Positonal arguments aScript arg1 arg2 … Parameters “$n” Is the value of the n th paramter.  The command is parameter zero “$#” Is the number of parameters entered. “$*” Expands as a list of all the parameters entered except the command. Quoted list

Loops for, while and until The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string. The while executes a piece of code if the control expression is true, and only stops when it is false (or a explicit break is found within the executed code. The until loop is almost equivalent to the while loop, except that the code is executed while the control expression evaluates to false.

For samples Example $ for x in 1 2 3; do > echo $x > done Example $ for x in *; do > echo $x > done bin mail public_html …

For samples #!/bin/bash for i in $(cat list.txt) ; do echo item: $i done #!/bin/bash for (( i=0; i<10; ++i )) ; do echo item: $i done

While sample COUNTER=0 while (( COUNTER < 10 )) ; do echo The counter is $COUNTER (( COUNTER = COUNTER+1 ))‏ done COUNTER=0 while [ $COUNTER -lt 10 ] ; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done

Until sample #!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ] do echo COUNTER $COUNTER let COUNTER-=1 done

Example: Renaming Goal: Rename all the files of the form cs265l*.ppt to cs265l*.exe where * should be 02, 03 … 10. Solution: for loop for n in ; do mv cs265l$n.ppt cs265l$n.exe done  Moves cs265l02.ppt to cs265l02.exe, cs265l03.ppt to cs265l03.exe, etc.

Loop Control break terminates the loop continue causes a jump to the next iteration of the loop

Debugging Tip set -x for n in *; do echo $n done  Will display the expanded command before executing it. + echo bin bin + echo mail mail -e – errtrace -T – functrace

Functions As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion. Declaring a function is just a matter of writing function my_func { my_code }. Calling a function is just like calling another program, you just write its name.

Local variables #!/bin/bash HELLO=Hello function hello { local HELLO=World echo $HELLO } $ echo $HELLO $ hello $ echo $HELLO Hello World Hello

Functions with parameters sample #!/bin/bash function quit { echo 'Goodbye!' exit } function hello { echo "Hello $1" } for name in Vera Kurt ; do hello $name done quit Output: Hello Vera Hello Kurt Goodbye!

Parameter Expansion ${parameter:-word} Use Default Values. ${parameter:=word} Assign Default Values. ${parameter:?word} Display Error if Null or Unset. ${parameter:+word} Use Alternate Value.

More Parameter Expansion We can remove parts of a value: ${param#pattern} ${param##pattern} - removes shortest (#) or longest (##) leading pattern, if there's a match ${param#pattern} ${param##pattern} - removes shortest(%) or longest (%) trailing pattern, if match pattern is expanded just as in pathname expansion (globbing) - *, ?, []

Example – Parameter Expansion $ foo=j.i.c $ echo ${foo#*.} i.c $ echo ${foo##*.} c $ echo ${foo%.*} j.i $ echo ${foo%.*} j

Using the command line #!/bin/bash if [ -z "$1" ]; then echo usage: $0 directory exit fi SRCD=$1 TGTD="/tmp/" OF=home-$(date +%Y%m%d).tgz tar -cZf $TGTD$OF $SRCD