Lecture2 - Shell Scripting Week 2 The Shell and OS The...

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

View Full Document Right Arrow Icon
Shell Scripting Week 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
The Shell and OS • The shell is the user’s interface to the OS • From it you run programs.
Background image of page 2
Scripting Languages Versus Compiled Languages • Compiled Languages – Ex: C/C++, Java – Programs are translated from their original source code into object code that is executed by hardware – Efficient – Work at low level, dealing with bytes, integers, floating points, etc • Scripting languages – Interpreted – Interpreter reads program, translates it into internal form, and execute programs
Background image of page 3

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

View Full DocumentRight Arrow Icon
Why Use a Shell Script • Simplicity • Portability • Ease of development
Background image of page 4
Example $ who george pts/2 Dec 31 16:39 (valley-forge.example.com) betsy pts/3 Dec 27 11:07 (flags-r-us.example.com) benjamin dtlocal Dec 27 17:55 (kites.example.com) jhancock pts/5 Dec 27 17:55 (:32) Camus pts/6 Dec 31 16:22 tolstoy pts/14 Jan 2 06:42 $ who | wc -l Count users 6
Background image of page 5

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

View Full DocumentRight Arrow Icon
Self-Contained Scripts: The #! First Line When the shell runs a program, it asks the kernel to start a new process and run the given program in that process. It knows how to do this for compiled programs but for a script, the kernel will fail, returning a “not executable format file” error so it’ll start a new copy of /bin/sh (the standard shell) to run the program. But if there is more than one shell installed on the system, we need a way to tell the kernel which shell to use for a script #! /bin/csh –f #! /bin/awk –f #! /bin/sh
Background image of page 6
Basic Shell Constructs • Shell recognizes three fundamental kinds of commands – Built-in commands: Commands that the shell itself executes – Shell functions: Self-contained chunks of code, written in shell language – External commands
Background image of page 7

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

View Full DocumentRight Arrow Icon
Variables • Start with a letter or underscore and may contain any number of following letters, digits, or underscores • Hold string variables $ myvar=this_is_a_long_string_that_does_not_mean_much Assign a value $ echo $myvar Print the value this_is_a_long_string_that_does_not_mean_much first=isaac middle=bashevis last=singer Multiple assignments allowed on one line fullname="isaac bashevis singer" Use quotes for whitespace in value oldname=$fullname Quotes not needed to preserve spaces in value fullname="$first $middle $last" Double quotes required here, for concatenating
Background image of page 8
Simple Output with echo $ echo Now is the time for all good men Now is the time for all good men $ echo to come to the aid of their country. to come to the aid of their country. There is also fancier output with printf, which can refer to its man page for
Background image of page 9

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

View Full DocumentRight Arrow Icon
•Most programs read from stdin •Write to stdout •Send error messages to stderr $ cat With no arguments, read standard input, write standard output now is the time Typed by the user now is the time Echoed back by cat for all good men for all good men to come to the aid of their country to come to the aid of their country ^D Ctrl-D, End of file
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/16/2011 for the course CS35L 187105201 taught by Professor Pope during the Spring '10 term at UCLA.

Page1 / 54

Lecture2 - Shell Scripting Week 2 The Shell and OS The...

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

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