Top Banner
CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS, COMMANDS, MAN PAGES, TEXT EDITORS, AND COMPILERS Prof. Michael J. Reale Fall 2014
52

CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Apr 17, 2018

Download

Documents

truongnhan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

CS 307: UNIX PROGRAMMING

ENVIRONMENT

INTRODUCTION TO SHELLS,

COMMANDS, MAN PAGES,

TEXT EDITORS, AND COMPILERS

Prof. Michael J. Reale

Fall 2014

Page 2: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

“If you hold a Unix shell to your ear, can you

hear the C?”

-- A venerable yet bad Unix joke

Introduction to Shells

Page 3: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

What is a “Shell”?

When you login to a Unix system with your

username and password, the OS starts a program

called a “shell”

Page 4: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

What is a “Shell”?

Shell – program that allows user to:

Enter commands and run programs

Redirect input and output

Program/script in shell itself

“Shell scripts” – list of commands to shell

Effectively an interpreted programming language with

variables and control flow (i.e., loops, if-else statements, etc.)

Page 5: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Shell / Kernel / User Relationship

Kernel:

Creates shell

Connects user to shell

Provides services and exposes resources to programs

(including the shell)

Disposes of shell when user is done

Page 6: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Shell / Kernel / User Relationship

Page 7: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Lineage of Unix Shells:

Thompson’s Shell

Thompson’s shell

Used until 6th ed. Unix

First Unix shell

Split into the Bourne Shell (sh) and C-Shell (csh)

Page 8: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Lineage of Unix Shells:

Thompson’s Bourne (sh)

Bourne shell (sh)

Default in 7th ed. Unix

Written by Stephen Bourne at Bell Labs to replace original

shell and to add new features (like scripting)

Still included in many distributions of Unix/Linux (/bin/sh)

Pretty primitive by today’s standards

Page 9: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Lineage of Unix Shells:

Bourne (sh) korn

Korn (ksh)

Written by David Korn at Bell Labs

Heavy focus on shell programming

Improvement over Bourne (sh) and adds some features from

C-shell (csh)

Popular among commercial users

Adopted in future editions of AT&T Unix (8-10th

editions)

Was proprietary until year 2000

http://www.kornshell.com/

Page 10: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Lineage of Unix Shells:

Bourne (sh) Bourne Again (bash)

Bourne Again (bash)

Originally written by Brian Fox at Free Software

Foundation (FSF)

Part of GNU Toolkit

Free replacement for Bash (sh), hence the pun

Community supported

http://www.gnu.org/software/bash/

Page 11: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Lineage of Unix Shells:

Thompson’s C-Shell (csh)

C-Shell (csh)

Written by Bill Joy as part of BSD

Based on C Programming language.

Scripting commands are based on C statements.

Under BSD License, so couldn't distribute freely

Page 12: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Lineage of Unix Shells:

C-Shell (csh) TCSH

TCSH (tcsh)

Enhancement of the C-shell while being free from

licenses

In public domain for academic users

Page 13: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Lineage of Unix Shells: Family Tree

http://web.cs.sunyit.edu/~merantn/cs307/img/shells.jpg

Page 14: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Shells: Who Uses What?

Split by users:

Bash Linux

Tcsh BSD

Korn Commercial users

Depends on primary use:

Interactive use: bash or tcsh

Scripting: Bourne (portability/compatibility) or Bash (extended features)

Page 15: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Shells: What shells do I have?

See available shells on your system:

cat /etc/shells

Can switch to a different shell by calling it as a program:

/bin/bash

You can write your own shell (it’s a garden-variety program).

Page 16: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Shell Customization

Can customize your shell (what programs start, what

variables are set, etc.)

Usually a default file in the system, but also one in

your home directory

Page 17: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Bourne (sh) Customization

Bourne (sh) uses ~/.profile

May also have system-wide file /etc/profile, which is

executed before local .profile

Note: sometimes sh actually linked to bash

http://stefaanlippens.net/bashrc_and_others

Page 18: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Bash Customization

For login bash shells:

Reads and executes /etc/profile (system-wide)

Looks for one of these (in this order) and reads first one it

finds:

~/.bash_profile

~/.bash_login

~/.profile

Reads and executes ~/.bash_logout on logout (if it exists)

https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

Page 19: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Bash Customization

For interactive, non-login bash shells:

~/.bashrc

Executed when you call bash manually (/bin/bash) or

open a new terminal window (xterm)

Page 20: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

C-Shell Family Customization

C-shell and its descendants run the following:

.cshrc Shell environment

Run on both interactive and non-interactive shells

.login Run on login

Contains commands and variables

.logout Script that runs on logout

Page 21: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Basic Command Syntax

Page 22: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

How to enter commands

To run a command, type it at the shell prompt and

hit Enter:

Example: ls

Lists the contents of the current directory

Page 23: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Command Syntax

Commands usually have the following general

syntax:

Command_name options arguments

(Whitespace separates each part)

Page 24: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Command Syntax

Command_name = the command/program we wish to execute

Options = determine how the program/command is run

Example: ls -a Lists ALL the contents of current directory, including hidden files (i.e., files with

a period before their names)

Arguments = provide additional information to the command, such as filenames

Example: ls /etc List the contents of the /etc directory (instead of the current one)

Page 25: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Options and Arguments

Options may be combined:

Example: ls -al

List ALL the contents AND list it in the “long” format (-l)

Options may also have arguments (known as “option arguments”)

Example: cal –B 2

Usually, cal just shows the current calendar month

-B 2 shows the current month AND the two months before it

“2” is the option argument for option “-B”

http://tldp.org/LDP/abs/html/special-chars.html

Page 26: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Long Options

Long options use a double-dash --

Example: less --version

Prints the version information for the program less

Page 27: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Previous Commands

Up arrow on keyboard access previous

commands

Type first two letters then UP search command

history

Page 28: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Grouping and Stopping Commands

Group commands with a semicolon ;

Example: cd ; ls

Go to home directory and then list contents

Stop commands with CTRL-C

Interrupt; break out of whatever you are doing

Page 29: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Common Signals

Sequence Action

^A Move cursor to start of line

^E Move cursor to end of line

^C Interrupt. Break out of what you're doing

^D eof. End of transmission, end of file

^U Erase entire input line

^L Clear screen above current line

^TDisplay status of foreground process

(FreeBSD)

Can use Control-letter to send signals to the terminal

Typically abbreviated with ^

Example: CTRL-C or ^C

Page 30: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

COMMAND KATA 0

Page 31: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Command Kata 0: Preparation

First, go to ~/cs307

Page 32: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Command Kata 0

Where am I? pwd

Create directory kata mkdir kata

What is in my current location? ls

Go to directory kata cd kata

Where am I? pwd

Go to parent directory cd ..

Where am I? pwd

Remove directory kata rmdir kata

What is in my current location? ls

Page 33: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

R.T.F.M. = “READ THE (Fine) MANUAL!!!”

MAN PAGES (DOCUMENTATION)

Page 34: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

MAN Pages

MAN pages

The MANual pages

Documentation for the Unix system and programs

Usage: man [section] command

Example: man ls

Brings up documentation for ls command

See Hahn chapter 9

Page 35: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

How to navigate in MAN pages

Navigation: Hahn page 195

Moving:

Down page space, f

Up page b

Up/down line up/down arrows

Search:

Down /

Up ?

Next match n

Page 36: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

How to navigate in MAN pages

Jump to:

Top of page g

Bottom of page G

Quit q

Help h

Page 37: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

MAN Page Organization

Synopsis = Overview of the command, listing options and requirements

Optional items are contained within [brackets]

Description = Description of actions performed by the command and detail information about all of the supported options

Examples = Examples of common usage

See Also = Other man pages to read for related information

Page 38: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

MAN Search

man -k keyword - search the man pages

Page 39: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

MAN Sections

Manual sections:

1 - Commands

2 - System Calls

3 - Library Functions

4 - Special Files

5 - File Formats

6 - Games

7 - Misc Info

8 - System administration

Page 40: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

COMMAND KATA 1

Page 41: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Command Kata 1: Preparation

First, go to ~/cs307

Create directory kata1 and go to it:

mkdir kata1; cd kata1

Page 42: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Command Kata 1

Create file abc touch abc

What is in this folder? ls

Copy abc to def cp abc def

What is in this folder? ls

Rename abc to ghi mv abc ghi

What is in this folder? ls

Remove def and ghi files rm def ghi

What is in this folder? ls

Page 43: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Text Editors

Page 44: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Common Text Editors

pico

Simple text editor written by the University of

Washington to accompany their mail client, pine.

nano

A GNU pico clone with enhancements.

Page 45: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Common Text Editors

vi

Original UNIX text editor

Not as easy to use as pico/nano, but much more powerful.

More information: Run vimtutor

Back page of UNIX Command summary handout (see website)

UNIX in a Nutshell Book

vim

vi Improved

Page 46: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Basic Compiler Usage

Page 47: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

GNU C/C++ Compiler

gcc & g++

gcc compiling C code

g++ compile C++ code

Source code file extensions must either be .c or .cpp

Page 48: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

gcc/g++ Usage Example

Usage:

gcc [ -o outfile ] source

Example:

gcc -o name_of_executable source.c

OR

g++ -o name_of_executable source.cpp

WHERE

name_of_executable = executable file to create after compiling your source code, instead of using the default a.out

Page 49: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Basic Compiler Usage:

Hello World with gcc/g++

Let do a simple exercise to:

Compile a simple program in C/C++

Get more practice editing text files

Page 50: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Basic Compiler Usage:

Hello World with gcc/g++

Make a folder called “hello” in your home directory and go there:

mkdir ~/cs307/hello; cd hello

Open vi:

vi hello.cpp

(This will create hello.cpp if it does not exist)

Press ‘i’ to enter insert mode

Page 51: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Basic Compiler Usage:

Hello World with gcc/g++

Type:

#include <iostream> using namespace std; int main() {

cout << "Hello World!" << endl; return 0;

}

Hit “ESC” a couple of times, then type SHIFT+Z twice (save and exit)

Page 52: CS 307: UNIX Programming Environment Course Introweb.cs.sunyit.edu/.../cs307/slides/CS_307_Fall_2014_0… ·  · 2014-08-28CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS,

Basic Compiler Usage:

Hello World with gcc/g++

Compile the source file:

g++ -o hello hello.cpp

Run program!

./hello