System Programming. Unix Shells

Similar documents
5/20/2007. Touring Essential Programs

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

ACS Unix (Winter Term, ) Page 21

Chapter 9. Shell and Kernel

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.

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 UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

The Online Unix Manual

System Programming. Introduction to Unix

Assignment clarifications

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

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

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

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

Operating Systems. Copyleft 2005, Binnur Kurt

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

Unix Shells and Other Basic Concepts

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

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

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

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

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.

EECS2301. Lab 1 Winter 2016

Operating Systems, Unix Files and Commands SEEM

Vi & Shell Scripting

Implementation of a simple shell, xssh

CHAPTER 2 THE UNIX SHELLS

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

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

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

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

Work Effectively on the Command Line

Introduction to Linux

85321, Systems Administration Chapter 6: The shell

UNIX shell scripting

CST Algonquin College 2

System Programming. Session 6 Shell Scripting

Introduction to UNIX Part II

Shells and Shell Programming

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

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

Introduction to UNIX Shell Exercises

Computational Physics Operating systems

Shells and Shell Programming

Introduction to the Shell

Linux & Shell Programming 2014

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

More on Unix Shell SEEM

Week 5 Lesson 5 02/28/18

5/8/2012. Exploring Utilities Chapter 5

Introduction to Linux

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

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02

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

PROGRAMMAZIONE I A.A. 2015/2016

Environment Variables

Implementation of a simple shell, xssh

Introduction. Let s start with the first set of slides

Chapter-3. Introduction to Unix: Fundamental Commands

CSE II-Sem)

Last Time. on the website

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

COMP 4/6262: Programming UNIX

Practical Session 0 Introduction to Linux

Linux Command Line Interface. December 27, 2017

Introduction: What is Unix?

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Environment Variables

CS 307: UNIX PROGRAMMING ENVIRONMENT REVIEW FOR EXAM 1

UNIX Kernel. UNIX History

Bash Programming. Student Workbook

Introduction to Shell Scripting

Introduction to Linux

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

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

UNIX Shell Programming

UNIX System Programming Lecture 3: BASH Programming

C Shell Tutorial. Section 1

27-Sep CSCI 2132 Software Development Lab 4: Exploring bash and C Compilation. Faculty of Computer Science, Dalhousie University

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

COMS 6100 Class Notes 3

Lecture #10 - Interactive Korn Shell (Chapter 11)

UNIX 2 OPERATING SYSTEM

The Unix Shell & Shell Scripts

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

SOFTWARE ARCHITECTURE 3. SHELL

15-122: Principles of Imperative Computation

CSC UNIX System, Spring 2015

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

CHAPTER 3 SHELL PROGRAMS: SCRIPTS

Introduction to Linux Part 2b: basic scripting. Brett Milash and Wim Cardoen CHPC User Services 18 January, 2018

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Overview of the UNIX File System

RH033 Red Hat Linux Essentials

Linux shell programming for Raspberry Pi Users - 2

Topic 2: More Shell Skills

Scripting Languages Course 1. Diana Trandabăț

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Introduction to Linux

Transcription:

Content : Unix shells by Dr. A. Habed School of Computer Science University of Windsor adlane@cs.uwindsor.ca http://cs.uwindsor.ca/ adlane/60-256

Content Content 1 Introduction 2 Interactive and non-interactive shells login and non-login shells Exiting a shell 3 Definition Environment and Shell variables The global/local variables relationship Getting and setting variables 4 5 csh startup files tcsh startup files bash startup files 6

Introduction What is a shell? A shell is a program that acts as an interface between a user and the operating system. A Unix shell is basically a command interpreter. A Unix shell interprets commands that are either typed-in (keyboard) or in a script-file.

Running shells Introduction Running shells A shell can run as: a login shell: it is the first shell that runs as you login. a non-login shell: it is a shell that the user, a script-file or a program executes after login an interactive shell: it is a shell that waits for user input through a command line. a non-interactive shell: it is responsible of executing a shell script and does not wait for user input.

Some existing shells Many shells exist. They differ by their syntax and by the functionalities that they offer. Some existing shells All shells are normally located in /usr/bin/ or /bin/: Bourne shell: sh is the most basic shell. C shell: csh richer than sh, syntax to C. Korn shell: ksh derived from sh with more functions. TC shell: tcsh derived from csh with more functions. Bourne Again Shell: bash compatible with sh and ksh.

Interactive vs. non-interactive shells Interactive and non-interactive shells login and non-login shells Exiting a shell An interactive shell An interactive shell 1 reads a special startup files that contain some initialization information. 2 displays a prompt and waits for user commands then, 3 the shell executes the user s command and returns to step 2 unless it is terminated. A non-interactive shell In general, a non-interactive shell is present to run a script-file. The first line of a script-file indicates the shell that will run the script. A script-file starting with the line: #!/usr/bin/csh will automatically run under the C shell

The login shell Introduction Interactive and non-interactive shells login and non-login shells Exiting a shell The login shell Your login shell it is the first shell that runs as you login to your account on the computer. This shell is interactive. The Unix system administrator chooses a shell for each user when the account is created. This is your login or default shell. The name of your login shell program can be found in the corresponding line in /etc/passwd.

A non-login shell Introduction Interactive and non-interactive shells login and non-login shells Exiting a shell A non-login shell can be either interactive or non-interactive: non-login non-interactive shell An example of a non-login non-interactive shell is one that executes a script-file as discussed in the previous slides. non-login interactive shell An example of a non-login interactive shell is one that is started from the command line by typing its name. For example, to run C shell, type: csh

Changing your login shell Interactive and non-interactive shells login and non-login shells Exiting a shell Changing your login (or default) shell You can change your login shell by: changing your entry in the passwd file (you need to be a super-user) using the chsh command (not always available). The passwd command can also be used (with appropriate options). The new shell must be a valid shell, i.e. defined in the file /etc/shells. This file must be defined by the super-user.

Exiting a shell Introduction Interactive and non-interactive shells login and non-login shells Exiting a shell Because the shell accepts commands from the keyboard, it terminates when the end-of-file character CTRL-D is entered. The exit command allows also to exit a shell. When the user logs out from the system When the user is logging out from the system, the shell reads and executes commands from a file (if the file exists). For example: csh and tcsh execute the file /.logout bash executes /.bash logout

: definition Definition Environment and Shell variables The global/local variables relationship Getting and setting variables A Unix variable allows passing information from the shell to programs. Its value is a string or a set of strings. Variable names are case sensitive. A variable name may contain up to 20 characters (numbers, letters and the underscore character). A program looks for a particular variable and uses its value. A variable can be set either by the system, the user (through a command-line), the shell, a program.

Environment and Shell variables Definition Environment and Shell variables The global/local variables relationship Getting and setting variables Environment and Shell variables are, in general, used to customize the environment in which your shell runs. Most of these variables are initialized by a start-up file. Standard are split into two categories, 1 Shell variables are local variables. They apply only to the current instance of the shell and are used to set short-term working conditions. By convention, they are named in lower case characters. 2 Environment variables are global variables. They have a farther reaching significance, and those set at login are valid for the duration of the session. By convention, they are named in upper case characters.

Definition Environment and Shell variables The global/local variables relationship Getting and setting variables Example of environment (global) variables OSTYPE: the name of the current operating system USER: your login name LOGNAME: the login-name of the user HOME: the path name of your home directory MAIL: the path to your mail inbox HOST: the name of the computer you are using DISPLAY: the name of the computer screen display PRINTER: the default printer EDITOR: the default text editor PATH: the directories the shell should search to find a command SHELL: the full pathname of the login shell

Example of shell (local) variables Definition Environment and Shell variables The global/local variables relationship Getting and setting variables argv: an array of command/program arguments cwd: the full pathname of the current directory gid: the user s real group ID history: indicates the number of history events to save. home: initialized to the home directory of the invoker. owd: the old (pr previous) working directory path: a list of directories to look for executable commands prompt: the string printed before reading a command shell: the full pathname of the running shell term: the terminal type tty: the name of the tty (TeleTYpe) terminal uid: the user s real user ID user: the user s login name

Definition Environment and Shell variables The global/local variables relationship Getting and setting variables The global/local variables relationship (in general) In general In general, environment and shell variables that have the same name (apart from the case) are distinct and independent. Possibly, these variables can have the same initial values.

Definition Environment and Shell variables The global/local variables relationship Getting and setting variables The global/local variables relationship (special cases) There are, however, some exceptions to the general case: HOME, USER and TERM PATH Every time the shell variables home, user and term are changed, the corresponding environment variables HOME, USER and TERM receive the same values. However, changing the environment variables has no effect on the corresponding shell variables. PATH and path specify directories to search for commands and programs. Both variables always represent the same directory list, and altering either automatically causes the other to be changed.

Getting the value of a variable Definition Environment and Shell variables The global/local variables relationship Getting and setting variables The value of an environment or shell variable is either a string or a set of strings. Regardless of which shell is running, getting the value of a variable is possible by placing the $ sign at the beginning of the variable name. e.g.: $VARIABLE Regardless of the running shell, you can also get the value of the variable by using. e.g.: ${VARIABLE} which is particularly useful if the variable name is immediately followed by other text.

Examples Introduction Definition Environment and Shell variables The global/local variables relationship Getting and setting variables Example Determining your login shell The pathname of the login shell is stored in the shell variable SHELL. You get your login shell by typing echo $SHELL /bin/tcsh Example echo My shell saves $history events My shell saves 32 events echo My shell does not save ${history}00 events My shell does not save 3200 events

Setting a variable Introduction Definition Environment and Shell variables The global/local variables relationship Getting and setting variables Unlike getting the value of a variable, setting a variable differs from one shell to another. For example, to define a variable COLOR: using Bourne shell (sh): COLOR=green By default, variables in this shell are local. To turn a local variable global, use: export COLOR using C shell and TC shells: setenv COLOR green setenv is used for global (environment) variables. For local (shell) variables, use: set color=green Note the difference in the syntax of setenv and set.

How to unset a variable Definition Environment and Shell variables The global/local variables relationship Getting and setting variables Syntax in csh and tcsh: unset [var-name-list], for local variables Or, unsetenv [var-name-list], for environment variables

Listing the values of all variables Definition Environment and Shell variables The global/local variables relationship Getting and setting variables Remark Under csh and tcsh, set without arguments displays all shell variables and their values, setenv without arguments displays all environment variables and their values.

Example 1 Introduction Definition Environment and Shell variables The global/local variables relationship Getting and setting variables The following examples shows that an environment variable COLOR when set, is inherited by subsequent shells. This variable is initially undefined in all shells. Lines starting with # are comments for clarity. Example on our luna server with tcsh as a login shell luna: >setenv COLOR yellow #set COLOR as an environment variable luna: >echo $COLOR #display the value of the variable COLOR yellow luna: >tcsh #run a new instance of tcsh luna: >echo $COLOR #display the value of the variable COLOR yellow luna: >bash #run an instance of bash bash-3.00$ echo $COLOR #display the value of the variable COLOR yellow

Example 2 Introduction Definition Environment and Shell variables The global/local variables relationship Getting and setting variables The following examples shows that a shell variable color when set, is not inherited by subsequent shells. This variable is initially undefined in all shells. Lines starting with # are comments for clarity. Example on our luna server with tcsh as a login shell luna: >set color=red #set color as a shell variable luna: >echo $color #display the value of the variable color red luna: >tcsh #run a new instance of tcsh luna: >echo $color #display the value of the variable color color: Undefined variable. luna: >bash #run an instance of bash bash-3.00$ echo $color #display the value of the variable COLOR #an empty line is displayed

External vs. internal commands A shell command can be internal (built-in): the code to be executed is part of the shell. Running these commands does not require searching their path by the system. external: the code to be executed resides in a sperate binary file. Running these commands requires searching their path by the system.

External Commands PATH For an external command, the shell searches for its file in the directories whose names are stored in the variable PATH. How to define the shell variable PATH? Example: set path =./:/usr/bin: /bin the shell looks for the command in order in the current directory, called./, /usr/bin/ your-login-directory/bin/ (which is also /bin) Remark Note that after the above setting, both path and PATH contain the same string./:/usr/bin: /bin.

Modifying the PATH variable Modifying the PATH variable To add more directories to your current path by: set path = /usr/local/bin:$path You can view your path: echo $PATH The command which followed by the command-name allows you to find the file location of the command. Examples: which echo, which emacs Remark Note that PATH is set in a shell startup file, Example: in the file /.cshrc, where / represents the current user login directory.

Startup files Introduction csh startup files tcsh startup files bash startup files Most environment and shell variables are initialized by the startup files. Different shells read different startup files. Some startup files are in the directory /etc/. These are intended for system wide initialization of environment variables. You can customize your environment and shell variables by assigning different values to these variables in the startup files in your home directory (which we refer to as ).

C shell (csh) startup files csh startup files tcsh startup files bash startup files Whenever invoked When csh is invoked, it first executes the commands in the system-wide startup file, usually located in /etc/csh.cshrc. Then, csh reads and executes a script named /.cshrc. Invoked as a login shell In addition to the above files, tcsh reads and executes the files /etc/csh.login and /.login once only at login.

TC shell (csh) startup files csh startup files tcsh startup files bash startup files Whenever invoked When tcsh is invoked, it first executes the commands in the system-wide startup file, usually located in /etc/csh.cshrc. Then, tcsh reads and executes a script named /.tcshrc. If /.tcshrc is not found, then it executes /.cshrc. Invoked as a login shell In addition to the above files, tcsh reads and executes the files /etc/csh.login and /.login once only at login.

csh startup files tcsh startup files bash startup files Bourne Again shell (bash) startup files Invoked as a login shell bash attempts to read several initialization files: First, it reads and executes commands in /etc/profile Then, it reads and executes commands from the first one of the files /.bash profile, /.bash login and /.profile Invoked as an interactive non-login shell bash reads and executes commands from /.bashrc. Invoked as a non-interactive shell When a non-interactive bash is started, bash reads and executes commands in the file named by the variable $BASH ENV if defined.

Example of /.cshrc in luna csh startup files tcsh startup files bash startup files Example # @(#)cshrc 1.11 89/11/29 SMI umask 077 set path=(/bin /usr/bin /usr/ucb /etc.) if ( $?prompt ) then set history=32 endif if ( -f /etc/system.cshrc) then source /etc/system.cshrc endif

... These are special characters with special meanings: >: Output redirection E.g., ls > filename.txt <: Input redirection E.g., mailx user@uwindsor.ca < letter.txt >>: Output redirection, appends to a file E.g., ls >> filename.txt *: filename Wild card, matches 0 or more characters E.g., rm *ps, delete all files ending with ps.?: filename Wild card, matches 1 character. E.g., rm *.? delete files with one character after. ls?? list files/directories made up of 2 characters.

(cont d) command : command substitution, replaced by the command output. E.g. 1. echo The date of today is date E.g. 2. echo hello ls hello ls echo hello ls hello followed by the ls outputs. : Pipe between two commands. E.g., ls wc -w output of ls is piped to wc to get the number of files/directories. Note the utility wc displays a count of lines, words and characters in a file. ;: Used to sequence commands E.g., date ; ls; date : Executes a command if the previous one fails. E.g., cc prog1.c CC prog1.c gcc prog1.c

(cont d) && : Executes a command if the previous one succeeds E.g., CC prog1.c -o prog1 &&./prog1 &: Executes a command in the background E.g., netscape & #: characters after this are ignored (comment) $: Expands the value of a shell variable E.g., echo $PATH \: Prevents special interpretation of next character. E.g., echo this is \& this is & & is not a metacharacter in this case.