COMP2100/2500 Lecture 17: Shell Programming II

Size: px
Start display at page:

Download "COMP2100/2500 Lecture 17: Shell Programming II"

Transcription

1 [ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help] COMP2100/2500 Lecture 17: Shell Programming II Summary I work through a more substantial shell scripting example. Aims To demonstrate some of the processes behind developing a shell script. To introduce more features of the Bash language that weren't covered in the previous lecture. 1. The Problem Since student accounts have a strict quota and students are often running into trouble with the quota, it would be nice to be able to produce a list of the ten largest les in your account. These would be good places to start when deleting les to get back under quota. So our task is: Write a bash script which produces a list of the ten largest les in the user's area, together with their sizes. That's the original statement of the problem, and it's not particularly 1 of 15 01/05/15 09:57

2 precise. It will get modied as we go. This is one of the things that happens in any software project: once you understand the problem a little, the requirements must be changed, and they keep on changing throughout the project. [Richard's comment: Work really hard at getting the requirements right before you start designing and coding. For such a simple problem it is all too easy to start coding right away; jumping in early frequently leads to disaster.] 2. A Rough Strategy Here's a plan for how to do it. I'm remembering that in the previous lecture we had a way to list (and count) all les and directories, so I plan to start with that. 1. List all les and directories. 2. Weed out the directories to get a list of all les. 3. Add the size to each item in the list. 4. Sort this list into descending order of size. 5. Print the rst ten. Step 1 we know how to do already. We can try it at the command line before we even start writing a script. (But note the caveat in the previous lecture about the output including blank lines.) bash$ ls -R.: CVS bigles1 bigles2 bigles3 bigles4 bigles5 lec03.html CVS: Entries Repository Root bash$ ls -R grep -v ':$' CVS bigles1 bigles2 bigles3 bigles4 bigles5 lec03.html Entries Repository 2 of 15 01/05/15 09:57

3 Root I just ran that in the directory where I'm writing this, so you can see the les I'm working on. The les bigles1... bigles5 are the ve stages in the development of the script for this lecture. The CVS directory is where the CVS version control system keeps stuff it needs. We'll be covering version control - using a simpler system called RCS - later this semester. This is working, but it has a few problems. One is that it still lists the directories as well as the ordinary les. That's not too much of a problem, because there is a way to tell the difference. We'll be see that later. The bigger problem is that for les in the CVS subdirectory it's just listing their names, but not the path to them. And for this script to be useful, the user needs to know where the big les are, not just what their names are. So we need a way to display the full paths to the les we nd. I looked through the manual for the ls command, (by typing man ls but didn't nd anything useful. I could imagine having the script sort through and instead of throwing away all the lines ending in a colon, keeping them and sticking that directory path onto the start of each le name. But it sounds complicated, and there's a better way. 3. The du command There's a very useful command du which stands for disk usage. Without any options it just lists the size (in something called blocks, which are a different size on different computers) of a directory and all its subdirectories: bash$ du 4./CVS 34. Not particularly useful, but looking through the manual page (by typing man du) I found that there's an option -a to have it list sizes of all les as well as directories. Just what we want, and the important thing is that it lists each le with its full path (either from the current directory, or from the directory given on the command line). There's also a -k option which makes sure that the sizes are in blocks of 1024 bytes - K's. bash$ du -ka 1./CVS/Root 3 of 15 01/05/15 09:57

4 1./CVS/Repository 1./CVS/Entries 4./CVS 1./bigles1 22./lec03.html 1./bigles2 1./bigles3 1./bigles4 2./bigles5 33. bash$ du -ka /home/barnes/2001/comp2100/lectures/lec03/ 1 /home/barnes/2001/comp2100/lectures/lec03/cvs/root 1 /home/barnes/2001/comp2100/lectures/lec03/cvs/repository 1 /home/barnes/2001/comp2100/lectures/lec03/cvs/entries 4 /home/barnes/2001/comp2100/lectures/lec03/cvs 1 /home/barnes/2001/comp2100/lectures/lec03/bigles1 22 /home/barnes/2001/comp2100/lectures/lec03/lec03.html 1 /home/barnes/2001/comp2100/lectures/lec03/bigles2 1 /home/barnes/2001/comp2100/lectures/lec03/bigles3 1 /home/barnes/2001/comp2100/lectures/lec03/bigles4 2 /home/barnes/2001/comp2100/lectures/lec03/bigles5 33 /home/barnes/2001/comp2100/lectures/lec03 This is the sort of list we can work with. 4. Removing the sizes In order to weed out the directories from this list, we actually need to throw away those sizes. We'll get them back later when we only have ordinary les in our list. We can get rid of the sizes using the cut command from last lecture. A quick check on the manual page gives information on the command-line options: -f, --elds eld-list Print only the elds listed in eld-list. Fields are separated by a TAB by default. -d, --delimiter delim For -f, elds are separated by the rst character in delim instead of by TAB. (When you type man cut, the information is presented to you by the paging program less. This is useful for reading through large les: you can invoke it by typing less le. When you're inside less, you can use the space bar to move down a page, the Enter key to move down a line, the `u' key to move up a page, the `g' to go to the top of the le, `G' to go to the end. You can also search for a word by typing `/' followed by the word. This will highlight all occurrences, and you can move from one to the next by typing `n'. You get out by typing 4 of 15 01/05/15 09:57

5 `q'.) OK, so it looks like the numbers are separated from the le and directory names by TABs, so we can try bash$ du -ka cut -f2./cvs/root./cvs/repository./cvs/entries./cvs./bigles1./lec03.html./bigles2./bigles3./bigles4./bigles5. This is what we need for the next step. 5. Weeding out the directories Some of the lines we have there are directories rather than les, so we want to get rid of them and leave a list of just the les. At this point I think it's time to stop playing around interactively and start writing my script. The rst version, with nothing new in it, is: #!/bin/bash # Get a list of all les (and directories etc) les=$(du -ka cut -f2) # Print the list echo $les When I run this (after remembering to make it executable), I get: bash$ bigles1./cvs/root./cvs/repository./cvs/entries./cvs./bigles1./le c03.html./bigles2./bigles3./bigles4./bigles5. As you can see, the result looks a little different, but that's OK. It's a list of all the les and directories, ready for us to do some more work. Now we want to go through that list, one at a time, and only keep the real les. How to do this? Back to the manual pages. This time look at the manual for bash itself. It turns out that there's a very useful builtin 5 of 15 01/05/15 09:57

6 command called test, which evaluates an expression and returns a true or false answer (as zero or not-zero) as its return code. It can do comparisons similar to boolean expressions in Eiffel. It can also check whether a le exists, whether it is a directory, whether it is an ordinary le, and so on. To make it look even more like ordinary boolean expressions, it has two alternative forms: test expr and [ expr ]. (The spaces around the expression are important in the second form.) The command [ -d le ] returns 0 (true) if le is a directory, non-zero otherwise. Similarly [ -f le ] tells whether le is a normal le or not, and [ -e le ] simply tells whether le exists. We can use a for loop to examine the les on our list one by one, and use this test to select only the real les. Here's the second version of the script. #!/bin/bash # Get a list of all les (and directories etc) les=$(du -ka cut -f2) # echo $les # Write list of all regular les with sizes to a temporary le tmp=".tmp_bigles" for f in $les do if [ -f $f ] du -ka $f >> $tmp done # Print the list cat $tmp This excludes not just directories, but any other weird things that might be there. (Is this the right thing to do? Good question. That's where our requirements aren't really precise enough.) Running the new version of the script gives us: bash$ bigles2 1./CVS/Root 1./CVS/Repository 1./CVS/Entries 1./bigles1 23./lec03.html 6 of 15 01/05/15 09:57

7 1./bigles2 1./bigles3 1./bigles4 2./bigles5 A couple of points. The usual output redirection is with >. Using >> appends output to the end of the le named, rather than overwriting it. I got the sizes back by just calling du again. Now I've made a mistake here, which you'll see when I run the script again: bash$ bigles2 1./CVS/Root 1./CVS/Repository 1./CVS/Entries 1./bigles1 23./lec03.html 1./bigles2 1./bigles3 1./bigles4 2./bigles5 1./CVS/Root 1./CVS/Repository 1./CVS/Entries 1./bigles1 1./.tmp_bigles 24./lec03.html 1./bigles2 1./bigles3 1./bigles4 2./bigles5 Since my script just appends to.bigles, that le is just going to grow and grow. I need to delete it when I'm nished. And in order to be sure that I don't clobber something important, I'd better also check that it doesn't already exist before I do anything to it. So here's version 3. #!/bin/bash # Get a list of all les (and directories etc) les=$(du -ka cut -f2) # echo $les # Make sure we're not about to clobber someone's data tmp=".tmp_bigles" if [ -e $tmp ] 7 of 15 01/05/15 09:57

8 echo "$tmp already exists - exiting." exit 1 # Write list of all regular les with sizes to $tmp for f in $les do if [ -f $f ] du -ka $f >> $tmp done # Print the list cat $tmp # Remove the temporary le rm -f $tmp The [ -e $tmp ] test checks whether there is already a le with that name. If there is, we'll just stop, rather than messing up any data that might be there. (Chances are that any le with a name starting with ``tmp'' probably isn't important, but you never know. Better to leave it up to the user to decide what to do.) The command exit 1 terminates execution of our script with return code 1, which says that the script was unsuccessful. This is important: if anyone ever calls this script from another script, they need to be able to check the return value to tell whether it succeeded or failed. The other thing I added is the last line. As you know, the rm command removes les. The option -f stands for ``force''. In other words, do it anyway, even if the user has their account set up so that rm usually asks for conrmation from the user. (You can do that by editing your.cshrc and inserting the line alias rm 'rm -i'.) Let's try it. bash$ bigles3.tmp_bigles already exists - exiting. bash$ rm.tmp_bigles bash$ bigles3 1./CVS/Root 1./CVS/Repository 1./CVS/Entries 1./bigles1 24./lec03.html 1./bigles2 8 of 15 01/05/15 09:57

9 1./bigles3 1./bigles4 2./bigles5 6. Sorting the list Our next task is to take that list of le sizes and names, sort it into descending order (of size) and print the rst ten. This is much easier than you might think. We don't have to go back to our rst year notes and try to code the quick-sort algorithm in bash. The philosophy of shell scripting is to always look for a quick-and-dirty way to do things, and the quickest and dirtiest is to re-use code someone else has already written. The Unix system has hundreds, perhaps thousands of well-written utility programs for all sorts of applications. For most tasks you might want to do, you're probably not the rst person to want to do them. In our case, we want to sort some data - and we're certainly not the rst people to want to do that. The sort program exists just for this purpose, and has been tried and tested for many years. No matter how hard you try, you would be very unlikely to write a better generalpurpose sorting program. [Richard's note: `tried and tested' doesn't mean all that much. Better to have a proof that the algorithms used are correct.] Again, we check the manual page for the command options: -n Restrict the sort key to an initial numeric string, consisting of optional blank characters, optional minus sign, and zero or more digits with an optional radix character and thousands separators (as dened in the current locale), which will be sorted by arithmetic value. An empty digit string is treated as zero. Leading zeros and signs on zeros do not affect ordering. -r Reverse the sense of comparisons. So it looks as if we want sort -rn $tmp. This will read the lines from the le $tmp, sort them into reverse numeric order (i.e. descending order of size) and write the results to standard output. All that remains for us to do is to print the rst ten lines. We can do that with a pipe to the head command. So the nished script is: #!/bin/bash # Get a list of all les (and directories etc) 9 of 15 01/05/15 09:57

10 les=$(du -ka cut -f2) # Make sure we're not about to clobber someone's data tmp=".tmp_bigles" if [ -e $tmp ] echo "$tmp already exists - exiting." exit 1 # Write list of all regular les with sizes to $tmp for f in $les do if [ -f $f ] du -ka $f >> $tmp done # Sort the list and print the top ten sort -rn $tmp head -10 # Remove the temporary le rm -f $tmp Here's the result of running it. bash$ bigles4 24./lec03.html 2./bigles5 1./bigles4 1./bigles3 1./bigles2 1./bigles1 1./CVS/Root 1./CVS/Repository 1./CVS/Entries bash$ cd.. bash$ lec03/bigles4 29./lec02/lec02.html 24./lec03/lec03.html 17./lec19/lec19.html 16./lec26/lec26.html 15./lec09/lec09.html 14./lec17/lec17.html 14./lec11/lec11.html 13./lec12/lec12.html 13./lec04/lec04.html 10 of 15 01/05/15 09:57

11 12./lec23/lec23.html 7. Are we done yet? This script does pretty much what we wanted, but not quite. The initial requirements said list the ten biggest les in the user's area, but this script lists the ten biggest in the current directory and all its subdirectories. Running it from the user's home directory will do the job required, but it's worth thinking about. Why not extend it to allow the user to specify a directory on the command line, but still use the current directory as a default? It would also be nice to be able to change the number 10 to something else. To do this, we're going to have to read arguments from the command line. In the previous lecture we learned about the special variables which give access to the command line arguments. Now we're going to use them in a more sophisticated way. For this we need two new bash commands. The rst is shift. This throws away the rst command line argument (${1}) and moves all the others down one position, decrementing the value of ${#} at the same time. This is useful for loops which process one command argument at a time. The second new bash thing is the case construction, which is a bit like the inspect statement in Eiffel. It tries to match something against each of a list of regular expressions in turn, and carries out the action corresponding to the rst successful match. The regular expression syntax used is the same one as is used for le names by the ls command (which means it's not the same as the one for grep). (By the way, this expansion is called ``globbing''.) Here's the enhanced version of the bigles script. #!/bin/bash # bigles # List the n (or 10) largest les in the directory specied (or the # current directory by default), and all its subdirectories, together # with their sizes. # Version 5 # Ian Barnes, 1 February of 15 01/05/15 09:57

12 # Set default values debug= number="-10" # Parse command line arguments while [ ${#} -gt 0 ] do case ${1} in -*) # Assume that what's after the - is a number number=${1} ;; *) # It's a directory dir=${1} esac shift done [ $debug ] && echo number=${number} [ $debug ] && echo dir=${dir} # Get a list of all les (and directories etc) if [ ${dir} ] if [ -d ${dir} ] les=$(du -ka ${dir} cut -f2) else echo Error: ${dir} is not a directory! exit 1 else les=$(du -ka cut -f2) [ $debug ] && echo ${les} # Make sure we don't clobber any data tmp=".tmp_bigles" if [ -e ${tmp} ] echo Error: $tmp already exists! exit 1 # Write list of all regular les with sizes to $tmp 12 of 15 01/05/15 09:57

13 for f in ${les} do if [ -f ${f} ] du -ka ${f} >> ${tmp} done [ $debug ] && cat ${tmp} # Sort the list and print the top -${number} sort -rn ${tmp} head ${number} # Remove the temporary le rm -f ${tmp} [Richard's note: can the code duplication (the du cut line) be avoided?] There's one other new thing there. I have created a new variable debug, which is initially assigned the empty string (meaning ``False''). If I change the assignment statement at the start of the script to assign any non-empty value, the script will print a whole lot of helpful information about its progress. The notation statement1 && statement2 means ``Execute statement1. If the return code is `True' (= `success' = `zero') execute statement 2.'' In other words, it is shorthand for if statement1 statement2 Similarly you can write statement1 statement2 which means ``Execute statement1. If the return code is `False' (= `failure' = `non-zero') execute statement 2.'' This is particularly useful for tasks that might fail, and which should terminate the script if they do. do something dubious { echo Failed; exit 1; } [Richard's note: watch the spacing and punctuation. The space after { 13 of 15 01/05/15 09:57

14 is absolutely necessary, as is the ; just before the }. But in this case you don't need the space before the { or the space before the }, because here they are immediately preceded by other shell punctuation. It does makes sense (once you get used to it), but remember that these little things make the difference between your script working or not working.] What I've done is simpler: the test instruction, if given a single string, returns true if the string is non-empty, false otherwise. Exercises Exercise 1: Modify the script so that debug mode can be turned on by giving the script a -d option on the command line like this: bigles -d -17 ~. Exercise 2: Modify the script so that it prints a usage message and quits if the user gives the option -h or if there is more than one directory given, or if an unknown option is supplied (i.e. a minus sign followed by something which is not a number, `d' or `h'). Exercise 3*: Modify the script so that it uses sizes in bytes (not K) for sorting and output. (Hint: If you do ls -l le you will get a long line containing all the information you want, but a lot of other stuff too. The cut program won't do it the way we've used it so far, because the separators aren't tabs or single spaces, but sequences of varying numbers of spaces, which line the output up in columns 8 spaces wide. There are two possibilities I can think of. One is to use cut, but nd out about how to cut at particular character (= byte) positions rather than by elds. The other possibility is to learn enough about awk or sed to pull out just the parts you want.) Richard's notes for those who've made it this far Ian's notes show you the hard way to do it. Let's see the easy way. The nd command does all of the hard work. The following works on GNU/Linux: nd. -type f -printf "%k\t%p\n" sort -nr head -10 [Careful here: there's the `good old' version of nd that you'll nd on Solaris, and there's GNU nd, which has lots more options. You can do 14 of 15 01/05/15 09:57

15 the same thing with the old nd -- check out the -ls option -- but GNU nd has that extra printf and %k stuff that's so useful. Just be aware that using the options that are GNU-nd-specic makes your script less portable. As an exercise, use nd's -ls option (together with the cut command) to solve the problem in a way that works portably across Solaris and GNU/Linux.] What are the advantages and disadvantages of doing it my way? [ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help] Copyright 2005, Ian Barnes & Richard Walker, The Australian National University Version , Monday, 4 April 2005, 14:18: Feedback & Queries to comp2100@cs.anu.edu.au 15 of 15 01/05/15 09:57

COMP2100/2500 Lecture 16: Shell Programming I

COMP2100/2500 Lecture 16: Shell Programming I [ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help] COMP2100/2500 Lecture 16: Shell Programming I Summary

More information

More Scripting Techniques Scripting Process Example Script

More Scripting Techniques Scripting Process Example Script More Scripting Techniques Scripting Process Example Script 1 arguments to scripts positional parameters input using read exit status test program, also known as [ if statements error messages 2 case statement

More information

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

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT Unix as a Platform Exercises + Solutions Course Code: OS 01 UNXPLAT Working with Unix Most if not all of these will require some investigation in the man pages. That's the idea, to get them used to looking

More information

9.2 Linux Essentials Exam Objectives

9.2 Linux Essentials Exam Objectives 9.2 Linux Essentials Exam Objectives This chapter will cover the topics for the following Linux Essentials exam objectives: Topic 3: The Power of the Command Line (weight: 10) 3.3: Turning Commands into

More information

Lab #2 Physics 91SI Spring 2013

Lab #2 Physics 91SI Spring 2013 Lab #2 Physics 91SI Spring 2013 Objective: Some more experience with advanced UNIX concepts, such as redirecting and piping. You will also explore the usefulness of Mercurial version control and how to

More information

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5 Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5 [talking head] This lecture we study theory design and implementation. Programmers have two roles to play here. In one role, they

More information

An Illustrated Guide to Shell Magic: Standard I/O & Redirection

An Illustrated Guide to Shell Magic: Standard I/O & Redirection An Illustrated Guide to Shell Magic: Standard I/O & Redirection Created by Brennen Bearnes Last updated on 2015-03-03 05:15:07 PM EST Guide Contents Guide Contents Overview Input & Output Standard I/O

More information

Lecture 02 The Shell and Shell Scripting

Lecture 02 The Shell and Shell Scripting Lecture 02 The Shell and Shell Scripting In this course, we need to be familiar with the "UNIX shell". We use it, whether bash, csh, tcsh, zsh, or other variants, to start and stop processes, control the

More information

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5 Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5 [talking head] Formal Methods of Software Engineering means the use of mathematics as an aid to writing programs. Before we can

More information

Assignment 3, Due October 4

Assignment 3, Due October 4 Assignment 3, Due October 4 1 Summary This assignment gives you practice with writing shell scripts. Shell scripting is also known as bash programming. Your shell is bash, and when you write a shell script

More information

Introduction to Linux

Introduction to Linux Introduction to Linux The command-line interface A command-line interface (CLI) is a type of interface, that is, a way to interact with a computer. Window systems, punched cards or a bunch of dials, buttons

More information

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between MITOCW Lecture 10A [MUSIC PLAYING] PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between all these high-level languages like Lisp and the query

More information

Shells and Shell Programming

Shells and Shell Programming Shells and Shell Programming 1 Shells A shell is a command line interpreter that is the interface between the user and the OS. The shell: analyzes each command determines what actions are to be performed

More information

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

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 Summer 2010 Department of Computer Science and Engineering York University Toronto June 29, 2010 1 / 36 Table of contents 1 2 3 4 2 / 36 Our goal Our goal is to see how we can use Unix as a tool for developing

More information

CS 2505 Computer Organization I Test 1

CS 2505 Computer Organization I Test 1 Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other computing devices may

More information

Answers to Even-numbered Exercises

Answers to Even-numbered Exercises 11 Answers to Even-numbered Exercises 1. ewrite t propriate actions xists and the user does not have write permission to the le. Verify that the modied script works. 2. The special parameter "$@" is referenced

More information

Shells and Shell Programming

Shells and Shell Programming Shells and Shell Programming Shells A shell is a command line interpreter that is the interface between the user and the OS. The shell: analyzes each command determines what actions are to be performed

More information

Shells & Shell Programming (Part B)

Shells & Shell Programming (Part B) Shells & Shell Programming (Part B) Software Tools EECS2031 Winter 2018 Manos Papagelis Thanks to Karen Reid and Alan J Rosenthal for material in these slides CONTROL STATEMENTS 2 Control Statements Conditional

More information

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12) Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12) Objective: Learn some basic aspects of the UNIX operating system and how to use it. What is UNIX? UNIX is the operating system used by most computers

More information

An Introduction to DOS

An Introduction to DOS An Introduction to DOS Contents 1. Introduction........................................................................................ 1 2. The file system......................................................................................

More information

A shell can be used in one of two ways:

A shell can be used in one of two ways: Shell Scripting 1 A shell can be used in one of two ways: A command interpreter, used interactively A programming language, to write shell scripts (your own custom commands) 2 If we have a set of commands

More information

Do not start the test until instructed to do so!

Do not start the test until instructed to do so! Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other electronic devices

More information

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018 EECS 470 Lab 5 Linux Shell Scripting Department of Electrical Engineering and Computer Science College of Engineering University of Michigan Friday, 1 st February, 2018 (University of Michigan) Lab 5:

More information

CS 2505 Computer Organization I Test 1

CS 2505 Computer Organization I Test 1 Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other computing devices may

More information

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

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT Unix as a Platform Exercises Course Code: OS-01-UNXPLAT Working with Unix 1. Use the on-line manual page to determine the option for cat, which causes nonprintable characters to be displayed. Run the command

More information

COMP 4/6262: Programming UNIX

COMP 4/6262: Programming UNIX COMP 4/6262: Programming UNIX Lecture 12 shells, shell programming: passing arguments, if, debug March 13, 2006 Outline shells shell programming passing arguments (KW Ch.7) exit status if (KW Ch.8) test

More information

Post Experiment Interview Questions

Post Experiment Interview Questions Post Experiment Interview Questions Questions about the Maximum Problem 1. What is this problem statement asking? 2. What is meant by positive integers? 3. What does it mean by the user entering valid

More information

Skill 1: Multiplying Polynomials

Skill 1: Multiplying Polynomials CS103 Spring 2018 Mathematical Prerequisites Although CS103 is primarily a math class, this course does not require any higher math as a prerequisite. The most advanced level of mathematics you'll need

More information

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Scripting Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Adapted from Practical Unix and Programming Hunter College Copyright 2006 2009 Stewart Weiss What a shell

More information

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers A Big Step Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Copyright 2006 2009 Stewart Weiss What a shell really does Here is the scoop on shells. A shell is a program

More information

CS Unix Tools & Scripting

CS Unix Tools & Scripting Cornell University, Spring 2014 1 February 24, 2014 1 Slides evolved from previous versions by Hussam Abu-Libdeh and David Slater A note on awk for (item in array) The order in which items are returned

More information

A Brief Introduction to the Linux Shell for Data Science

A Brief Introduction to the Linux Shell for Data Science A Brief Introduction to the Linux Shell for Data Science Aris Anagnostopoulos 1 Introduction Here we will see a brief introduction of the Linux command line or shell as it is called. Linux is a Unix-like

More information

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

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi Titolo presentazione Piattaforme Software per la Rete sottotitolo BASH Scripting Milano, XX mese 20XX A.A. 2016/17, Alessandro Barenghi Outline 1) Introduction to BASH 2) Helper commands 3) Control Flow

More information

Linked Lists. What is a Linked List?

Linked Lists. What is a Linked List? Linked Lists Along with arrays, linked lists form the basis for pretty much every other data stucture out there. This makes learning and understand linked lists very important. They are also usually the

More information

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

CSC209H Lecture 1. Dan Zingaro. January 7, 2015 CSC209H Lecture 1 Dan Zingaro January 7, 2015 Welcome! Welcome to CSC209 Comments or questions during class? Let me know! Topics: shell and Unix, pipes and filters, C programming, processes, system calls,

More information

The Unix Shell & Shell Scripts

The Unix Shell & Shell Scripts The Unix Shell & Shell Scripts You should do steps 1 to 7 before going to the lab. Use the Linux system you installed in the previous lab. In the lab do step 8, the TA may give you additional exercises

More information

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology. Guide to and Hi everybody! In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology. This guide focuses on two of those symbols: and. These symbols represent concepts

More information

Linux shell scripting Getting started *

Linux shell scripting Getting started * Linux shell scripting Getting started * David Morgan *based on chapter by the same name in Classic Shell Scripting by Robbins and Beebe What s s a script? text file containing commands executed as a unit

More information

Shell Programming Overview

Shell Programming Overview Overview Shell programming is a way of taking several command line instructions that you would use in a Unix command prompt and incorporating them into one program. There are many versions of Unix. Some

More information

Review of Fundamentals

Review of Fundamentals Review of Fundamentals 1 The shell vi General shell review 2 http://teaching.idallen.com/cst8207/14f/notes/120_shell_basics.html The shell is a program that is executed for us automatically when we log

More information

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Lecture 5. Essential skills for bioinformatics: Unix/Linux Lecture 5 Essential skills for bioinformatics: Unix/Linux UNIX DATA TOOLS Text processing with awk We have illustrated two ways awk can come in handy: Filtering data using rules that can combine regular

More information

My Favorite bash Tips and Tricks

My Favorite bash Tips and Tricks 1 of 6 6/18/2006 7:44 PM My Favorite bash Tips and Tricks Prentice Bisbal Abstract Save a lot of typing with these handy bash features you won't find in an old-fashioned UNIX shell. bash, or the Bourne

More information

Useful Unix Commands Cheat Sheet

Useful Unix Commands Cheat Sheet Useful Unix Commands Cheat Sheet The Chinese University of Hong Kong SIGSC Training (Fall 2016) FILE AND DIRECTORY pwd Return path to current directory. ls List directories and files here. ls dir List

More information

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1 Shell Scripting Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 If we have a set of commands that we want to run on a regular basis, we could write a script A script acts as a Linux command,

More information

Slide 1 CS 170 Java Programming 1 Testing Karel

Slide 1 CS 170 Java Programming 1 Testing Karel CS 170 Java Programming 1 Testing Karel Introducing Unit Tests to Karel's World Slide 1 CS 170 Java Programming 1 Testing Karel Hi Everybody. This is the CS 170, Java Programming 1 lecture, Testing Karel.

More information

MITOCW watch?v=4dj1oguwtem

MITOCW watch?v=4dj1oguwtem MITOCW watch?v=4dj1oguwtem PROFESSOR: So it's time to examine uncountable sets. And that's what we're going to do in this segment. So Cantor's question was, are all sets the same size? And he gives a definitive

More information

MITOCW ocw f99-lec07_300k

MITOCW ocw f99-lec07_300k MITOCW ocw-18.06-f99-lec07_300k OK, here's linear algebra lecture seven. I've been talking about vector spaces and specially the null space of a matrix and the column space of a matrix. What's in those

More information

bash, part 3 Chris GauthierDickey

bash, part 3 Chris GauthierDickey bash, part 3 Chris GauthierDickey More redirection As you know, by default we have 3 standard streams: input, output, error How do we redirect more than one stream? This requires an introduction to file

More information

Lab 4: Shell scripting

Lab 4: Shell scripting Lab 4: Shell scripting Comp Sci 1585 Data Structures Lab: Tools Computer Scientists Outline 1 2 3 4 5 6 What is shell scripting good? are the duct tape and bailing wire of computer programming. You can

More information

CSC209. Software Tools and Systems Programming. https://mcs.utm.utoronto.ca/~209

CSC209. Software Tools and Systems Programming. https://mcs.utm.utoronto.ca/~209 CSC209 Software Tools and Systems Programming https://mcs.utm.utoronto.ca/~209 What is this Course About? Software Tools Using them Building them Systems Programming Quirks of C The file system System

More information

MITOCW watch?v=rvrkt-jxvko

MITOCW watch?v=rvrkt-jxvko MITOCW watch?v=rvrkt-jxvko The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

MITOCW watch?v=kz7jjltq9r4

MITOCW watch?v=kz7jjltq9r4 MITOCW watch?v=kz7jjltq9r4 PROFESSOR: We're going to look at the most fundamental of all mathematical data types, namely sets, and let's begin with the definitions. So informally, a set is a collection

More information

MITOCW watch?v=w_-sx4vr53m

MITOCW watch?v=w_-sx4vr53m MITOCW watch?v=w_-sx4vr53m The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To

More information

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

Assignment 2. Summary. Some Important bash Instructions. CSci132 Practical UNIX and Programming Assignment 2, Fall Prof. Assignment 2 Summary The purpose of this assignment is to give you some practice in bash scripting. When you write a bash script, you are really writing a program in the bash programming language. In class

More information

Who am I? I m a python developer who has been working on OpenStack since I currently work for Aptira, who do OpenStack, SDN, and orchestration

Who am I? I m a python developer who has been working on OpenStack since I currently work for Aptira, who do OpenStack, SDN, and orchestration Who am I? I m a python developer who has been working on OpenStack since 2011. I currently work for Aptira, who do OpenStack, SDN, and orchestration consulting. I m here today to help you learn from my

More information

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 Command Lists A command is a sequence of commands separated by the operators ; & && and ; is used to simply execute commands in

More information

Output with printf Input. from a file from a command arguments from the command read

Output with printf Input. from a file from a command arguments from the command read More Scripting 1 Output with printf Input from a file from a command arguments from the command read 2 A script can test whether or not standard input is a terminal [ -t 0 ] What about standard output,

More information

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash CS 25200: Systems Programming Lecture 10: Shell Scripting in Bash Dr. Jef Turkstra 2018 Dr. Jeffrey A. Turkstra 1 Lecture 10 Getting started with Bash Data types Reading and writing Control loops Decision

More information

COMS 6100 Class Notes 3

COMS 6100 Class Notes 3 COMS 6100 Class Notes 3 Daniel Solus September 1, 2016 1 General Remarks The class was split into two main sections. We finished our introduction to Linux commands by reviewing Linux commands I and II

More information

Physics REU Unix Tutorial

Physics REU Unix Tutorial Physics REU Unix Tutorial What is unix? Unix is an operating system. In simple terms, its the set of programs that makes a computer work. It can be broken down into three parts. (1) kernel: The component

More information

GEO 425: SPRING 2012 LAB 9: Introduction to Postgresql and SQL

GEO 425: SPRING 2012 LAB 9: Introduction to Postgresql and SQL GEO 425: SPRING 2012 LAB 9: Introduction to Postgresql and SQL Objectives: This lab is designed to introduce you to Postgresql, a powerful database management system. This exercise covers: 1. Starting

More information

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1 Review of Fundamentals Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 GPL the shell SSH (secure shell) the Course Linux Server RTFM vi general shell review 2 These notes are available on

More information

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one] Data and Computer Security (CMPD414) Lab II Topics: secure login, moving into HOME-directory, navigation on Unix, basic commands for vi, Message Digest This lab exercise is to be submitted at the end of

More information

CSC209. Software Tools and Systems Programming. https://mcs.utm.utoronto.ca/~209

CSC209. Software Tools and Systems Programming. https://mcs.utm.utoronto.ca/~209 CSC209 Software Tools and Systems Programming https://mcs.utm.utoronto.ca/~209 What is this Course About? Software Tools Using them Building them Systems Programming Quirks of C The file system System

More information

Bourne Shell Reference

Bourne Shell Reference > Linux Reviews > Beginners: Learn Linux > Bourne Shell Reference Bourne Shell Reference found at Br. David Carlson, O.S.B. pages, cis.stvincent.edu/carlsond/cs330/unix/bshellref - Converted to txt2tags

More information

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting Slide 1: Cover Welcome to lesson 3 of the db2 on Campus lecture series. Today we're going to talk about tools and scripting, and this is part 1 of 2

More information

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia The goal for this tutorial is to make sure that you understand a few key concepts related to programming, and that you know the basics

More information

Introduction to Unix CHAPTER 6. File Systems. Permissions

Introduction to Unix CHAPTER 6. File Systems. Permissions CHAPTER 6 Introduction to Unix The Unix operating system is an incredibly powerful and complex system that is ideal for running a distributed system such as ours, particularly since we use computers primarily

More information

Lab #12: Shell Scripting

Lab #12: Shell Scripting Lab #12 Page 1 of 11 Lab #12: Shell Scripting Students will familiarize themselves with UNIX shell scripting using basic commands to manipulate the le system. Part A Instructions: This part will be demonstrated

More information

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 Command Lists A command is a sequence of commands separated by the operators ; & && and ; is used to simply execute commands in

More information

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

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More Topic 2: More Shell Skills Plan: about 3 lectures on this topic Sub-topics: 1 quoting 2 shell variables 3 sub-shells 4 simple shell scripts (no ifs or loops yet) 5 bash initialization files 6 I/O redirection

More information

Linux Text Utilities 101 for S/390 Wizards SHARE Session 9220/5522

Linux Text Utilities 101 for S/390 Wizards SHARE Session 9220/5522 Linux Text Utilities 101 for S/390 Wizards SHARE Session 9220/5522 Scott D. Courtney Senior Engineer, Sine Nomine Associates March 7, 2002 http://www.sinenomine.net/ Table of Contents Concepts of the Linux

More information

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version... Contents Note: pay attention to where you are........................................... 1 Note: Plaintext version................................................... 1 Hello World of the Bash shell 2 Accessing

More information

CSC 352, Fall 2015 Assignment 3 Due: Wednesday, September 16 at 23:59:59

CSC 352, Fall 2015 Assignment 3 Due: Wednesday, September 16 at 23:59:59 CSC 352, Fall 2015 Assignment 3 Due: Wednesday, September 16 at 23:59:59 Introduction The a2 write-up started with 3+ pages of various information about assignments. None of that is repeated here, but

More information

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?

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? 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? Tuesday, September 7 CS 375 UNIX System Programming - Lecture

More information

6.001 Notes: Section 15.1

6.001 Notes: Section 15.1 6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define

More information

Getting to grips with Unix and the Linux family

Getting to grips with Unix and the Linux family Getting to grips with Unix and the Linux family David Chiappini, Giulio Pasqualetti, Tommaso Redaelli Torino, International Conference of Physics Students August 10, 2017 According to the booklet At this

More information

CSC148, Lab #4. General rules. Overview. Tracing recursion. Greatest Common Denominator GCD

CSC148, Lab #4. General rules. Overview. Tracing recursion. Greatest Common Denominator GCD CSC148, Lab #4 This document contains the instructions for lab number 4 in CSC148H. To earn your lab mark, you must actively participate in the lab. We mark you in order to ensure a serious attempt at

More information

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options Basic Unix Command The Unix command has the following common pattern command_name options argument(s) Here we are trying to give some of the basic unix command in Unix Information Related man It is used

More information

Introduction to Scripting using bash

Introduction to Scripting using bash Introduction to Scripting using bash Scripting versus Programming (from COMP10120) You may be wondering what the difference is between a script and a program, or between the idea of scripting languages

More information

vi Primer Adapted from:

vi Primer Adapted from: Adapted from: http://courses.knox.edu/cs205/205tutorials/viprimer.html vi Primer This document is designed to introduce you to the standard UNIX screen editor, vi (short for "visual"). Vi can be used to

More information

MITOCW watch?v=zm5mw5nkzjg

MITOCW watch?v=zm5mw5nkzjg MITOCW watch?v=zm5mw5nkzjg The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

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

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable. Bourne Shell Programming Variables - creating and assigning variables Bourne shell use the set and unset to create and assign values to variables or typing the variable name, an equal sign and the value

More information

Introduction to UNIX Shell Exercises

Introduction to UNIX Shell Exercises Introduction to UNIX Shell Exercises Determining Your Shell Open a new window or use an existing window for this exercise. Observe your shell prompt - is it a $ or %? What does this tell you? Find out

More information

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth Subcontractors bc, xargs,, find David 2011-14 bc math help for the shell interactive or programatic can accept its commands from stdin can accept an entire bc program s worth 1 bc math help for shell bc,

More information

CPSC 320 Sample Solution: Physics, Tug-o-War, and Divide-and-Conquer

CPSC 320 Sample Solution: Physics, Tug-o-War, and Divide-and-Conquer CPSC 30 Sample Solution: Physics, Tug-o-War, and Divide-and-Conquer February 4, 017 In tug-o-war, two teams face each other and carefully pull on a well-selected rope (to avoid injury). The team that pulls

More information

MITOCW watch?v=flgjisf3l78

MITOCW watch?v=flgjisf3l78 MITOCW watch?v=flgjisf3l78 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To

More information

bash Scripting Introduction COMP2101 Winter 2019

bash Scripting Introduction COMP2101 Winter 2019 bash Scripting Introduction COMP2101 Winter 2019 Command Lists A command list is a list of one or more commands on a single command line in bash Putting more than one command on a line requires placement

More information

Introduction to Unix

Introduction to Unix Part 2: Looking into a file Introduction to Unix Now we want to see how the files are structured. Let's look into one. more $ more fe_03_06596.txt 0.59 1.92 A-f: hello 1.96 2.97 B-m: (( hello )) 2.95 3.98

More information

Assignment 2, due September 17

Assignment 2, due September 17 Assignment 2, due September 17 The objectives of this assignment are to introduce you to some simple UNIX commands, to give you practice working with directories and the le hierarchy, and to give you practice

More information

mk-convert Contents 1 Converting to minikanren, quasimatically. 08 July 2014

mk-convert Contents 1 Converting to minikanren, quasimatically. 08 July 2014 mk-convert 08 July 2014 Contents 1 Converting to minikanren, quasimatically. 1 1.1 Variations on a Scheme..................... 2 1.2 Racket to minikanren, nally.................. 8 1.3 Back to the beginning......................

More information

Chris' Makefile Tutorial

Chris' Makefile Tutorial Chris' Makefile Tutorial Chris Serson University of Victoria June 26, 2007 Contents: Chapter Page Introduction 2 1 The most basic of Makefiles 3 2 Syntax so far 5 3 Making Makefiles Modular 7 4 Multi-file

More information

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

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University Lecture 33 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 33: Shell Scripting 28-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor: Vla Keselj

More information

The following content is provided under a Creative Commons license. Your support

The following content is provided under a Creative Commons license. Your support MITOCW Lecture 10 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To make a

More information

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently. The Science of Computing I Lesson 4: Introduction to Data Structures Living with Cyber Pillar: Data Structures The need for data structures The algorithms we design to solve problems rarely do so without

More information

The following content is provided under a Creative Commons license. Your support

The following content is provided under a Creative Commons license. Your support MITOCW Lecture 2 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To make a donation

More information

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

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University Unix/Linux Basics 1 Some basics to remember Everything is case sensitive Eg., you can have two different files of the same name but different case in the same folder Console-driven (same as terminal )

More information

More Scripting Todd Kelley CST8207 Todd Kelley 1

More Scripting Todd Kelley CST8207 Todd Kelley 1 More Scripting Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 Arithmetic Output with printf Input from a file from a command CST8177 Todd Kelley 2 A script can test whether or not standard

More information

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used: Linux Tutorial How to read the examples When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used: $ application file.txt

More information

15-122: Principles of Imperative Computation

15-122: Principles of Imperative Computation 15-122: Principles of Imperative Computation Lab 0 Navigating your account in Linux Tom Cortina, Rob Simmons Unlike typical graphical interfaces for operating systems, here you are entering commands directly

More information