CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2 – Processes, Programs, the Shell (& emacs)

Slides:



Advertisements
Similar presentations
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 4: More Command Line Interface (CLI) Chapter 7: The Linux Shell By Fred R. McClurg Linux.
Advertisements

A Guide to Unix Using Linux Fourth Edition
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
Linux+ Guide to Linux Certification, Second Edition
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Linux+ Guide to Linux Certification, Second Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Introduction to Linux and Shell Scripting Jacob Chan.
Introduction to Linux Workshop February Introduction Rob Lane & The HPC Support Team Research Computing Services CUIT.
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CS 2061 Shells Also known as: Unix Command Interpreter.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Linux+ Guide to Linux Certification Chapter Four Exploring Linux Filesystems.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
CS 390 Unix Programming Summer Unix Programming - CS 3902 Course Details Online Information Please check.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Lesson 2 1.Commands 2.Filename Substitution 3.I/O Redirection 4.Command Grouping 5.Shell Responisibilites Review of the Basics.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
40 Years and Still Rocking the Terminal!
Unix Shell Environments February 23rd, 2004 Class Meeting 6.
UNIX shell environments CS 2204 Class meeting 6 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 4 – Shell Variables, More Shell Scripts.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2014 Lecture 3 – I/O Redirection, Shell Scripts.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Administration Working with the BASH Shell.
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Introduction to the bash Shell (Bourne-Again SHell)
Using Linux Kaya Oğuz Room: 310.
SUSE Linux Enterprise Desktop Administration
slides created by Marty Stepp, modified by Josh Goodwin
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
The Linux Operating System
CSE 303 Concepts and Tools for Software Development
Basic UNIX OLC Training.
Shells, Help, and Paths.
Shell Programming.
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Introduction to Bash Programming, part 3
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2 – Processes, Programs, the Shell (& emacs)

News > 20 new students registered since Mon. Welcome!! HW0/klaatu/vmware: getting closer –Klaatu and vmware distribution accounts (not the same thing) for most students should be ok now – will add newest students shortly. –Turn in hw0 when done. If your joined the class late we’ll sort out late day issues later if needed. “do your own work” != “don’t talk to anyone” !!! Please use discussion board, or mail to cse374-staff, not mail to individual TAs/instructor. Office hour etiquette: no camping please. Goal is to help you get unstuck so you can make progress, not enough capacity for a study group/work session. 2

Where we are It’s like we started over using the computer from scratch All we can do is run dinky programs at the command-line But we are learning a model (a system is files, processes, and users) and a powerful way to control it (the shell) If we get the model right, hopefully we can learn lots of details quickly Today: –The rest of the model briefly: Processes and Users –More programs (ps, chmod, kill,... ) –Special shell characters (*, ~,... ) –Text editing (particularly emacs) 3

Users There is one file-system, one operating system, one or more CPUs, and multiple users whoami ls -l and chmod (permissions), quota (limits) –Make your homework unreadable by others! /etc/passwd (or equivalent) guides the login program: –Correct username and password –Home directory –Which shell to open (pass it the home directory) –The shell then takes over, with startup scripts (e.g.,.bash_profile,.bashrc). (ls -a) One “superuser” a.k.a. root. (Change passwords, halt machine, change system directories, add/remove user accounts,... ) 4

Programs & the Shell A program is a file that can be executed Almost all system commands are programs The shell is itself a program –Reads lines you type in & carries them out –Normally finds the named program and runs it A few commands are shell “built-ins” that the shell executes itself because they change the state of the shell. Obvious example: cd –After the named program runs it exits and the shell reads the next command –More to this story to come… 5

Processes A running program is called a process. An application (e.g., emacs), may be running as 0, 1, or 57 processes at any time The shell runs a program by “launching a process” waiting for it to finish, and giving you your prompt back. –What you want for ls, but not for emacs. –&, jobs, fg, bg, kill — job control –ps, top Each process has private memory and I/O streams A running shell is just a process that kills itself when interpreting the exit command (Apologies for aggressive vocabulary, but we’re stuck with it for now.) 6

Standard I/O streams Every process has 3 standard streams: stdin (input), stdout (output), stderr (error messages) Default is keyboard (stdin), terminal window (stdout, stderr) Default behavior is to read from stdin, write normal output to stdout, write diagnostic output to stderr –Many programs accept command-line arguments naming files to read –If not supplied, just read stdin –Also ways to redirect stdin, stdout, stderr. Later… 7

That’s most of a running system File-system, users, processes The operating system manages these Processes can do I/O, change files, launch other processes. Other things: Input/Output devices (monitor, keyboard, network) GUIs don’t change any of this, but they do hide it a bit Now: Back to the shell... 8

The shell so far So far, our view of the shell is the barest minimum: –builtins affect subsequent interpretations –New builtin: source –Otherwise, the first “word” is a program run with the other “words” passed as arguments Programs interpret arguments arbitrarily, but conventions exist 9

Complicating the shell But you want (and bash has) so much more: –Filename metacharacters –Pipes and Redirections (redirecting I/O from and to files) –Command-line editing and history access –Shell and environment variables –Programming constructs (ifs, loops, arrays, expressions, … ) All together, a very powerful feature set, but awfully inelegant 10

Filename metacharacters - globbing Much happens to a command-line to turn it into a “call program with arguments” (or “invoke builtin”) Certain characters can expand into (potentially) multiple filenames: –~foo – home directory of user foo –~ – current user’s home directory (same as ~$user or ‘whoami‘). –* (by itself) – all files in current directory –* – match 0 or more filename characters –? – match 1 filename character –[abc], [a-E], [^a],...more matching Remember, this is done by the shell before the program sees the resulting arguments 11

Filename metacharacters: why Manually, you use them all the time to save typing. In scripts, you use them for flexibility. Example: You do not know what files will be in a directory, but you can still do: cat * (though a better script would skip directories) But what if it’s not what you want? Use quoting ("*" or '*') or escaping (\*) The rules on what needs escaping where are very arcane A way to experiment: echo –echo args... copies its arguments to standard output after expanding metacharacters 12

History The history builtin The ! special character –!!, !n, !abc,... –Can add, substitute, etc. This is really for fast manual use; not so useful in scripts 13

Aliases Idea: Define a new command that expands to something else (not a full script) Shell builtin command: alias repeat=echo alias dir=ls alias hello="echo hello" alias rm="rm -i" % for cautious users alias % list existing aliases Often put in a file read by source or in a startup file read automatically Example: your.bashrc – feel free to change 14

Bash startup files Bash reads (sources) specific files when it starts up. Put commands here that you want to execute every time you run bash Which file gets read depends on whether bash is starting as a “login shell” or not –Login shell: ~/.bash_profile (or others – see bash documentation) –Non-login shell: ~/.bashrc (or others if not found) Suggestion: Include the following in your.bash_profile file so the commands in.bashrc will execute regardless of how the shell starts up if [ -f ~/.bashrc ]; then source ~/.bashrc; fi 15

Where we are Features of the bash “language”: 1.builtins 2.program execution 3.filename expansion (Pocket Guide 23-25, 1 st ed ) 4.history & aliases 5.command-line editing 6.shell and environment variables 7.programming constructs But file editing is too useful to put off... so a detour to emacs (which shares some editing commands with bash) 16

What is emacs? A programmable, extensible text editor, with lots of goodies for programmers Not a full-blown IDE but much “heavier weight” than vi Top-6 commands: C-g C-x C-f C-x C-s, C-x C-w C-x C-c C-x b C-k, C-w, C-y,... Take the emacs tutorial to get the hang of the basics Everyone should know this at least a little – emacs editing shortcuts are common in other Linux programs Customizable with elisp (starting with your.emacs) 17

Command-line editing Lots of control-characters for moving around and editing the command-line. (Pocket Guide page 28, emacs-help, and Bash reference manual Sec. 8.4.) They make no sense in scripts Gotcha: C-s is a strange one (stops displaying output until C-q, but input does get executed) Good news: many of the control characters have the same meaning in emacs (and bash has a vi “mode” too) 18

Summary As promised, we are flying through this stuff! Your computing environment has files, processes, users, a shell, and programs (including emacs) Lots of small programs for files, permissions, manuals, etc. The shell has strange rules for interpreting command-lines. So far: –Filename expansion –History expansion The shell has lots of ways to customize/automate. So far: –alias and source –run (i.e., automatically source).bash_profile or.bashrc when shell starts Next: I/O Redirection & stream details, Shell Programming 19