1 UNIX System Programming Introduction. 2 Outline UNIX History UNIX Today? UNIX Processes and the Login Process Shells: Command Processing, Running Programs.

Slides:



Advertisements
Similar presentations
CSCI 1730 April 1 st, Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.
Advertisements

Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
1 Introduction to UNIX Ke Liu
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
Chapter Seven Unix Shell Environments1 System Programming UNIX Shell Environments.
The Unix Shell. Operating System shell The shell is a command interpreter It forms the interface between a user and the operating system When you log.
Guide To UNIX Using Linux Third Edition
Introduction to Linux/UNIX. History UNIX beginnings in 1969 (Linus Torvalds is born!) AT & T Bell Laboratories (Ken Thompson & Dennis Richie) Working.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
July 10, 2003Serguei A. Mokhov, 1 UNIX Commands COMP 444/5201 Revision 1.4 January 25, 2005.
Linux Commands LINUX COMMANDS.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Using Macs and Unix Nancy Griffeth January 6, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis of Complex.
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to UNIX System --- Cosc513 Presentation n Instructor: Morteza Anvari n Author: Yonghong Pan n ID#: n Date: Jan.29, 2001.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Session 2 Wharton Summer Tech Camp Basic Unix. Agenda Cover basic UNIX commands and useful functions.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
1 Operating Systems and Using Linux Topics What is an Operating System? Linux Overview Frequently Used Linux Commands Some content in this lecture added.
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.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
ITR3 lecture 6: intoduction to UNIX Thomas Krichel
Chapter Four I/O Redirection1 System Programming Shell Operators.
UNIX Commands COMP 1090 Introduction to Unix Fall
40 Years and Still Rocking the Terminal!
Unix Shell Environments February 23rd, 2004 Class Meeting 6.
Linux+ Guide to Linux Certification, Third Edition
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.
Introduction to Programming Using C An Introduction to Operating Systems.
Λειτουργικά Συστήματα – Lab2 Γιάννης Πετράκης. Directory Navigation and Control  The Unix file system is set up like a tree branching out from the root.
Linux Commands C151 Multi-User Operating Systems.
1 Lecture 2 Working with Files and Directories COP 3353 Introduction to UNIX.
UNIX filesystem CS 2204 Class meeting 2 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
A Brief Overview of Unix Brandon Bohrer. Topics What is Unix? – Quick introduction Documentation – Where to get it, how to use it Text Editors – Know.
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Learning Unix/Linux Based on slides from: Eric Bishop.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
Some Linux Commands.
Basic UNIX OLC Training.
An Introduction to UNIX System --- Cosc513 Presentation
Introduction to Linux Week 0 - Thursday.
Introduction to Linux/UNIX
Chapter Four UNIX File Processing.
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Unix Shell Environments
Linux Commands LINUX COMMANDS.
A shell is a user interface.
January 26th, 2004 Class Meeting 2
LPI Linux Certification
Presentation transcript:

1 UNIX System Programming Introduction

2 Outline UNIX History UNIX Today? UNIX Processes and the Login Process Shells: Command Processing, Running Programs The File The Process System Calls and Library Routines

3 UNIX History Developed in the late 1960s and 1970s at Bell Labs UNICS – a pun MULTICS (Multiplexed Information and Computer Service) which was supposed to support 1000 on line users but only handled a few (barely 3). (MULTI- UNiplexed). Thomson writes first version of UNICS in assembler for a PDF-7 in one MONTH which contains a new type of file system: kernel, shell, editor and the assembler (one week) Thomson writes interpretive B based on BCPL -- Ritchie improves on B and called it “C” 1972 UNIX is re-written in C to facilitate porting

4 UNIX History (cont) 1973 UNIX philosophy developed: –Write programs that do one thing and do it well –Write programs that work together –Write programs that handle text streams, because that is the universal interface

5 UNIX Today Supports many users running many programs at the same time, all sharing the same computer system Information Sharing Geared towards facilitating the job of creating new programs Sun: SunOS, Solaris; GNU: Linux; SGI: IRIX; Free BSD; Hewlett Packard: HP-UX; Apple: OS X (Darwin)

6 User UNIX Interface: SHELL Provides command line as an interface between the user and the system Is simply a program that starts automatically when you login Uses a command language –Allows programming (shell scripting) within the shell environment –Uses variables, loops, conditionals, etc. –Next week

7 Various UNIX shells sh (Bourne shell) ksh (Korn shell) csh (C shell) tcsh bash … Differences mostly in scripting details

8 The Korn Shell (ksh) I will be using ksh as the standard shell for examples in this class Language is a superset of the Bourne shell (sh)

9 Changing Shell On most UNIX machines: –which ksh (note path) –chsh On the some machines: –which ksh (note path /bin/ksh) –ypchsh

10 Environment variables A set of variables the shell uses for certain operations Variables have a name and a value Current list can be displayed with the env command A particular variable’s value can be displayed with echo $ Some interesting variables: HOME, PATH, PS1, USER, HOSTNAME, PWD

11 Setting environment variables Set a variable with –ksh: = –tcsh: setenv Examples: –TERM=vt100 –PS1=myprompt> –PS1=“multiple word prompt> “ –PATH=$PATH:$HOME –DATE=`date`

12 Aliases Aliases are used as shorthand for frequently-used commands Syntax: –ksh: alias = –tcsh: alias Examples: –alias ll=“ls -lF” –alias la=“ls -la” –alias m=more –alias up=“cd..” –alias prompt=“echo $PS1”

13 Repeating commands Use history to list the last 16 commands tcsh: traverse command history: – -P previous history – -N next history ksh: ESC, then k (up), j (down) RETURN

14 Editing on the command line Some command lines can be very long and complicated - if you make a mistake you don’t want to start all over again You can interactively edit the command line in several ways –set -o vi allows you to use vi commands to edit the command line –set -o vi-tabcomplete also lets you complete commands/filenames by entering a TAB

15 Login scripts You don’t want to enter aliases, set environment variables, set up command line editing, etc. each time you log in All of these things can be done in a script that is run each time the shell is started For ksh: –~/.profile - is read for a login shell –~/.kshrc For tcsh –~/.login –~/.cshrc

16 Example.profile (partial) # set ENV to a file invoked each time sh is started for interactive use. ENV=$HOME/.shrc; export ENV HOSTNAME=`hostname`; export HOSTNAME alias 'll'='ls -l' alias 'la'='ls -la' alias 'ls'='ls -F' alias 'rm'='rm -i' alias 'm'='more' set -o vi echo ".profile was read"

17 stdin, stdout, and stderr Each shell (and in fact all programs) automatically open three “files” when they start up –Standard input (stdin): Usually from the keyboard –Standard output (stdout): Usually to the terminal –Standard error (stderr): Usually to the terminal Programs use these three files when reading (e.g. scanf() ), writing (e.g. printf() ), or reporting errors/diagnostics

18 Redirecting stdout Instead of writing to the terminal, you can tell a program to print its output to another file using the > operator >> operator is used to append to a file Examples: –man ls > ls_help.txt –Echo $PWD > current_directory –cat file1 >> file2

19 Redirecting stderr Instead of reading from the terminal, you can tell a program to read from another file using the: –ksh: 2> operator –tcsh: &> operator Examples (suppose j is a file that does not exist) {ajax} ls j ls: j: No such file or directory {ajax} ls j &> hello.txt {ajax} cat hello.txt ls: j: No such file or directory

20 Redirecting stdin Instead of reading from the terminal, you can tell a program to read from another file using the < operator Examples: –Mail < message –interactive_program < command_list

21 Pipes and filters Pipe: a way to send the output of one command to the input of another Filter: a program that takes input and transforms it in some way –wc - gives a count of words/lines/chars –grep - searches for lines with a given string –more –sort - sorts lines alphabetically or numerically

22 Examples of filtering ls -la | more cat file | wc man ksh | grep “history” ls -l | grep “bowman” | wc who | sort > current_users

23 UNIX Tutorial

24 UNIX Filesystem The filesystem is your interface to –physical storage (disks) on your machine –storage on other machines –output devices –etc. Everything in UNIX is a file (programs, text, peripheral devices, terminals, …) There are no drive letters in UNIX! The filesystem provides a logical view of the storage devices

25 Working directory The current directory in which you are working pwd command: outputs the absolute path (more on this later) of your working directory Unless you specify another directory, commands will assume you want to operate on the working directory

26 Home directory A special place for each user to store personal files When you log in, your working directory will be set to your home directory Your home directory is represented by the symbol ~ (tilde) The home directory of “user1” is represented by ~user1

27 UNIX file hierarchy Directories may contain plain files or other directories Leads to a tree structure for the filesystem Root directory: / / tmpusersbin mariagunnar csci1730joke.txt lab2.txtlab1.txt

28 Path names Separate directories by / Absolute path –start at root and follow the tree –e.g. /users/maria/joke.tx t / tmpusersbin mariagunnar csci1730joke.txt lab2.txtlab1.txt Relative path start at working directory.. refers to level above;. refers to working dir. If /users/maria/ccsci1730 is working dir, all these refer to the same file../joke.txt ~/joke.txt ~maria/joke.txt

29 Changing directories Change the working directory with the cd command –cd –Use absolute or relative path names –cd by itself equivalent to cd ~

30 Output of ls -lF total 4 lrwxr-xr-x 1 maria user 18 Aug 28 13:41 home -> /usr/people/maria/ -rw-r--r-- 1 maria user 94 Aug 28 13:42 nothing.txt drwxr-xr-x 2 maria user 9 Aug 28 13:40 test_dir/ File type PermissionsOwnerGroupModify dateFile name

31 Types of files Plain ( - ) –Most files –Includes binary and text files Directory ( d ) –A directory is actually a file –Points to another set of files Link ( l ): A pointer to another file or directory Special: e.g. peripheral devices

32 Creating links ln –s This command creates a symbolic link The file “link_name” will be a pointer to the “existing_file” which may be in another directory or even on another physical machine

33 File permissions Permissions used to allow/disallow access to file/directory contents Read (r) 4, write (w) 2, and execute (x) 1 For owner, group, and world (everyone) chmod –chmod 700 file.txt –chmod g+rw file.txt

34 File ownership Each file has a single owner chown command can be used to change the owner (usually only root user can use this command) There are also various groups to which users can belong Groups may have different permissions than everyone else

35 File modification date Last time the file was changed Useful information when –There are many copies of a file –Many users are working on a file touch command can be used to update the modification date to the current date, or to create a file if it doesn’t exist

36 Looking at file contents cat –“concatenate” –output the contents of the file all at once more –Output the contents of a file one screen at a time –Allows forward and backward scroll and search

37 Moving, renaming, copying, and removing files mv (rename) mv (move) mv (move & rename) cp [ | | ] (copy) rm [-i] (remove) Let’s try some examples…

38 Creating and removing directories mkdir –Create a subdirectory of the current directory rmdir –Remove a directory (only works for empty directories) rm –r –Remove a directory and all of its contents, including subdirectories

39 Wildcards in file names All of the commands covered here that take file names as arguments can also use wildcards –* for any string, e.g. *.txt, obj*, a*.* –? for any character, e.g. doc? –[] around a range of characters, e.g. [a- c]*

40 Getting help on UNIX commands These notes only give you the tip of the iceberg for these basic commands man shows you all the documentation for a command apropos shows you all the commands with the keyword in their description

41 The UNIX System Kernel – Heart of the OS –Process scheduling –I/O control (accesses) Shell – Interpreter between the user and the computer Tools and applications –Accessible from shell –Can be run independently of shell

42 UNIX System Programming Programs make system (primitive), or library subroutine (efficient, special purpose) calls to invoke kernel. Types of system calls –File I/O –Process management –Inter-process communication (IPC) –Signal handling

43 System Calls (Library calls) System calls –Interface to the kernel Library fread Kernel Space User Space Program Code read user read kernel

44 Basic file I/O Processes keep a list of open files Files can be opened for reading, writing Each file is referenced by a file descriptor (integer) Three files are opened automatically –FD 0: standard input –FD 1: standard output –FD 2: standard error

45 File I/O system call: open() fd = open(path, flags, mode) path: string, absolute or relative path flags: –O_RDONLY - open for reading –O_WRONLY - open for writing –O_RDWR - open for reading and writing –O_CREAT - create the file if it doesn’t exist –O_TRUNC - truncate the file if it exists –O_APPEND - only write at the end of the file mode: specify permissions if using O_CREAT

46 File I/O system call: close() retval = close(fd) Close an open file descriptor Returns 0 on success, -1 on error

47 File I/O system call: read() bytes_read = read(fd, buffer, count) Read up to count bytes from file and place into buffer fd: file descriptor buffer: pointer to array count: number of bytes to read Returns number of bytes read or -1 if error

48 File I/O system call: write() bytes_written = write(fd, buffer, count) Write count bytes from buffer to a file fd: file descriptor buffer: pointer to array count: number of bytes to write Returns number of bytes written or -1 if error

49 System call: lseek() retval = lseek(fd, offset, whence) Move file pointer to new location fd: file descriptor offset: number of bytes whence: –SEEK_SET - offset from beginning of file –SEEK_CUR - offset from current offset location –SEEK_END - offset from end of file Returns offset from beginning of file or -1

50 UNIX File access primitives open – open for reading, or writing or create an empty file creat - create an empty file close – read - get info from file write - put info in file lseek - move to specific byte in file unlink - remove a file remove - remove a file fcntl - control attributes assoc. w/ file

51 Simple file I/O examples

52 File I/O using FILEs Most UNIX programs use higher-level I/O functions –fopen() –fclose() –fread() –fwrite() –fseek() These use the FILE datatype instead of file descriptors Need to include stdio.h

53 Using datatypes with file I/O All the functions we’ve seen so far use raw bytes for file I/O, but program data is usually stored in meaningful datatypes (int, char, float, etc.) fprintf(), fputs(), fputc() - used to write data to a file fscanf(), fgets(), fgetc() - used to read data from a file