Unix Scripting Session 4 March 27, 2008.

Slides:



Advertisements
Similar presentations
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Advertisements

Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
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.
Shell Control Structures CSE 2031 Fall August 2015.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
1 Day 16 Sed and Awk. 2 Looking through output We already know what “grep” does. –It looks for something in a file. –Returns any line from the file that.
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.
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
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.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Chapter Four I/O Redirection1 System Programming Shell Operators.
40 Years and Still Rocking the Terminal!
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
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.
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.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
– Introduction to the Shell 1/21/2016 Introduction to the Shell – Session Introduction to the Shell – Session 3 · Job control · Start,
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
If condition1 then statements elif condition2 more statements […] else even more statements fi.
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.
#!/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.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
Lesson 5-Exploring Utilities
Shell Control Structures
Chapter 11 Command-Line Master Class
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
The UNIX Shell Learning Objectives:
CSC 352– Unix Programming, Fall 2012
Part 1: Basic Commands/Utilities
Lecture 9 Shell Programming – Command substitution
CSE 303 Concepts and Tools for Software Development
Writing Shell Scripts ─ part 3
Basic UNIX OLC Training.
What is Bash Shell Scripting?
Unix Scripting Dave Yearke
CSC 352– Unix Programming, Spring 2016
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
More advanced BASH usage
Linux Shell Script Programming
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
CST8177 Scripting 2: What?.
CSC 4630 Meeting 4 January 29, 2007.
Review.
Unix Scripting Session 2 March 13, 2008.
Presentation transcript:

Unix Scripting Session 4 March 27, 2008

Answers to the Last Homework Print arguments in reverse order: #!/bin/sh output="" while [ $# -ne 0 ]; do output="$1 $output" shift done echo "$output"

Answers to the Last Homework Manage arguments in sets of three: #!/bin/sh if [ $# -eq 0 ]; then echo "Usage: $0 [arg [+-] arg] ..." 1>&2 exit 1 fi while [ $# -ne 0 ]; do tuple="" error="none" for arg in one two three; do if [ "$arg" = "one" ]; then tuple="$1" else tuple="$tuple $1" if [ "$arg" = "two" ]; then case "$1" in "+") ;; "-") ;; *) error="unknown operator $1 in" ;; esac [0-9]*) ;; *) error="$1 is not a number in" ;; shift echo "Error: Missing argument in $tuple" 1>&2 done if [ "$error" != "none" ]; then echo "Error: $error $tuple" 1>&2 echo "$tuple"

Answers to the Last Homework Count the words in “space_tff”, version one with an unexpected bug: #!/bin/sh linecount=0 wordcount=0 cat /eng/home/yearke/scripting/space_tff | \ while read line; do linecount=`expr $linecount + 1` for word in $line; do wordcount=`expr $wordcount + 1` done echo "The file has $linecount lines and $wordcount words."

Answers to the Last Homework Count the words in “space_tff”, version two with more accurate use of a child process: #!/bin/sh linecount=0 wordcount=0 cat /eng/home/yearke/scripting/space_tff | \ (while read line; do linecount=`expr $linecount + 1` for word in $line; do wordcount=`expr $wordcount + 1` done echo "The file has $linecount lines and $wordcount words.")

Answers to the Last Homework Count the words in “space_tff”, version three using the IFS environment variable: #!/bin/sh linecount=0 wordcount=0 ORIGIFS="$IFS" IFS=" " for line in `cat /eng/home/yearke/scripting/space_tff`; do linecount=`expr $linecount + 1` LINEIFS="$IFS" IFS="$ORIGIFS" for word in $line; do wordcount=`expr $wordcount + 1` done IFS="$LINEIFS" echo "The file has $linecount lines and $wordcount words."

Answers to the Last Homework Print “space_tff” with the lines reversed: #!/bin/sh cat space_tff | \ while read line; do newline="" for word in $line; do newline="$word $newline" done echo "$newline"

Job Control Processes that are attached to your terminal session are said to be running in the “foreground”. Processes that are unattached from your terminal are running in the “background”. To run a job in the background, simply put an ampersand (“&”) at the end off the line: ./takesalongtime &

Job Control The “wait” command tells the parent process to wait for the child to finish: #!/bin/sh ./childjob & # Do some other stuff wait # Do some stuff with the child's results echo "All done. Bye bye!" A background task will stop if you log off. To avoid this, “nohup” it and redirect all output: nohup ./childjob > results 2>&1 &

Using Exit Status With “if” The exit status of a command can be used directly with the “if” statement: if grep Space space_tff; then echo "Found it!" else echo "Didn't find it." fi Sometimes you want to hide the actual program output to avoid cluttering your script’s output: if grep Space space_tff > /dev/null 2>&1; then The “/dev/null” file is the system bit-bucket. Anything redirected to it is thrown away.

Conditional Process Execution (the “Short Circuit” operators) Recall that “;” allows multiple commands per line. “&&” and “||” do the same thing, but the following command or commands are only executed if the previous command succeeded or failed, respectively: grep Space space_tff && echo "Found Space." grep Kirk space_tff || echo "Didn't find Kirk." Or, hiding any program output: grep Space space_tff > /dev/null 2>&1 && echo "Found Space." grep Kirk space_tff > /dev/null 2>&1 || echo "Didn't find Kirk.“ Fun fact: These have the opposite meaning in some older versions of C-Shell, but this is mostly fixed now. Be careful, though.

Twelve Useful Unix Commands Reference: http://www.eng.buffalo.edu/~yearke/unix/commands12.shtml man ls cat tail (and head) grep (and egrep and fgrep) Sort wc diff (and cmp) find sed awk cut

“man” and “ls” Mostly used interactively, not in scripts. In fact I can’t think of why you would use “man” in a script, but it’s too important to ignore. “ls” is mostly interactive but does have some scripting uses. As you become more familiar with “find”, though, you may tend to not use “ls” as much in scripts.

“cat”, “tail”, and “head” cat is most useful for feeding a file through standard output to another process, or for concatenating (hence the name) a bunch of files together into stdout: tail shows (by default) the last 10 lines of a file, and head (by default) the first 10 lines. These can be useful when you want to process part of a very large file, such as a system log.

The “grep” Clan grep is the “Group Regular Expression Parser”. It finds text strings in files or streams. There are a lot of metacharacters and arguments that can be used to fine-tune the search parameters. egrep (Enhanced grep) adds additional metacharacter capabilities which can provide even better pattern-matching. fgrep (Fixed grep) treats all metacharacters as literal characters, eliminating the need to escape them with backslash. “f” does not stand for fast!

“sort” and “wc” The sort command, obviously, sorts files. It takes a lot of arguments that can be used to tune the sort fields and how they’re treated. Filtering out repeated lines and treating fields as numbers are particularly useful. The word counter, wc, also counts lines and characters. I tend to use the line counting feature a lot when I want to determine if a command returned meaningful output, or not.

Exercises None this time. 

Links Twelve useful Unix commands (for next time): As usual, the current Solaris Bourne Shell man page: http://docs.sun.com/app/docs/doc/819-2239/sh-1?l=en&q=man+pages&a=view Twelve useful Unix commands (for next time): http://www.eng.buffalo.edu/~yearke/unix/commands12.shtml (This is part of a work-in-progress and might be a little rough, I haven’t looked at it in a while but will do so this week.)

Next Time Twelve useful Unix commands for scripts, part 2: diff (and cmp) find sed awk cut Regular expressions.