+ All Categories
Home > Documents > CS 307: UNIX Programming Environment Course...

CS 307: UNIX Programming Environment Course...

Date post: 17-Apr-2018
Category:
Upload: truongnhan
View: 221 times
Download: 0 times
Share this document with a friend
52
CS 307: UNIX PROGRAMMING ENVIRONMENT INTRODUCTION TO SHELLS, COMMANDS, MAN PAGES, TEXT EDITORS, AND COMPILERS Prof. Michael J. Reale Fall 2014
Transcript

CS 307: UNIX PROGRAMMING

ENVIRONMENT

INTRODUCTION TO SHELLS,

COMMANDS, MAN PAGES,

TEXT EDITORS, AND COMPILERS

Prof. Michael J. Reale

Fall 2014

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

hear the C?”

-- A venerable yet bad Unix joke

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”

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.)

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

Shell / Kernel / User Relationship

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)

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

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/

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/

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

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

Lineage of Unix Shells: Family Tree

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

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)

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).

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

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

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

Bash Customization

For interactive, non-login bash shells:

~/.bashrc

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

open a new terminal window (xterm)

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

Basic Command Syntax

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

Command Syntax

Commands usually have the following general

syntax:

Command_name options arguments

(Whitespace separates each part)

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)

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

Long Options

Long options use a double-dash --

Example: less --version

Prints the version information for the program less

Previous Commands

Up arrow on keyboard access previous

commands

Type first two letters then UP search command

history

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

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

COMMAND KATA 0

Command Kata 0: Preparation

First, go to ~/cs307

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

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

MAN PAGES (DOCUMENTATION)

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

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

How to navigate in MAN pages

Jump to:

Top of page g

Bottom of page G

Quit q

Help h

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

MAN Search

man -k keyword - search the man pages

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

COMMAND KATA 1

Command Kata 1: Preparation

First, go to ~/cs307

Create directory kata1 and go to it:

mkdir kata1; cd kata1

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

Text Editors

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.

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

Basic Compiler Usage

GNU C/C++ Compiler

gcc & g++

gcc compiling C code

g++ compile C++ code

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

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

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

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

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)

Basic Compiler Usage:

Hello World with gcc/g++

Compile the source file:

g++ -o hello hello.cpp

Run program!

./hello


Recommended