CST Algonquin College 2

Similar documents
Review of Fundamentals

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Chapter 9. Shell and Kernel

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

5/20/2007. Touring Essential Programs

Introduction to UNIX Shell Exercises

Unix/Linux: History and Philosophy

Lab 2: Linux/Unix shell

5/8/2012. Specifying Instructions to the Shell Chapter 8

UNIX System Programming Lecture 3: BASH Programming

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Introduction to UNIX Part II

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

Linux Command Line Interface. December 27, 2017

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Processes and Shells

Unix Processes. What is a Process?

Module 8 Pipes, Redirection and REGEX

Linux for Beginners. Windows users should download putty or bitvise:

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

bash, part 3 Chris GauthierDickey

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

85321, Systems Administration Chapter 6: The shell

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

Bash command shell language interpreter

Implementation of a simple shell, xssh

UNIX Essentials Featuring Solaris 10 Op System

EECS2301. Lab 1 Winter 2016

Linux & Shell Programming 2014

Topic 2: More Shell Skills

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

Introduction to the Shell

Bourne Shell (ch 8) Overview. Bourne Shell. Bourne Shell. Bourne Shell. Bourne Shell. Redirect standard error. Redirect standard error

The Command Line. Matthew Bender. Friday 18 th September, CMSC Command Line Workshop

UNIX System Administration

UNIX Kernel. UNIX History

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

Implementation of a simple shell, xssh

S E C T I O N O V E R V I E W

Part III. Shell Config. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Shells and Shell Programming

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Linux shell programming for Raspberry Pi Users - 2

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

5/8/2012. Exploring Utilities Chapter 5

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

Assignment clarifications

System Programming. Unix Shells

Creating a Shell or Command Interperter Program CSCI411 Lab

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

BASH and command line utilities Variables Conditional Commands Loop Commands BASH scripts

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

CHAPTER 2 THE UNIX SHELLS

Basics. I think that the later is better.

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Unix Internal Assessment-2 solution. Ans:There are two ways of starting a job in the background with the shell s & operator and the nohup command.

Introduction: What is Unix?

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

Project 1: Implementing a Shell

SOFTWARE ARCHITECTURE 3. SHELL

Useful Unix Commands Cheat Sheet

Bash Reference Manual

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

Common File System Commands

CSE II-Sem)

Shells and Shell Programming

Standard. Shells. tcsh. A shell script is a file that contains shell commands that perform a useful function. It is also known as shell program.

CS 307: UNIX PROGRAMMING ENVIRONMENT REVIEW FOR EXAM 2

Topic 2: More Shell Skills

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Chapter 1 - Introduction. September 8, 2016

Introduction to Linux Part 1. Anita Orendt and Wim Cardoen Center for High Performance Computing 24 May 2017

Command-line interpreters

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

The Online Unix Manual

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

This is Lab Worksheet 3 - not an Assignment

Vi & Shell Scripting

System Programming. Introduction to Unix

Essential Unix and Linux! Perl for Bioinformatics, ! F. Pineda

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

Linux shell scripting Getting started *

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

Why Bourne Shell? A Bourne Shell Script. The UNIX Shell. Ken Wong Washington University. The Bourne Shell (CSE 422S)

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Work Effectively on the Command Line

PESIT Bangalore South Campus

Introduction to Linux Workshop 1

Please choose the best answer. More than one answer might be true, but choose the one that is best.

Unix Handouts. Shantanu N Kulkarni

An Introduction to Unix Power Tools

UNIX Shell Programming

CSC209F Midterm (L5101) Fall 1998 University of Toronto Department of Computer Science

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Transcription:

The Shell

Kernel (briefly) Shell What happens when you hit [ENTER]? Output redirection and pipes Noclobber (not a typo) Shell prompts Aliases Filespecs History Displaying file contents CST8207 - Algonquin College 2

Chapter 1 (parts of) Chapter 7 (The entire chapter) Chapter 9 (The entire chapter) Chapter 5 (utilities, up to end of Page 160) CST8207 - Algonquin College 3

Kernel is the core part of Linux. It manages resource of Linux. Resources means facilities available in Linux. For e.g. Facility to store data, print data on printer, memory, file management etc. Kernel decides who will use this resource, for how long and when. It runs your programs (or set up to execute binary files). The kernel acts as an intermediary between the computer hardware and various programs/application/shell.

It performs the following tasks : I/O management Process management Device management File management Memory management

7

In OS there is special program called Shell. Shell accepts your instructions or commands and pass them to kernel. Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc. In Unix and Linux, shell is the term used to describe the command-line interpreter (roughly equivalent to command.com in DOS).

Several shell available with Linux including: BASH, CSH, TCSH, KSH In this course, we will use the bash shell because of its wide popularity BASH ( Bourne-Again SHell ), developed by Brian Fox and Chet Ramey from Free Software Foundation (FSF). It is the most common shell in Linux.

To find all available shells in your system type following command: cat /etc/shells To find your current shell type following command echo $SHELL Builtins A builtin is a utility that is built into a shell. Each of the shells has its own set of builtins. A shell does not fork a new process when you execute builtins, so they run more quickly.

Command A directive to the shell typed at the prompt. It could be a utility, another program, a built-in, or a shell script. Program A file containing a sequence of executable instructions. Note that it's not always a binary file but can be text. Filter A command that can take its input from stdin and send its output to stdout. It normally transforms a stream of data through a series of pipes. All good scripts should be written as filters. Utility A (set of) program(s) that provides services to a user.

Built-in A command that is built into the shell. That is, it is not a program as defined above. They do not require a new process unlike those above. History A list of previous commands that can be recalled, edited if desired, and re-executed. Token The smallest unit of parsing; often a word delimited by white space (blanks or spaces, tabs and newlines) and other punctuation (special characters).

Stdin The standard input; the keyboard; what cin reads from; the file at offset 0 in the file table. Stdout The standard output; the screen, what cout writes to; offset 1 in the file table; Stderr The standard error; the screen; what cerr writes to; offset 2 in the file table. Standard I/O stdin, stdout, and stderr.

Pipe To use a shell service that connects the stdout of one program to the stdin of the next; the " " symbol. Redirect To use a shell service that replaces stdin, stdout, or stderr with a regular file. CST8207 - Algonquin College 14

A process is a program that is being executed. Some processes (called daemons) never end, as they are providing a service to many users such as FTP services from ftpd. Other processes are run by you, the user, from commands you enter at the prompt. These usually run in the foreground, using the screen and keyboard for their standard I/O. You can run them in the background instead, if you wish. Each process has a PID (or pid, a process identifier), and a parent process with its own pid, known to the child process as a ppid (parent pid). You can examine the running processes with the ps command or examine the family relationships with pstree.

A child process is forked or spawned from the parent by means of a system call to the kernel services. Forking produces an exact copy of the process, so it is usually then replaced during an exec system call (ie the exact copy, the child, usually asks the kernel to turn it into a different process). The forked copy also includes the environment variables and the file table. This latter becomes very useful when redirecting the standard I/O, since a child can redirect its I/O without affecting the parent.

PID Environment Process Code and Data 0 stdin 1 stdout 2 stderr 3

1. I/O management 2. Process management 3. Device management 4. File management 5. Memory management 6. All of the above 7. None of the above 90% 10% 0% 0% 0% 0% 0% 1 2 3 4 5 6 7 CST8207 - Algonquin College 18

There's the system prompt. You enter a line of words, giving a command to the shell. What happens next?

1. Substitute history 2. Tokenize command line (break it into "words" based on spaces and other delimiters; also known as lexical analysis, or parsing) 3. Update history 4. Process quotes 5. Substitute aliases and define functions 6. Set up redirection, pipes, and background processes 7. Substitute variables 8. Substitute commands 9. Substitute filename (called "globbing") 10. Execute (run) program

The command history is a list of the previous commands you have executed in this session with this copy of the shell. It's usually set to some large number of entries, often 100 or more.

To list the history: System prompt> history more To repeat the last command entered: System prompt>!! To repeat the last ls command: System prompt>!ls To repeat the command from prompt number 3: System prompt>!3 To scroll up and down the list (arrow keys): System prompt> To edit the command: [DEL] [Backspace]

Three files are available immediately upon shell startup: stdin, stdout, stderr These can be overridden by various redirection operators Following is a list of these operators

Redirection Operator < filename > filename >> filename 2> filename 2>>filename &> filename >& 2>&1 1>&2 >! <> filename Redirects input from filename Redirects output to filename Appends output to filename Redirects errors to filename Appends errors to filename Redirects output and errors to filename As above; preferred format Redirects errors to output destination As above but reversed Overrides noclobber Behaviour Uses filename as both standard input and output if a device file (from /dev)

To create an alias alias ll="ls -l" To list all aliases alias or alias more To delete an alias unalias ll

Metacharacter \ & ; $? [abc] [!abc] * (cmd) {cmd} Interprets the following character literally Processes in the background Separates commands Substitutes variables Matches for a single character Matches for one character from a set; in this case an a or b or c Matches for one character not from a set; in this case not an a or b or c Matches zero or more characters Meaning Executes commands in a subshell Executes commands in the current shell

*? [aed] [a-e] [!a-e] Matches 0 or more characters in a filename Matches 1 character in a filename Matches 1 character in a filenameprovided it is either a,e, or d. Matches 1 character in a filenameprovided it is a,b,c,d, or e Matches 1 character in a filenameprovided it is not a,b,c,d, or e

(<) Output Redirection Operator ( file ls > file1 (Redirect output to a cat /etc/fstab > file2 (Redirect output to ( printer (<<) Append Redirection Operator Lets you redirect a output to the end of an existing file ls >> file3 (Add output to the end of a file)

Using to connect two commands cat /etc/passwd grep user1 The output of the first command is the input of the second command

It sends output to both standard output and a file: Examples: cat /etc/fstab tee ~/file1 ls a ~/ tee a ~/file1 Option a : append to given file, not to overwrite

Setting noclobber variable prevents you from accidentally overwriting a file when you redirect output to a file To setup noclobber set o noclobber To unset noclobber set +o noclobber Example: date > f1 set o noclobber date > f1 set +o noclobber date > f1

Ctrl+H To erase one character Ctrl+U To erase one line Ctrl+W To erase a word

Changing primary user prompt: PS1 environment variable You can make your own prompt by using the following codes. \d, \h, \u, \n, \s, \t, \w PS1= [\u@\h \w] $ Changing secondary user prompt PS2 environment variable Default secondary prompt is > for unfinished command in previous line, waiting for user to continue the command line. PS2= secondary prompt:

wc command prints nline, word and byte counts for files Options w: word counts l: line counts c: character counts Examples: wc ~/* wc -l /etc/fstab

Using {} to create a list of files or directories touch ~/dir1/{f1,f2,f3} mkdir ~/dir2/{a1,a2,a3} mkdir p \ ~/backup/{old,new}/{labs{1,2,3},lecture{1,2,3}} How many directories have been created? find ~/backup type d wc -l

history Keeps a record of the most recent commands you have used Clear history list history c Configuring history HISTFILE: where the history is stored, by default, this file is ~/.bash_history HISTSIZE: the number of history events could be saved

You can use! Command to reference an history event The reference can be the event number!3 The reference can be a beginning set of characters in the event!chm The reference can be an offset from the end of the history list! 4

tail head more less cat tac Display the last 10 lines Display the first 10 lines Display text files page by page/line by line Less is more Display the entire file Display the entire file in reverse order

df command Used to check free space on the disk Only view mounted filesystems Options: -h Example: df -h du command Directory usage: to view the size of directory and its contents in kilobytes. Options: -s, -h Example: du hs /etc