Unix Scripting Dave Yearke

Slides:



Advertisements
Similar presentations
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
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.
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.
Linux+ Guide to Linux Certification, Second Edition
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.
Introduction to Linux and Shell Scripting Jacob Chan.
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.
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.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Linux+ Guide to Linux Certification, Third Edition
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.
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)
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.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
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.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
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.
Linux+ Guide to Linux Certification, Second Edition
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.
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.
2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.
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.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
CIRC Summer School 2016 Baowei Liu
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
CSC 352– Unix Programming, Spring 2016
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CSC 352– Unix Programming, Fall 2012
Lecture 9 Shell Programming – Command substitution
Unix Scripting Session 4 March 27, 2008.
Shell Script Assignment 1.
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall 2012
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
UNIX Reference Sheets CSE 2031 Fall 2010.
CSE 303 Concepts and Tools for Software Development
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Unix Scripting Session 2 March 13, 2008.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Unix Scripting Dave Yearke Session 2 March 13, 2008

Tangent: Text Editors nano or pico – Really easy to use, not much power, but good for starting scripting. Emergency exit: Control-X vi – Preferred editor for Unix admins, powerful but learning curve involved. Emergency exit: Escape key followed by :q! emacs – Incredibly powerful, menu bar available in window mode, text mode can be challenging. Emergency exit: Control-X Control-C Other choices: gedit (X/GNOME), dtpad (X/CDE), vim/gvim (many including Windows)

Basic Shell Statements Variable assignment: name="george" count="5" today=`date` Variable unassignment: unset name Do basic math on a variable: counter=`expr $counter + 1` Read from standard input (usually console): read answer Print text: echo "Your name is $name"

Basic Shell Statements Conditional Execution: if .. elif .. else .. fi case word in pattern) .. ;; ... esac Loops and Iteration: for name in words; do .. done while list do; .. done

if … fi The “if” statement provides conditional execution of commands: count=5 if [ $count -eq 0 ]; then echo "Nothing left to do" fi name="Herbert" if [ "$name" != "George" ]; then echo "You're not George. I will name you George."

if … fi When doing text comparisons, always put variables inside double-quotes: name="" if [ $name != "George" ]; then echo "You're not George. I will name you George." fi “test: argument expected” is an error you may see a lot of at first. Because variable substitution happens early in the process, the above reduces to: if [ != "George" ]; then To be safe, put all variables in double quotes. It won’t hurt. Don’t use single quotes, variables aren’t expanded in them.

Test Test operators (located inside the square brackets) are described in the man page for “test”, not “sh”. Common String Test operators: Numeric String Equality $a –eq $b "$a" = "$b" Not Equal $a –ne $b "$a" != "$b" Greater Than $a –gt $b Less Than $a –lt $b Empty String -z "$a" Non-Empty String -n "$a"

Test Common File Test operators: Joining test operators: File Exists -f "$a" Directory Exists -d "$a" Symlink Exists -h "$a" File is Readable -r "$a" File is Writeable -w "$a" File is Executable -x "$a“ Negate any of the above ! –f "$a" Joining test operators: File Exists and is readable: if [ -f "$a" –a –r "$a" ]; then File is a file or a directory: if [ -f "$a" –o –d "$a" ]; then

if … elif … else … fi The “if” statement can provide alternative actions if the condition isn’t met: read response if [ "$response" = "yes" ]; then echo "We want to do that." elif [ "$response" = "no" ]; then echo "We don't want to do that." else echo "Huh?" fi

case … esac The “case” statement can evaluate alternate results for the same condition, and can also use file matching wildcard patterns: read response case "$response" in [Yy]*) echo "We want to do that." ;; [Nn]*) echo "We don't want to do that." ;; *) echo "Huh?" ;; esac

Getting Loopy The “for” loop: The “while” loop: for file in a b c d e; do echo "$file" done The “while” loop: done="no" while [ "$done" = "no" ]; do read magicword if [ "$magicword" = "plugh" ]; then done="yes" fi

I/O Redirection One of the most powerful features of Unix Three standard I/O channels: 0 – Standard Input (stdin) 1 – Standard Output (stdout) 2 – Standard Error Output (stderr) Redirect a file into a command using stdin: grep SENS < myreport Redirect command output into a file using stdout: ps –ef > processes Redirect input and output using stdin and stdout: grep SENS < infile > outfile

I/O Redirection Redirect stdout and stderr into the same file: grep SENS reports/* > out 2>&1 Redirect stdout and stderr into different files: grep SENS reports/* > out 2> badstuff Print to stderr instead of stdout: echo "It all went wrong!" 1>&2

Pipes Pipes take the output of one command and makes it the input of another command: Print just the year from the date command: date | awk -e '{print $6;}' date | cut –c 25-28 date "+%Y" Show how many processes are running: ps -e | wc -l Show how many processes I’m running: ps -ef | grep yearke | wc -l ps –fu yearke | wc –l The concept of the pipe is one of the fundamental underpinnings of Unix.

Semi-Practical Example 2.1 What side of the Node am I in? #!/bin/sh cd homedir=`pwd` case "$homedir" in /nsm/home/*) side="Natural Sciences and Math" ;; /eng/home/*) side="Engineering School" ;; *) side="Unknown" ;; esac if [ "$side" = "Unknown" ]; then echo "I'm sorry, I cannot determine your affilation." else echo "Your affiliation is the $side part of SENS." fi

Semi-Practical Example 2.2 Examine some files in the current directory: #!/bin/sh counter=0 for file in t*; do if [ -f "$file" -a -w "$file" ]; then counter=`expr $counter + 1` fi done echo "There are $counter writeable files starting with 't'."

Homework Write a script that runs on a Solaris 10 or Linux system (the unix.eng timeshares are great for this) and counts the number of files and directories under the “/etc” directory. You do not need to traverse subdirectories and enumerate files and directories under them, just the count under the top-level of “/etc” will be sufficient.

Links Current Solaris Bourne Shell man page: http://docs.sun.com/app/docs/doc/819-2239/sh-1?l=en&q=man+pages&a=view An Introduction to the Unix Shell: http://steve-parker.org/sh/bourne.shtml The Unix Programming Environment: http://www.amazon.com/Unix-Programming-Environment-Prentice-Hall-Software/dp/013937681X

Next Time Argument Clinic Child/Parent issues (environment) A lurking “gotcha” in loops