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.

Similar documents
Introduction to UNIX Shell Exercises

ACS Unix (Winter Term, ) Page 21

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.

The Online Unix Manual

5/20/2007. Touring Essential Programs

System Programming. Unix Shells

EECS2301. Lab 1 Winter 2016

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

CST Algonquin College 2

Linux Command Line Interface. December 27, 2017

Unix Processes. What is a Process?

Unix Shell Environments. February 23rd, 2004 Class Meeting 6

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

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

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

Introduction to the Shell

Assignment clarifications

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Sperimentazioni I LINUX commands tutorial - Part II

Introduction to Linux

Introduction to Linux Organizing Files

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

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Chapter 9. Shell and Kernel

Review of Fundamentals

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

CSC UNIX System, Spring 2015

UNIX. The Very 10 Short Howto for beginners. Soon-Hyung Yook. March 27, Soon-Hyung Yook UNIX March 27, / 29

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

CSCI 2132 Software Development. Lecture 3: Unix Shells and Other Basic Concepts

Processes and Shells

Unix Shells and Other Basic Concepts

Shells and Shell Programming

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

UNIX Kernel. UNIX History

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

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

bash, part 3 Chris GauthierDickey

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

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Linux shell scripting Getting started *

System Programming. Introduction to Unix

Shells and Shell Programming

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

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Systems Programming/ C and UNIX

Introduction to UNIX Part II

Creating a Shell or Command Interperter Program CSCI411 Lab

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

Lab 2: Linux/Unix shell

Perl and R Scripting for Biologists

UNIX Quick Reference

Advanced UNIX. 2. The Shell (Ch.5, Sobell) Objectives. to supplement the Introduction to UNIX slides with extra information about the Shell

CS 307: UNIX PROGRAMMING ENVIRONMENT REVIEW FOR EXAM 2

Study Guide Processes & Job Control

Introduction: What is Unix?

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

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.

Implementation of a simple shell, xssh

Unix/Linux: History and Philosophy

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

Implementation of a simple shell, xssh

Introduction to the Linux Command Line January Presentation Topics

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

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

Lab 2: Implementing a Shell COMPSCI 310: Introduction to Operating Systems

Introduction to Shell Scripting

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

Command-line interpreters

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Oxford University Computing Services. Getting Started with Unix

UNIX Shell Programming

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

ACS Unix (Winter Term, ) Page 92

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

85321, Systems Administration Chapter 6: The shell

Configuring Your Login Session

Bamuengine.com. Chapter 7. The Process

UNIX Basics by Peter Collinson, Hillside Systems. The Korn Shell

Topic 2: More Shell Skills

An Introduction to Unix Power Tools

Chap2: Operating-System Structures

Operating Systems, Unix Files and Commands SEEM

Lab Week02 - Part I. Shell Commands. Professional Training Academy Linux Series

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Esercitazione Introduzione al linguaggio di shell

CSCI2467: Systems Programming Concepts

CSE II-Sem)

Operating Systems. Copyleft 2005, Binnur Kurt

Introduction to Linux Workshop 1

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Environment Variables

CHAPTER 2 THE UNIX SHELLS

Linux shell programming for Raspberry Pi Users - 2

Vi & Shell Scripting

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

CHAPTER 3 SHELL PROGRAMS: SCRIPTS

I/O and Shell Scripting

UNIX shell scripting

Linux Refresher (1) 310/ Fourth Workshop on Distributed Laboratory Instrumentation Systems (30 October - 24 November 2006)

Transcription:

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. The shell is your interface to the operating system. It acts as a command interpreter; it takes each command and passes it to the operating system. It then displays the results of this operation on your screen. There are several shells in widespread use. The most common ones are described below. Bourne shell (sh) Original Unix shell written by Steve Bourne of Bell Labs. Available on all UNIX systems. Does not have the interactive facilities provided by modern shells such as the C shell and Korn shell. The Bourne shell does provide an easy to use language with which you can write shell scripts. C shell (csh) Written at the University of California, Berkeley. As it name indicates, it provides a C like language with which to write shell scripts. Korn shell (ksh) Written by David Korn of Bell Labs. It is now provided as the standard shell on Unix systems. Provides all the features of the C and TC shells together with a shell programming language similar to that of the original Bourne shell. TC Shell (tcsh) Available in the public domain. It provides all the features of the C shell together with EMACS style editing of the command line. Bourne Again Shell (bash) Public domain shell written by the Free Software Foundation under their GNU initiative. Ultimately it is intended to be a full implementation of the IEEE POSIX Shell and Tools specification. Widely used within the academic community. Provides all the interactive features of the C shell (csh) and the Korn shell (ksh). Its programming language is compatible with the Bourne shell (sh). Your login shell is usually established by the local System Administrator when your userid is created. You can determine your login shell with the command: echo $SHELL Each shell has a default prompt. For the 5 most common shells: $ (dollar sign) - sh, ksh, bash % (percent sign) - csh, tcsh 1

Depending upon the shell, certain features will be available. The table below summarizes the features available with the 5 most common shells. Note: these features will be described in detail later. Bourne C TC Korn BASH sh csh tcsh ksh bash Programming language Yes Yes Yes Yes Yes Shell variables Yes Yes Yes Yes Yes Command alias No Yes Yes Yes Yes Command history No Yes Yes Yes Yes Filename completion No Yes* Yes Yes* Yes Command line editing No No Yes Yes* Yes Job control No Yes Yes Yes Yes * not the default setting for this shell Processes Whenever you enter a command at the shell prompt, it invokes a program. While this program is running it is called a process. Your login shell is also a process, created for you upon logging in and existing until you logout. UNIX is a multi-tasking operating system. Any user can have multiple processes running simultaneously, including multiple login sessions. As you do your work within the login shell, each command creates at least one new process while it executes. Process id: every process in a UNIX system has a unique PID - process identifier. ps - displays information about processes. Note that the ps command differs between different UNIX systems - see the local ps man page for details. To see your current shell's processes: % ps PID TTY TIME CMD 26450 pts/9 0:00 ps 66801 pts/9 0:00 -csh To see a detailed list of all of your processes on a machine (current shell and all other shells): % ps uc USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND jsmith 26451 0.0 0.0 120 232 pts/9 R 21:01:14 0:00 ps jsmith 43520 0.0 1.0 300 660 pts/76 S 19:18:31 0:00 elm jsmith 66801 0.0 1.0 348 640 pts/9 S 20:49:20 0:00 csh jsmith 112453 0.0 0.0 340 432 pts/76 S Mar 03 0:00 csh 2

To see a detailed list of every process on a machine: % ps ug USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND root 0 0.0 0.0 8 8 - S Feb 08 32:57 swapper root 1 0.1 0.0 252 188 - S Feb 08 39:16 /etc/init root 514 72.6 0.0 12 8 - R Feb 08 28984:05 kproc root 771 0.2 0.0 16 16 - S Feb 08 65:14 kproc root 1028 0.0 0.0 16 16 - S Feb 08 0:00 kproc { lines deleted } root 60010 0.0 0.0 1296 536 - S Mar 07 0:00 -ncd19:0 kdr 60647 0.0 0.0 288 392 pts/87 S Mar 06 0:00 -ksh manfield 60968 0.0 0.0 268 200 - S 10:12:52 0:00 mwm kelly 61334 0.0 0.0 424 640 - S 08:18:10 0:00 twm sjw 61925 0.0 0.0 552 376 - S Mar 06 0:00 rlogin kanaha mkm 62357 0.0 0.0 460 240 - S Feb 08 0:00 xterm ishley 62637 0.0 0.0 324 152 pts/106 S Mar 06 0:00 xedit march2 tusciora 62998 0.0 0.0 340 448 - S Mar 06 0:05 xterm -e dilfeath 63564 0.0 0.0 200 268 - S 07:32:45 0:00 xclock tusciora 63878 0.0 0.0 548 412 - S Mar 06 0:41 twm kill - use the kill command to send a signal to a process. In most cases, this will be a kill signal, hence the command name. However, other types of signals are usually supported. Note that you can only kill processes which you own. The command syntax is: Examples: kill [-signal] process_identifier(pid) kill 63878 - kills process 63878 kill -9 1225 - kills (kills!) process 1225. Use if simple kill doesn't work. kill -STOP 2339 - stops process 2339 kill -CONT 2339 - continues stopped process 2339 kill -l - list the supported kill signals You can also use CTRL-C to kill the currently running process. Suspend a process: Use CTRL-Z. Background a process: Normally, commands operate in the foreground - you can not do additional work until the command completes. Backgrounding a command allows you to continue working at the shell prompt. To start a job in the background, use an ampersand (&) when you invoke the command: myprog & To put an already running job in the background, first suspend it with CRTL-Z and then use the "bg" command: myprog CTRL-Z bg - execute a process - suspend the process - put suspended process in background Foreground a process: To move a background job to the foreground, find its "job" number and then use the "fg" command. In this example, the jobs command shows that two processes 3

are running in the background. The fg command is used to bring the second job (%2) to the foreground. jobs [1] + Running xcalc [2] Running find / -name core -print fg %2 Stop a job running in the background: Use the jobs command to find its job number, and then use the stop command. You can then bring it to the foreground or restart execution later. jobs [1] + Running xcalc [2] Running find / -name core -print stop %2 Kill a job running in the background, use the jobs command to find its job number, and then use the kill command. Note that you can also use the ps and kill commands to accomplish the same task. jobs [1] + Running xcalc [2] Running find / -name core -print kill %2 Some notes about background processes: If a background job tries to read from the terminal, it will automatically be stopped by the shell. If this happens, you must put it in the foreground to supply the input. The shell will warn you if you attempt to logout and jobs are still running in the background. You can then use the jobs command to review the list of jobs and act accordingly. Alternately, you can simply issue the logout command again and you will be permitted to exit. Redirection Redirection refers to changing the shell's normal method of handling standard output (stdout), standard input (stdin) and standard error (stderr) for processes. By default, all of these are from/to your screen. The following symbols are used on the shell command line to redirect a process's stdin, stdout and/or stderr to another location, such as a file or device. > - redirect stdout (overwrite) >> - redirect stdout (append) < - redirect stdin 2> - redirect stderr (sh,ksh,bash) >& - redirect stdout and stderr (csh,tcsh) 4

Examples: mail tony < memo - uses the file memo as input to the mail program ls -l > my.directory - redirects output of ls -l command to a file called my.directory. If the file already exists, it is overwritten cat Mail/jsmith >> Oldmail - appends the contents of Mail/jsmith to the file Oldmail (does not overwrite) myprog >& output - redirects stdout and stderr from myprog's execution to a file called output (csh,tcsh) (myprog > out) >& err - redirects stdout from myprog's execution to a file called out and stderr to the file err (csh,tcsh) myprog 2> runtime.errors - redirects stderr from myprog's execution to a file called runtime.errors (sh,ksh,bash) myprog > output 2>& - redirects stderr and stdout from myprog's execution to a file called output (sh,ksh,bash) myprog > out 2> err - redirects stdout from myprog's execution to a file called out and stderr to the file err (sh,ksh,bash) Pipes A pipe is used by the shell to connect the stdout of one command directly to the stdin of another command. The symbol for a pipe is the vertical bar ( ). The command syntax is: command1 [arguments] command2 [arguments] Pipes accomplish with one command what otherwise would take intermediate files and multiple commands. For example, operation 1 and operation 2 are equivalent: Operation 1 who > temp sort temp Operation 2 who sort 5

Pipes do not affect the contents of the input files. Two very common uses of a pipe are with the "more" and "grep" utilities. Some examples: ls -al more who more ps ug grep myuserid who grep kelly Filters A filter is a command that processes an input stream of data to produce an output stream of data. Command lines which use a filter will include a pipes to connect it to the stdout of one process and the stdin of another process. For example, the command line below takes the output of "who" and sorts it. The sorted output is then passed to the lp command for printing. In this example, sort is a filter. who sort lp Both filters and pipes demonstrate a basic UNIX principle: Expect the output of every program to become the input of another, yet unknown, program to combine simple tools to perform complex tasks. Features (csh) Each shell has its own set of features. Those of the C Shell are discussed below. Command history: The history mechanism maintains a list of recently used command lines, called events. Provides a shorthand for re-executing previous commands. To use history: 1. Set the history variable: set history = 100 2. Issue the history command and view the output: history 35 12:34 sort < unsorted > sorted.list 36 12:34 cat sorted.list 37 12:35 ls * > unsorted 38 12:35 cat unsorted 39 12:35 ls 40 13:06 history 41 13:08 alias 42 13:11 ls 43 13:11 vi.cshrc 6

3. To save history events across all login sessions, set the savehistory variable: set savehistory = 50 Event re-execution: Allows you to specify a shorthand for re-executing a previous event. Works with the history list.!! - repeats last command!number - repeats numbered command from history list!string - repeats last command starting with string Modifying previous events: Allows you to correct typos in previous command, or modify it to create a new command. ^old^new!number:s/old/new/ - changes the string "old" to the string "new" in the last command issued - changes numbered command from history list; substitutes the string "old" with the string "new". Note that there is no space between number and : Aliases: The alias command allows you to define new commands. Useful for creating shorthands for longer commands. The syntax is. alias entered_command executed_command Some examples: alias m more alias rm "rm -i" alias h "history -r more" alias xpvm /source/pd/xpvm/src/rs6k/xpvm To view all current aliases: alias To remove a previously defined alias: unalias alias_name Filename Generation: When you give the shell abbreviated filenames which contain special characters (metacharacters), it can generate filenames which match existing files. Some examples appear below: ls *.txt - list files with.txt suffix ls [abc]* - list files with names that start with a, b or c 7

lpr prog?.c cd ~jsmith - print files named prog?.c where? is any character - change to user jsmith's home directory You can "turn off" filename generation by setting the noglob variable. This will permit special characters to be interpreted literally. For example: set noglob Filename Completion: The shell will complete a filename for you if you type in only enough characters to make the name unique. This feature is a great time saver and helps to prevent typos when trying to type long filenames. To use filename completion, you need to set filec, either on the command line or in one of your initialization files. set filec Then, when specifying a filename, type the part which is unique and hit the escape key (C shell) or tab key (TC Shell). For example, if you had a directory with a long name, such as "Introduction.UNIX.Filesystems", you could cd to that directory by using the cd command with only a portion of the file's name, provided that the portion you specify is unique (no other files have similar names) cd Intro<ESC> Note: typing a portion of a filename and then hitting CTRL-D instead of ESCape or TAB will display a list of the filenames which match. Variables (csh) Each shell has its own set of variables and rules for using variables. Those associated with the C Shell are discussed below. The shell has variables which are predefined as well as variables which you define (user defined). Shell variables control many aspects of how your shell environment behaves. Modifying these variables (and creating new ones) allows you to customize your shell environment. Shell variables are used extensively when creating shell scripts (covered later). Variables can be local - current shell only global - current shell and child processes/shells string - treated as character numeric - treated as numbers arrays - contain more than one value 8

Commands used to declare and manipulate shell variables: set - assigns non-numeric string variables locally unset - removes a previously "set" variable set - shows all "set" variables setenv - assigns non-numeric string variables globally unsetenv - removes a previously setenv variable setenv - shows all setenv variables @ - assigns numeric variables locally echo $variable - displays value of variable Examples set name=fred - Sets variable name to value of fred unset name - Unsets the variable name set path=($path. ~/bin) - Adds to current setting of string array variable path setenv DISPLAY makena:0 - Sets environment variable DISPLAY echo $HOME - Displays value of variable HOME set colors=(red green blue) - Assigns three values to the string array variable colors @ count = 1 - Sets numeric variable count to one @ count = ($count + 1) - Adds one to numeric variable count set counts = (1 22 4 9) - Assigns 4 values to numeric array variable counts @ counts[2] = 5 - Assigns values to second element of numeric array variable counts @ echo $counts[3] - Displays value of third element of numeric array variable counts Predefined Shell Variables. A number of shell variables are predefined by the shell itself or inherited from the system environment. Some of the more common ones are described below. $ Contains the process id of the current shell argv Contains the command line arguments for an invoked command. argv is an array, with $argv[0] set to the name of the invoked command, $argv[1] set to the first argument, $argv[2] set to the second argument...and so on. $argv[*] can be used to specify all arguments. You may also use the shorthand $n where n is the number of the argument. #argv Set to the actual number of arguments in argv, excluding argv[0] 9

cdpath Expands the search path for the cd command. By default, the cd command issued with a simple filename will search only the working directory. Use cdpath to increase the number of directories searched. set cdpath=(/usr/jenny /usr/jenny/mail../) CWD or cwd Holds that name of the working/current directory. echo Causes the shell to echo the command before executing it. Use set/unset to turn this on/off. filec Enables file completion. Use set/unset to turn this on/off. history Controls the size of the history list. 100 is recommended as safe size. If the number is too large, the shell may run out of memory. HOME or home The pathname of your home directory ignoreeof Prevents exiting the shell by typing CTRL-D, and thus, prevents accidentally logging off. Use set/unset to turn this on/off. noclobber Prevents you from accidentally overwriting a file when you redirect output. Use set/unset to turn this on/off. noglob Prevents the shell from generating/expanding ambiguous filenames. Use set/unset to turn this on/off. notify Tells the shell to notify you immediately when a background job completes. Ordinarily, notification will wait until the next shell prompt to prevent interruption of work. Use set/unset to turn this on/off. PATH or path Specifies the path that the shell searches when asked to execute a command. If an executable is not found in the path, you must specify its full pathname. prompt Allows you to customize the shell prompt. By default, the C shell prompt is simply a percent sign (%). For example, to display the machine name you are logged into as part of your prompt: set prompt = "`hostname -s`% " savehist Specifies how many the number of command events to save as history after you logout. These events are saved in a file called.history in your home directory. The shell uses these as your initial history after you login again. 10

shell Contains the pathname of the shell status Contains the exit status of the last executed command USER or user Contains you login userid verbose Causes the shell to display each command after a history substitution. Use set/unset to turn this on/off. Initialization Files System-wide shell initialization files are common on UNIX systems. These files can be "sourced" automatically by the shell and are typically setup by the System Administrator for the local environment. Some examples of system-wide initialization files might be: /etc/environment /etc/profile /etc/cshrc /etc/login Every shell provides a means for the user to customize certain aspects of the shell's behavior. These customizations usually permit you to augment and/or override the system-wide defaults. User customizations are specified in initialization files located in the top level of your home directory. Naming: Depending upon the shell, you must name your initialization file(s) accordingly. Executed during interactive login.login - csh, tcsh.profile - sh, ksh, bash.bash_profile - bash (alternative 1).bash_login - bash (alternative 2) Executed for every new shell.cshrc - csh, tcsh.tcshrc - tcsh.kshrc - ksh.bashrc - bash The C shell uses two files to set user preferences:.cshrc runs at each login before the.login file runs whenever another shell is created runs when a new window is opened runs when many utilities are invoked typically sets alias information, path variable, filec, prompt, etc. 11

.login runs at invocation of login shell, after the.cshrc file typically sets terminal characteristics and one time shell options and environment variables After changing your.login or.cshrc files, you must "source" them for the changes to take effect: source.login source.cshrc The system administrator may/may not provide you with default.cshrc and.login files when you first obtain your userid. If they are provided, be careful about modifying them - especially removing specifications which are required for your local system. Example.cshrc and.login files are provided. Logout Files You are able to specify commands which the shell will execute upon logout. These commands are kept in a file located in the top level of your home directory..logout.bash_logout - csh, tcsh - bash 12