Lecture%2002%20-%20Shell%20Scripting - Lecture 02 The Shell...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 02 The Shell and Shell Scripting In this lecture The UNIX shell Simple Shell Scripts Shell variables File System commands, IO commands, IO redirection Command Line Arguments Evaluating Expr in Shell Predicates, operators for testing strings, ints and files If-then-else in Shell The for, while and do loop in Shell Writing Shell scripts Exercises 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 terminal, and to otherwise interact with the system. Many of you have heard of, or made use of "shell scripting", that is the process of providing instructions to shell in a simple, interpreted programming language . To see what shell we are working on, first SSH into unix.andrew.cmu.edu and type echo $SHELL ---- to see the working shell in SSH We will be writing our shell scripts for this particular shell (csh). The shell scripting language does not fit the classic definition of a useful language. It does not have many of the features such as portability, facilities for resource intensive tasks such as recursion or hashing or sorting. It does not have data structures like arrays and hash tables. It does not have facilities for direct access to hardware or good security features. But in many other ways the language of the shell is very powerful -- it has functions, conditionals, loops. It does not support strong data typing -- it is completely untyped (everything is a string). But, the real power of shell program doesn't come from the language itself, but from the diverse library that it can call upon -- any program. Shell programming remains popular because it provides a quick and easy way to integrate command-line tools and filters to solve often complex problems. But it is not a language for all tasks for all people. Simple Shell Scripts The simplest scripts of all are nothing more than lists of commands. Consider the script below. You can type this into a file called: first.sh #!/bin/sh --This line should always be the first line in your script # A simple script
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
who am i date pwd In general, anything after a # is a comment and is ignored by the shell. We see this used both as an entire line and next to each of several lines, where it shows example output. The "#!/bin/sh" tells the shell to invoke /bin/sh to run the script. This is necessary because different users might be using different shells: sh, csh, bash, zcsh, tcsh, &c. And these shells have slightly different languages and build-in features. In order to ensure consistent operation, we want to make sure that the same shell is used to run the script each time. This is achieved by starting the specified shell and passing the script into its standard in. To execute a shell script in csh, we simply type at the command prompt
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/27/2009 for the course CS 123 taught by Professor Bajkzek during the Fall '08 term at Carnegie Mellon.

Page1 / 13

Lecture%2002%20-%20Shell%20Scripting - Lecture 02 The Shell...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online