CS214 Advanced UNIX Lecture 4

Similar documents
Shell programming. Introduction to Operating Systems

Shell Control Structures

Conditional Control Structures. Dr.T.Logeswari

Shell Control Structures

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

Bourne Shell Reference

Name: Tej. D. Shah Subject:CC-304 Linux Uni. Practical programme College :L.J. College Of Computer Application. Questions:

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Assignment 3, Due October 4

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Useful Unix Commands Cheat Sheet

CS Unix Tools & Scripting

Assignment 2. Summary. Some Important bash Instructions. CSci132 Practical UNIX and Programming Assignment 2, Fall Prof.

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Shells & Shell Programming (Part B)

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

There are some string operators that can be used in the test statement to perform string comparison.

CSC 2500: Unix Lab Fall 2016

Lecture 02 The Shell and Shell Scripting

COMP 4/6262: Programming UNIX

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

The Unix Shell & Shell Scripts

# use temporary files to store partial results # remember to delete these temporary files when you do not need them anymore

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

COMP2100/2500 Lecture 17: Shell Programming II

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

Last Time. on the website

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Answers to Even-numbered Exercises

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

C Shell Tutorial. Section 1

6.033 Computer System Engineering

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

PART I : MCS-041. (b) Use head and tail in a pipeline to display lines 25 through 75 of a file? $Head 25 file name $Tail 75 File name

Lab #12: Shell Scripting

bash, part 3 Chris GauthierDickey

Assignment clarifications

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

LOG ON TO LINUX AND LOG OFF

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Vi & Shell Scripting

CS1101: Lecture 9 The Shell as a Programming Language

LING 408/508: Computational Techniques for Linguists. Lecture 5

A Brief Introduction to the Linux Shell for Data Science

More Scripting Techniques Scripting Process Example Script

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Basic Linux (Bash) Commands

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Shell Programming (Part 2)

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Do not start the test until instructed to do so!

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

A Brief Introduction to Unix

Introduction to UNIX Shell Exercises

Essentials for Scientific Computing: Bash Shell Scripting Day 3

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

System Programming. Session 6 Shell Scripting

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Utilities. September 8, 2015

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Chapter 4. Unix Tutorial. Unix Shell

Computer Systems and Architecture

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Common File System Commands

A shell can be used in one of two ways:

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

Csci 132 Spring 13. Assignment 2 Due: Tuesday, March 5 (by 11:59PM)

Shell Programming (ch 10)

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

Basic Linux Commands Manual Pdf Examples And Syntax

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

bash Execution Control COMP2101 Winter 2019

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Scripting Languages Course 1. Diana Trandabăț

CS197U: A Hands on Introduction to Unix

Introduction to Shell Scripting

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

Introduction To. Barry Grant

CHAPTER 2 THE UNIX SHELLS

University of Windsor : System Programming Winter Midterm 01-1h20mn. Instructor: Dr. A. Habed

RHCE BOOT CAMP. System Administration

Shells and Shell Programming

Exercise sheet 1 To be corrected in tutorials in the week from 23/10/2017 to 27/10/2017

CENG 334 Computer Networks. Laboratory I Linux Tutorial

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Linux Shell Script. J. K. Mandal

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Note for the LaT E X version of this Document

Original Script. Display commands to manually creating an account. #!/bin/bash

1 Very Short Linux Manual

Part 1: Basic Commands/U3li3es

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Transcription:

CS214 Advanced UNIX Lecture 4 March 2, 2005 Passing arguments to scripts When you invoke a command like > cat f1 f2 f3 f1, f2, f3 are all arguments you pass to the cat command. Many times in your script you will also want to be able to run the script with various arguments. Take for example the wsc.sh script from the assignment; you can invoke it as >wsc.sh newdatale and this will have the effect of sorting all the words in newdatale, and update the le wordstat.txt so that it shows all words in alphabetical order and how frequent they are in all the les considered so far, including newdatale. It also makes perfect sense to invoke your script with more than one le; for example >wsc.sh datale1 datale2 The result should the same as running your script on the rst le, and again on the second le: >wsc.sh datale1 >wsc.sh datale2 You can actually call wsc.sh with any number of les. These les will be passed as arguments to the script. The shell allows you to access the arguments inside the script in very simple way: $# gives you the number of arguments passed to your script (in the case above, $# equals 2) 1

$1 is the value of the rst argument, $2 the value of the second argument, and in general, $i, for some i from 1 to $#, is the value of the i-th argument (in our case, $1 is datale1, $2 is datale2) $* and $@ both give you all arguments; there is however a difference in the way the shell expands them: $* expands to $1 $2...$n, while $@ expands to $1 $2... $n ; we will see that in general it is better to use $@. It is also useful to know that $? is exit code of whatever program was last executed and $$ is current process id. Let s look now at two examples of how arguments can be used in your scripts. Suppose for example that you want to write your own calculator; in your script you will perform a number of standard operations. For example, you will want to pass the script two numbers and get back their product. Here is how you can this: echo $(( $1 * $2 )) It is good practice to write some code to deal with the situation when, by mistake, you pass less (or more) the intended number of arguments: if [ $# -ne 2 ] echo "Default 0 * 0 =0" else echo $(( $1 * $2 )) Here is a script that takes a lename as an argument and es two things: displays it and counts the number of lines in it: if [ "$#"!= 1 ] echo "Wrong number of arguments!" exit 1 else read -p "Display le? (y/n) " ans if [ "$ans" == "y" -o "$ans" == "Y" -o "$ans" == "yes" -o "$ans" == "Yes" ] less $1 wc -l < $1 2

Loops A while loop has the form while cmd cmd2... ne and has the effect of executing the sequence of commands, cmd2... as long as command cmd is successful (i.e. its exit code is 0). Here is a simple while loop that prints all numbers from 1 to 10: i="1" while [ $i -le 10 ] echo "$i " i= expr $i + 1 ne If you want to execute a sequence of commands until the command cmd is successful, use the following construct: until cmd cmd2... ne Here is how you can print all numbers from 1 to 10, this time using an until loop: i="1" until [ $i -ge 11 ] echo i is $i i= expr $i + 1 ne If you want to execute a sequence of commands multiple times, use a for loop: for var in string1 string2... stringn cmd2... ne 3

Look at the following simple for loop: for lename in f1 f2 cat $lename ne It has the same effect as executing the cat command twice, rst for f1, and for f2. You can now write a script that counts the total number of lines in the les passed as arguments: i="0" for f in "$@" j= wc -l < $f i= expr $i + $j ne echo $i Name this le lcount.sh. To count the number of lines in all les in your current directory, run >lcount.sh * Notice that this will not descend in subdirectories in your current directory. We will see in the next lecture how to modify your script to that. If you are familiar with Latex, you know that you can write a le with the extension.tex and run the command latex foo.tex and generate a number of les: a.dvi le that can be later on transformed into a.ps or.pdf le, and can be seen with.xdvi, an auxiliary le foo.aux, a log le and so on. Latex is very useful for writing articles and presentations, and it is sometimes handy to have a script that will show you what.tex les you have in your current directoy, what.dvi les, and so on. You can see that such a script is a specialized ls utility; call this script lstex.sh. You want to run the script with one option.tex,.dvi and so on, and see as a result all the les of that type in your current directory. In addition, you might want to update each.tex le, or transform each.dvi le into a.ps and so on. Here is how you can write your script ( not worry if you not understand what each command is ing, just notice how you can different things for different kind of les, in the same script):!/bin/bash/ #Man entry # Script that lists different types of les generated by Latex 4

# # syntax: lstex.sh -opt # opt may be: tex, dvi, ps, pdf, bib, aux, log, cls, sty #End man entry if [ "$#" -ne "1" ] echo "1 argument! Try -tex, -ps, -pdf, -dvi, -bib, -sty, -cls, -log or -aux" exit 1 else case "$1" in -tex) for f in ls grep \.tex$ echo $f read -p "Update? (Y/N) " ans if [ "$ans" == "Y" -o "$ans" == "y" ] latex $f ne bibtex $f #update the bibliography latex $f #needed for correct labelling -ps) for f in ls grep \.ps$ echo $f read -p "Transform to pdf? (Y/N) " ans if [ "$ans" == "Y" -o "$ans" == "y" ] ps2pdf $f ne -pdf) ls -all grep \.pdf$ -dvi) for f in ls grep \.dvi$ 5

echo $f read -p "Transform to ps? (Y/N) " ans if [ "$ans" == "Y" -o "$ans" == "y" ] read -p "Output le " ans dvips $f -o $ans.ps ne -bib) ls grep \.bib$ -sty) ls grep \.sty$ -cls) ls grep \.cls$ -log) for f in ls grep \.log$ rm -i $f ne -aux) for f in ls grep \.aux$ rm -i $f ne *) echo "Invalid option. Try -tex, -ps, -pdf, -dvi, -bib, -sty, -cls, -log or -aux" exit 1 esac Notice that we have used a case statement; its general form is case var in p1) 6

esac p2)... pn) cmd2 cmdn Here p1, p2, up to pn are patterns that are rst expanded by the shell. The statement has the effect of trying to rst match var with p1; if it succeeds, it executes, if not it tries to match var with p2 and so on. 7