ACS Unix (Winter Term, ) Page 21

Similar documents
The Online Unix Manual

5/20/2007. Touring Essential Programs

System Programming. Unix Shells

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.

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.

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

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

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

UNIX Kernel. UNIX History

Unix Shells and Other Basic Concepts

EECS2301. Lab 1 Winter 2016

Linux shell scripting intro/review

Introduction to Shell Scripting

Assignment clarifications

CSE II-Sem)

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

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

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

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

Introduction to UNIX Shell Exercises

Introduction: What is Unix?

Shells and Shell Programming

Shells and Shell Programming

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

UNIX shell scripting

COMP 4/6262: Programming UNIX

Operating Systems, Unix Files and Commands SEEM

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Bash Programming. Student Workbook

What is a shell? The shell is interface for user to computer OS.

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

Introduction to Linux (Part I) BUPT/QMUL 2018/03/14

Chapter-3. Introduction to Unix: Fundamental Commands

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

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

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

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

CHAPTER 3 SHELL PROGRAMS: SCRIPTS

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

85321, Systems Administration Chapter 6: The shell

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

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

Lecture # 2 Introduction to UNIX (Part 2)

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

Configuring Your Login Session

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

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

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

ACS Unix (Winter Term, ) Page 92

Chapter 9. Shell and Kernel

Lecture #10 - Interactive Korn Shell (Chapter 11)

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

Unix/Linux: History and Philosophy

CISC 220 fall 2011, set 1: Linux basics

Introduction to the Shell

No lectures on Monday!!

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

Overview of the UNIX File System

Implementation of a simple shell, xssh

CS246 Spring14 Programming Paradigm Notes on Linux

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

CptS 360 (System Programming) Unit 2: Introduction to UNIX and Linux

Practical Computing-II. Programming in the Linux Environment. 0. An Introduction. B.W.Gore. March 20, 2015

Week 5 Lesson 5 02/28/18

PROGRAMMAZIONE I A.A. 2015/2016

Introduction to Linux Workshop 1

BIOINFORMATICS POST-DIPLOMA PROGRAM SUBJECT OUTLINE Subject Title: OPERATING SYSTEMS AND PROJECT MANAGEMENT Subject Code: BIF713 Subject Description:

CSCI2467: Systems Programming Concepts

Overview of the UNIX File System. Navigating and Viewing Directories

Welcome to the Bash Workshop!

CST Algonquin College 2

(a) About Unix. History

System Programming. Introduction to Unix

Work Effectively on the Command Line

Writing Shell Scripts part 1

SIDDHARTH GROUP OF INSTITUTIONS :: PUTTUR Siddharth Nagar, Narayanavanam Road QUESTION BANK (DESCRIPTIVE) UNIT I

Implementation of a simple shell, xssh

Vi & Shell Scripting

UNIX / LINUX - GETTING STARTED

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

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

The UNIX operating system is a set of programs that act as a link between the computer and the user.

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

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

Review. Preview. Linux System Roadmap (Libraries)

C Shell Tutorial. Section 1

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Linux Systems Administration Getting Started with Linux

COMP2100/2500 Lecture 16: Shell Programming I

What is UNIX? A Little Bit about UNIX and User Interfaces. Adapted from Practical Unix and Programming Hunter College

Useful information mostly written by me, the conspicuous exception being the bash manpage... BASH(1) 1998 Apr 1 BASH(1)

CSC UNIX System, Spring 2015

A shell can be used in one of two ways:

Introduction. Let s start with the first set of slides

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

Shell Programming (bash)

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Continue reading command lines even after an execution error has caused the abandonment of aline.

Bash Reference Manual

Transcription:

ACS-294-001 Unix (Winter Term, 2016-2017) Page 21 The Shell From the beginning, Unix was designed so that the shell is an actual program separated from the main part of the operating system. What is a shell? A shell is a command processor -- a program that reads and interprets the commands that you enter. A shell is a programming language. You can write programs, called script, for a shell to interpret. These scripts can contain regular Unix commands, as well as special shell programming commands. A shell is your main interface into Unix. Using the facilities that are built into your shell, you can create a highly customized environment for yourself. There are several shells. Therefore, you can have a choice as to which interface you want to use. The Bourne Shell Family Bourne shell (sh) The earliest Unix shell developed by Steven Bourne of AT&T Bell Labs. It is still in use. Korn shell Bash (ksh) The Korn shell is an upwards compatible extension to Bourne shell. It was developed by David Korn, a Bell Labs scientist, in the mid-1980s. (bash) Bash stands for the Bourne Again SHell. It is the product of the Free Software Foundation. Bash extends the capabilities of the basic Bourne shell in a manner similar to the Korn shell.

ACS-294-001 Unix (Winter Term, 2016-2017) Page 22 Zsh (zsh) Zsh was developed by Paul Falstad in 1990. Zsh offers all of the important features of the other Unix shells as well as new capabilities. It is popular among programmers and advanced Unix users. The C-Shell Family C-Shell Tcsh (csh) The C-Shell was designed by Bill Joy as the Berkeley Unix alternative to the Bourne shell. The C-Shell offers many advantages over the Bourne shell. It is very popular among experienced Unix users, especially at universities and research organizations. (tcsh) Tcsh is an enhanced C-Shell that offers advanced features. What Shell Should You Use? Worldwide, the most widely used shells are the Bourne shell and its replacement, the Korn shell. However, in the academic, research, and programming communities, the C-Shell is the most popular shell. As a command processor, the C-Shell family provides a good allaround working environment. The programming language used by the Bourne shell family is easier and more pleasant to use than that of the C-Shell.

ACS-294-001 Unix (Winter Term, 2016-2017) Page 23 The Relative Complexity of Different Shells Name of Shell Size of the Man page (bytes) Relative Complexity rc 37,885 1.00 Bourne 44,500 1.17 C-Shell 76,816 2.03 Bash 127,361 3.36 Zsh 133,565 3.53 Korn 141,391 3.73 Tcsh 199,834 5.27 Changing Your Shell Temporarily When you log in, the shell Unix starts automatically is called your login shell. Since a shell is a program itself, you can start a new shell any time by entering the name of that shell. When you are finished with the new shell, you can stop it by entering either the exit command or press ^D, the eof key. You must go back to the login shell before you can log out. Using the C-Shell Shell Variables A shell variable is an item, known by a name, which represents a value of some type. The value of a shell variable can be changed. There are two types of shell variables: 1. Variables that act as off/on switches. 2. Variables that store a particular value as a string of characters.

ACS-294-001 Unix (Winter Term, 2016-2017) Page 24 Shell Variables That Act as Switches: set, unset To turn on switch variables, use the set command. Syntax: set [variable-name] set ignoreeof set filec To turn off a switch, use unset command. unset ignoreeof unset filec To display all shell variables and their current settings, enter the command set without arguments: set If a variable is set, its name will appear in the list. If a variable is unset, its name will not appear. Built-in Shell Variables: Switches Variable Name echo filec ignoreeof nobeep noclobber noglob nonomatch notify verbose Purpose display each command before execution enable filename completion must log out with logout instead of eof key (^D) no beep if filename is ambiguous do not allow redirected output to replace a file inhibit expansion of filenames no error if filename expansion matches nothing notify about job completions at any time display full command after history substitution

ACS-294-001 Unix (Winter Term, 2016-2017) Page 25 Shell Variables That Store Values: set Some of the shell variables can be set by you to modify the shell s behavior. Other values are set by the shell to pass information to you. To set a variable of this type, use the set command with the following syntax: set [variable-name = value] set history=50 Built-in Shell Variables That Store Values Variable Name argv cdpath cwd fignore hardpath histchars history home mail path prompt savehist shell status term time user Purpose list of arguments for current command directories to search to find a subdirectory pathname of current working directory suffixes to ignore during the file name completion no symbolic link pathnames in directory stack the two characters used for history substitution size of the history list pathname of your home directory pathnames where shell should check for mail list of directories to search for programs string of characters used for command prompt number of history lines to save upon logout pathname of the shell program return status of the last command type of terminal you are using threshold value for reporting of command timing name of the userid currently logged in On occasion, you may want to give a variable a value that contains spaces or other special characters. In that case, you must put single quotes around the value.

ACS-294-001 Unix (Winter Term, 2016-2017) Page 26 set prompt='my Own Prompt>' My Own Prompt>echo $prompt My Own Prompt> My Own Prompt>set prompt=' ' echo $prompt Displaying the Value of a Variable: echo The command echo simply displays the value of anything you give it. echo Have a nice day! Have a nice day! To display the value of a single variable, use the echo command with the following syntax: echo $variable-name echo $history 100 set history=200 echo $history 200 echo My working directory is $cwd and my home directory is $home. My working directory is /home/sliao and my home directory is /home/sliao.

ACS-294-001 Unix (Winter Term, 2016-2017) Page 27 Environment Variables The shell variables are used only within the shell to control preferences and settings. There is a whole other set of variables that the shell maintains for passing values between programs. These are called Environment Variables or Global Variables. All environment variables have uppercase names. Common Environment Variables Variable Name EDITOR HOME LOGNAME MAIL MANPATH PAGER PATH SHELL TERM USER Purpose pathname of your text editor pathname of your home directory name of the userid currently logged in pathname of your mail program list of directories to search for manual pages name of the paging program you prefer list of directories to search for programs pathname of the shell program type of terminal you are using name of the userid currently logged in The value of an environment variable is available to any program or shell. Many programs look at the TERM variable to see what type of terminal you are using.

ACS-294-001 Unix (Winter Term, 2016-2017) Page 28 Setting the Value of an Environment Variable setenv The syntax is setenv [variable-name value] setenv TERM vt100 Displaying the Value of Environment Variable(s) printenv The syntax is printenv [variable-name] printenv will display all the environment variables printenv TERM echo echo $TERM How Environment and Shell Variables Are Connected There are six common shell variables that have the same names as environment variables (except that environment variables have uppercase names). 1) home, shell vs. HOME, SHELL home contains the pathname of your home directory and shell contains the pathname of the shell you use. Whenever you log in, Unix automatically sets the values of home and shell (and HOME and SHELL). Your programs will examine these variables from time to time, but you will probably never need to change them yourself.

ACS-294-001 Unix (Winter Term, 2016-2017) Page 29 2) term, path, user vs. TERM, PATH, USER The local variables term, path, and user are tied to the corresponding global variables. Whenever you change a shell variable, the shell will automatically update the corresponding global variable. set term=hello echo $term; echo $TERM hello hello setenv TERM vt100 echo $term; echo $TERM hello vt100 setenv TERM hello set term=vt100 echo $term; echo $TERM vt100 vt100 3) mail vs. MAIL They are not connected. Commands That Are Built Into the Shell When you enter a command, the shell will break the command line into parts. The first part of each command is the name, the other parts are options and parameters. Some commands are internal to the shell. The shell can interpret these commands directly. The Number of Internal Commands in Each Shell Name of Shell Bourne 32 Korn 43 Bash 50 C-Shell 52 Tcsh 56 Zsh 73 Internal Commands

ACS-294-001 Unix (Winter Term, 2016-2017) Page 30 The Search Path If a command is not built into the shell, the shell must find the appropriate program to execute. The path variable tells the shell where to look for programs. The value of path is a list of directory names called the search path. When the shell is looking for a program to execute, it checks each directory in the search path in the order they are specified. set path = (. /usr/local/bin /usr/bin ~/bin ) bin is often used to indicate a directory that holds programs. Tilde (~) stands for the name of your home directory. Dot (.) is the current working directory. The shell automatically copies the value of path to PATH. Setting Up History Substitution: history History Substitution A feature that lets you change and re-enter a previous command without having to retype it. At all times, the shell saves your commands in a list called the History List. Each command is given an identification number (starting at 1). Whenever you enter a command, the identification number increases by 1. You can determine how long the history list should be by setting the shell variable history. To display the history list, use the history command. The syntax is: history [-r] [number]

ACS-294-001 Unix (Winter Term, 2016-2017) Page 31 history 6 22 ls 23 date 24 w 25 who 26 finger sliao 27 history 6 history -r 3 28 history -r 3 27 history 6 26 finger sliao Event Number In C-Shell, past commands are referred to as Events. The number that identifies each command is called an Event Number. You can display the event number within a prompt by using an exclamation mark (!). Whenever the shell displays the prompt, it will replace the! with the current event number. set prompt='pearl [\!]% ' pearl [34]% uptime 13:46:17 up 12 days, 23:16, 1 user, load average: 0.00, 0.01, 0.05 pearl [35]% Using History Substitution The C-Shell supports a large variety of complex substitutions.!! Re-use the previous command, exactly as you typed. pearl [36]% uptime 13:47:05 up 12 days, 23:17, 1 user, load average: 0.00, 0.01, 0.05 pearl [37]%!! uptime 13:47:08 up 12 days, 23:17, 1 user, load average: 0.00, 0.01, 0.05 pearl [38]%

ACS-294-001 Unix (Winter Term, 2016-2017) Page 32! event_number To re-use an older command, use an! followed by the event number for that command. pearl [38]% history 3 36 uptime 37 uptime 38 history 3 pearl [39]%!37 uptime 13:47:40 up 12 days, 23:17, 1 user, load average: 0.00, 0.01, 0.05 pearl [40]% It is also possible to add characters to the end of a command. pearl [40]% history 3 38 history 3 39 uptime 40 history 3 pearl [41]%!39 calendar uptime calendar 13:48:26 up 12 days, 23:18, 1 user, load average: 0.00, 0.01, 0.05 pearl [42]% ^^ To replace a string of characters in the previous command, type ^, followed by the characters you want to replace, followed by another ^, followed by the new characters. pearl [42]% historry 3 historry: Command not found. pearl [43]% ^rr^r history 3 41 uptime calendar 42 historry 3 43 history 3 pearl [44]%

ACS-294-001 Unix (Winter Term, 2016-2017) Page 33!pattern To re-use a command that begins with a particular pattern, enter! followed by that pattern. pearl [46]% history 2 45 uptime 46 history 2 pearl [47]%!u uptime 13:50:17 up 12 days, 23:20, 1 user, load average: 0.00, 0.01, 0.05 pearl [48]%!?pattern? You can also reference a command by specifying a pattern within question marks(?). The shell will execute the last command that contained this pattern. pearl [51]% history 4 48 date 49 uptime 50 finger 51 history 4 pearl [52]%!?ti? uptime 13:51:25 up 12 days, 23:21, 1 user, load average: 0.00, 0.01, 0.05 pearl [53]%!* This combination stands for everything on the command line after the name of the command. pearl [54]% ls -l calendar mbox -rw-rw-r-- 1 sliao sliao 68 Dec 14 15:40 calendar -rw------- 1 sliao sliao 1164 Dec 17 12:36 mbox pearl [55]% ls!* ls -l calendar mbox -rw-rw-r-- 1 sliao sliao 68 Dec 14 15:40 calendar -rw------- 1 sliao sliao 1164 Dec 17 12:36 mbox pearl [56]%