comp 2103 programming 3 part 5
play

COMP 2103 Programming 3 Part 5 Jim Diamond CAR 409 Jodrey School - PowerPoint PPT Presentation

COMP 2103 Programming 3 Part 5 Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University 226 Shell Programming Up until now, we have only discussed using the shell (command interpreter) interactively i.e., every


  1. COMP 2103 — Programming 3 Part 5 Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University

  2. 226 Shell Programming • Up until now, we have only discussed using the shell (command interpreter) interactively – i.e., every shell command you typed was executed immediately • The rest of the course is concerned with writing programs for the shell • You should review slides 8 through 25 from slides-pt1.pdf before next class Jim Diamond, Jodrey School of Computer Science, Acadia University

  3. 227 Shell Wars • A long time ago, in an Empire lab far, far away, there was the original Unix command interpreter (shell), known as sh – it was both powerful and minimal – powerful in what it could do – minimal in terms of bells and whistles e.g., no (real) editing of the line you were typing, no history, no aliases ( q.v. ), . . . • People started wanting more features – people had different opinions of what they wanted to add, how things should be done, and so on – one person thought the syntax should be more C-like • A proliferation of shells occurred ( csh , ksh , tcsh , bash , zsh , . . . ) • A “standard” (IEEE Std 1003.1, 2013 Edition, “POSIX”) was declared, which defines a set of features and behaviours see http://pubs.opengroup.org/onlinepubs/9699919799/ – utilities/toc.html for a free “equivalent” copy • We will concentrate on “POSIX-compliant” shell features Jim Diamond, Jodrey School of Computer Science, Acadia University

  4. 228 Shell Meta-characters • The shell provides some convenient expressions for naming files: “ /etc/rc* ” matches all files in /etc starting with “ rc ” – “ A8P1.* ” matches all files (in current dir) starting with “ A8P1. ” – “ * ” matches all (non-hidden) file names in the current directory – “ ???? ” matches all files with exactly four characters in their names – caveat: file names whose first char is “ . ” are not matched – unless the pattern starts with a “ . ” (e.g., “ .??* ”) “ [qaz] ” matches exactly one of “ q ”, “ a ” or “ z ” – • For example: cat * cat out all files in this dir ls -l *.c list all .c file in this dir ls -l a9p?.[ch] list all A9 .c and .h files • To tell the shell to NOT to (try to) match filenames to “ * ”, “ ? ” or “ [...] ” in a command line precede the meta-char with “ \ ”: “ echo \* ” – echoes an asterisk echo "?" – enclose it in single or double quotes: Jim Diamond, Jodrey School of Computer Science, Acadia University

  5. 229 Shell Meta-characters: 2 The shell † uses “ { ” and “ } ” as a short-hand notation as follows: • – $ echo AA{a,b,c}ZZ AAaZZ AAbZZ AAcZZ $ echo before AA{alice,bob,chu}ZZ after – before AAaliceZZ AAbobZZ AAchuZZ after • In other words, for each comma-separated string inside the braces, the shell generates a token using that string and the rest of the token outside the braces • ls /bin/[ab]* is the same as ls /bin/{a,b}* Note: but ls /bin/{red,blue}* is not easy with [...] – • Some GEQs: – Q: how can I get a space in one of the strings? Q: if allowed, what does A{1,2,3}{b,c}Z generate? – † at least bash , tcsh and zsh , but not all shells Jim Diamond, Jodrey School of Computer Science, Acadia University

  6. 230 Shell Scripts • A shell script is just a text file with lines that can be interpreted by the shell • A shell script starts with a line telling the operating system which shell to use to run the commands; for example, #! /bin/sh says to use /bin/sh to run this shell script on some Linux distros, /bin/sh is the same as /bin/bash – on other Linux distros, /bin/sh is some faster shell (e.g., dash is – used in Ubuntu) (optimized for running scripts, not user interaction) shells are typically “installed” in the /bin directory – • Shell script lines whose first non-white char is ‘ # ’ are comments • Sample shell script: #! /bin/sh # This is a trivial but valid shell script printf "The time is now " date Jim Diamond, Jodrey School of Computer Science, Acadia University

  7. 231 Shell Variables: 1 • Like most programming languages, the shell has variables which can hold values – the values are strings A value might look like a number, e.g., 123, but it is still a string • Variables can be declared, but don’t need to be • Variables are assigned using an equal sign, like this: $ my_pet=dog $ weightInKgs=50 – note: you can NOT use spaces around the “ = ” • You access the value of a variable (in most places!) with a “ $ ”: $ echo "My pet is a $my_pet and it weighs $weightInKgs Kgs" My pet is a dog and it weighs 50 Kgs • Variables are not expanded inside single-quote strings: $ echo ’ My pet is a $my_pet and it weighs $weightInKgs ’ My pet is a $my_pet and it weighs $weightInKgs Jim Diamond, Jodrey School of Computer Science, Acadia University

  8. 232 Shell Programming Constructs: ✐❢ (1) • This (and other) programming constructs can be typed in interactively, but are more often used in shell scripts if grep root /etc/passwd >/dev/null 2>&1 toss stdout, stderr then echo root is in /etc/passwd <other commands could go here> else echo root is NOT in /etc/passwd <other commands could go here too> fi • REALLY REALLY REALLY IMPORTANT DIFFERENCE BETWEEN SHELL AND C/Java/Python: in a shell if statement, the “condition” is not a Boolean expression – the “condition” is the return code of a program grep in the above example if the program returns “success”, the if statement executes the – “then” part of the statement – otherwise, the if statement executes the else part of the statement, if there is one Recall that Java’s main() is public static void : thus making Java programs much less useful in shell scripts Jim Diamond, Jodrey School of Computer Science, Acadia University

  9. 233 Shell Programming Constructs: ✐❢ (2) if grep root /etc/passwd >/dev/null 2>&1 toss stdout, stderr then echo root is in /etc/passwd <other commands could go here> else echo root is NOT in /etc/passwd <other commands could go here too> fi • The then clause statements are executed if grep returns success (e.g., EXIT_SUCCESS from a C program), otherwise the else clause statements are executed • The else clause is optional, just like in C or Java or Python • “ >/dev/null ” means “throw stdout away” “ 2>&1 ” means “send stderr wherever stdout is going right now ” – • Note that keywords are used to delimit the clauses, rather than braces Jim Diamond, Jodrey School of Computer Science, Acadia University

  10. 234 Shell Programming Constructs: ✇❤✐❧❡ • The following construct first runs “ some-program ” while some-program args do <some statements> done if some-program exits successfully, <some statements> are – executed, and the process is repeated – if some-program returns a failure code, the while/do/done block is skipped • You can use break to exit a while loop as in other programming languages, typically this would be in an if – statement • Similarly, you can use continue to finish this iteration and go back to the while condition at the top of the loop Jim Diamond, Jodrey School of Computer Science, Acadia University

  11. 235 Shell Programming Constructs: ❢♦r • The following construct • sets the variable i to arg1 , runs <some statements> , • sets i to arg2 , runs <some statements> , • and so on – typically, $i is used in <some statements> for i in arg1 arg2 arg3 ... do <some statements> done • A common usage is to iterate over all files in the current directory for f in * do ... done • Another common usage is to iterate over all command-line arguments for f in "$@" do ... done Jim Diamond, Jodrey School of Computer Science, Acadia University

  12. 236 Shell Programming Constructs: ✉♥t✐❧ , t❡st and the Amazing ‘ • until is like while , but repeats as long as the return code indicates failure, rather than success until test ‘ date +%p ‘ = PM do echo It is still too early to get up sleep 60 done • date +%p outputs AM or PM , according to the time of day • test is a very versatile command for testing all sorts of conditions see its man page for details – • Enclosing a command inside single back-quotes makes the shell – run that command, and then – substitute that command’s output into the command line • So running that until statement before noon would be equivalent to running until test AM = PM Jim Diamond, Jodrey School of Computer Science, Acadia University

  13. 237 Addendum on ‘ • The ‘ ... ‘ construct works in a wide variety of shells • However, in POSIX shells the $( ... ) construct can be used as well so instead of until test ‘ date +%p ‘ = PM – we can write until test $(date +%p) = PM • The ‘ ... ‘ construct works in csh and tcsh , but $( ... ) does not • You have to decide whether you prefer – extra portability, or not getting abuse from bash fanatics – • It is arguably easier to nest using the $( ... ) construct consider cmd1 ... $(cmd2 ... $(cmd3 ...) ...) ... – cmd1 ... ‘ cmd2 ... \ ‘ cmd3 ...\ ‘ ... ‘ ... – vs. • ‘ ... ‘ and $( ... ) handle \ differently; in bash try echo $(echo \\\\) ‘ echo \\\\ ‘ Jim Diamond, Jodrey School of Computer Science, Acadia University

Recommend


More recommend