Xuan Guo Chapter 4 The UNIX Shells Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003. Original Notes.

Slides:



Advertisements
Similar presentations
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
Advertisements

Lesson 10-Controlling User Processes. Overview Managing and processing processes. Managing jobs. Exiting/quitting when jobs have been stopped.
CHAPTER 2 THE UNIX SHELLS by U ğ ur Halıcı. layers in a unix system 1 Users Standard utility programs (shell, editors, compilers, etc.) Standard utility.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
CS 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
The UNIX Shells 1. What is a Unix shell? 2. A few common shells in the Unix & Linux. A. Bourne shell B. Korn shell C. C shell D. Bash-the default shell.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Chapter 4 The UNIX Shells (Bourne shell, Korn shell, C shell)‏ Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
UNIX Processes. The UNIX Process A process is an instance of a program in execution. Created by another parent process as its child. One process can be.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Introduction to Shell Script Programming
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II.
An Introduction to Unix Shell Scripting
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 8 Shell.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
Linux+ Guide to Linux Certification, Third Edition
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
CS465 - UNIX The Bourne Shell.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes.
The kernel considers each program running on your system to be a process A process lives as it executes, with a lifetime that may be short or long A process.
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.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Linux Commands C151 Multi-User Operating Systems.
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
Linux+ Guide to Linux Certification, Second Edition
Chapter 4 The UNIX Shells (Bourne shell, Korn shell, C shell)‏ Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Introduction to Shells
The UNIX Shell Learning Objectives:
System Programming and administration CS 308
Shell Script Assignment 1.
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
Chapter 4 The UNIX Shells (Bourne shell, Korn shell, C shell)‏
Chapter 3 The UNIX Shells
Presentation transcript:

Xuan Guo Chapter 4 The UNIX Shells Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original Notes by Raj Sunderraman Converted to presentation and updated by Michael Weeks and modified by Xuan Guo CSC

Xuan Guo The Relationship of shell functionality Common core Bourne shell Korn shell Common core C shell CSC

Xuan Guo Which Shell To change your default shell use the chsh utility $chsh Old shell: /bin/sh New shell: /bin/ksh $^D To examine your default shell, type: –echo $SHELL echo $SHELL /bin/bash CSC

Xuan Guo CORE Shell Functionality Built-in commands Scripts Variables (local, environment) ‏ Redirection Wildcards CSC

Xuan Guo CORE Shell Functionality Pipes Sequences (conditional, unconditional) ‏ Subshells Background processing Command substitution CSC

Xuan Guo Invoking the Shell A shell is invoked, either –automatically upon login, or –manually from the keyboard or script CSC

Xuan Guo What does the shell do? The following takes place: –(1) reads a special startup file (.cshrc for csh in the user's home directory) and executes all the commands in that file –(2) displays a prompt and waits for a user command –(3) If user enters CTRL-D (end of input) the shell terminates, otherwise it executes the user command(s) ‏ CSC

Xuan Guo User Commands ls (list files), ps (process info), \ continues line, lp (send to printer) ‏ $ ls $ ps -ef | sort | grep cron $ ls | sort | \ grep cron CSC

Xuan Guo Built-in commands Most Unix commands invoke utility programs stored in the file hierarchy (e.g. ls, vi etc) ‏ The shell has to locate the utility (using PATH variable) ‏ Shells have built-in commands, e.g: –echo –cd CSC

Xuan Guo Built-in commands $ echo [options]... [string]... $ echo Hi, How are you? –Hi, How are you? echo by default appends a new line ‏ -n do not appends a new line cd dir CSC

Xuan Guo Metacharacters Output redirection > writes standard output to file >>appends standard output to file Input redirection <reads std. input from file <<tokread std. input until tok CSC The shell redirection facility allows you to store the output of a process to a file use the contents of a file as input to a process

Xuan Guo Metacharacters File-substitution wildcards: –*matches 0 or more characters –?matches any single character –[...]matches any character within brackets Command substitution: –`command` replaced by the output of command –e.g. echo `ls` CSC

Xuan Guo Metacharacters | Pipe send output of one process to the input of another –e.g. list files, then use wordcount to count lines ls | wc -l –this effectively counts the files CSC

Xuan Guo Metacharacters ; Used to sequence commands Conditional execution –|| execute command if previous one fails –&&execute command if previous one succeeds CSC

Xuan Guo Metacharacters (...)Group commands &Run command in background # Comment –rest of characters ignored by shell $Expand the value of a variable \ Prevent special interpretation of character that follows CSC

Xuan Guo Filename substitution $ ls *.c # list.c files $ ls ?.c # list files like a.c, b.c, 1.c, etc $ ls [ac]* # list files starting with a or c $ ls [A-Za-z]*# list files beginning with a letter $ ls dir*/*.c # list all.c files in directories starting with dir CSC

Xuan Guo Pipes $ command1 | command2 | command3 $ ls ppp00* ppp24* ppp48* ppp72* $ ls | wc -w 4 CSC

Xuan Guo Pipes $ head -4 /etc/passwd root:fjQyH/FG3TJcg:0:0:root:/root:/bin/sh bin:*:1:1:bin:/bin: daemon:*:2:2:daemon:/sbin: adm:*:3:4:adm:/var/adm: $ cat /etc/passwd | awk -F: '{print $1}' | sort adm bin daemon raj CSC

Xuan Guo tee utility $ tee [options] {fileName}+ causes standard input to be copied to file and also sent to standard output. –-a option appends to file –-i option ignores interrupts (kill –l) CSC SIGINT2Issued if the user sends an interrupt signal (Ctrl + C). SIGQUIT3Issued if the user sends a quit signal (Ctrl + D).

Xuan Guo tee utility $ who raj tty1Jun 19 09:31 naveenttyp0Jun 19 20:17 (localhost)‏ $ who | tee who.capture | sort naveenttyp0Jun 19 20:17 (localhost)‏ rajtty1 Jun 19 09:31 $ more who.capture raj tty1Jun 19 09:31 naveenttyp0Jun 19 20:17 (localhost)‏ CSC

Xuan Guo Command Substitution A command surrounded by grave accents (`) is executed and its standard output is inserted in the command's place in the command line. $ echo today is `date` today is Sat Jun 19 22:23:28 EDT 2007 $ echo there are `who | wc -l` users on the system there are 2 users on the system CSC

Xuan Guo Sequences Commands or pipelines separated by semi- colons Each command in a sequence may be individually I/O redirected. Example: –$date; pwd; ls –$date > date.txt; pwd > pwd.txt; ls CSC

Xuan Guo Sequences $ date; pwd; ls Sat Jun 19 22:33:19 EDT 2007 /home/raj/oracle jdbc/ ows/ proc/ sql/ sqlj/ who.capture $ date > date.txt; pwd > pwd.txt; ls date.txt jdbc/ ows/ proc/ pwd.txt sql/ sqlj/ who.capture CSC

Xuan Guo Sequences Conditional sequences: $ cc myprog.c && a.out $ cc myprog.c || echo compilation failed In a series of commands separated by &&, the next command is executed if the previous one succeeds (returns an exit code of 0) ‏ In a series of commands separated by || the next command is executed if the previous one fails (returns an exit code of non-zero) ‏ CSC

Xuan Guo Grouping commands Commands can be grouped by putting them within parentheses –a sub shell is created to execute the grouped commands Example: $ (date; ls; pwd) > out.txt $ more out.txt CSC

Xuan Guo Grouping commands $ (date; ls; pwd) > out.txt $ more out.txt Sat Jun 19 22:40:43 EDT 2007 date.txt jdbc/ out.txt ows/ proc/ pwd.txt sql/ sqlj/ who.capture /home/raj/oracle CSC

Xuan Guo Background processing An & sign at end of a simple command, –or pipeline, sequence of pipelines, –or a group of commands Starts a sub-shell –commands are executed as a background process –does not take control of the keyboard A process id is displayed when it begins CSC

Xuan Guo Background processing Redirect the output to a file (if desired) ‏ –prevents background output on terminal Background process cannot read from standard input –If they attempt to read from standard input; they terminate. CSC

Xuan Guo Shell Programs/Scripts Shell commands may be stored in a text file for execution Use the chmod utility to set execute permissions on the file: chmod +x exescript Executing it by simply typing the file name When a script runs, the system determines which shell to use CSC

Xuan Guo Shell Programs/Scripts To determine which shell: –if the first line of the script is a pound sign (#) ‏ then the script is interpreted by the current shell –if the first line of the script is of the form #!/bin/sh or #!/bin/ksh etc then the appropriate shell is used to interpret the script –else the script is interpreted by the Bourne shell Note: pound sign on 1st column in any other line implies a comment line CSC

Xuan Guo Shell Programs/Scripts Always recommended to use #!pathname #!/bin/csh # A simple C-shell script echo -n "The date today is " date CSC

Xuan Guo Subshells Several ways a subshell can be created: –Grouped command (ls; pwd; date) ‏ –Script execution –Background processes A subshell has its own working directory cd commands in subshell do not change working directory of parent shell $pwd $(cd /; pwd) $pwd CSC

Xuan Guo Subshells Every shell has two data areas –environment space –local-variable space Child shell gets a copy of the parent's environment space –starts with an empty local-variable space. CSC

Xuan Guo Variables A shell supports two kinds of variables: –Local variables –Environment variables –Both hold data in string format Every shell has a set of pre-defined environment variables and local variables. Accessing variables in all shells is done by prefixing the name with a $ sign. CSC

Xuan Guo Variables Some pre-defined environment variables available in all shells: –$HOME –$PATH –$MAIL –$USER –$SHELL –$TERM showenv.sh CSC

Xuan Guo Assigning values to variables Depends on shell: sh, bash, ksh: –variable=value –variable="value" Notice no spaces around equal sign To make a variable an environment variable in sh, bash, ksh –export variable CSC

Xuan Guo Built-in Variables Common built-in variables with special meaning: $$ process ID of shell $0 name of shell script (if applicable)‏ $1..$9 $n refers to the nth command –line argument (if applicable)‏ $* a list of all command line arguments CSC

Xuan Guo Example using Built-in variables $ cat script2.sh #!/bin/bash echo the name of this file is $0 echo the first argument is $1 echo the list of all arguments is $* echo this script places the date into a temporary file called $1.$$ date > $1.$$ ls -l $1.$$ rm $1.$$ $ script2.sh paul ringo george john CSC

Xuan Guo Quoting Single quotes (') inhibit wildcard replacement, variable substitution, and command substitution Double quotes (") inhibits wildcard replacement only When quotes are nested only the outer quotes have any effect CSC

Xuan Guo Quoting Examples $ echo 3 * 4 = log 3.tex script.sh script2.sh 4 = 12 $ echo '3 * 4 = 12' 3 * 4 = 12 $ echo "my name is $USER; the date is `date`" my name is raj; the date is Sun Jun 20 21:59:13 EDT 2007 CSC

Xuan Guo Job Control ps command generates a list of processes and their attributes kill command terminates processes based on process ID wait allows the shell to wait for one of its child processes to terminate. $ Sleep seconds CSC

Xuan Guo ps Command $ ps [options] -e: include all running processes -f: include full listing -l: include long listing CSC

Xuan Guo ps Command ColumnMeaning Sthe process state UIDThe effective user ID of the process PIDThe process ID PPIDThe parent process ID CThe percentage of CPU time that the process used in the last minute PRIThe priority of the process SZThe size of the process’s data and stack in kb STIMEThe time the process was created TTYThe controlling terminal TIMEThe amount of CPU time used so far(MM:SS) CMDThe name of the command CSC

Xuan Guo ps Command LetterMeaning ORunning on a processor RRunnable SSleeping TSuspended ZZombie process CSC ps –elf|awk ‘$2==“R”{print $0}’

Xuan Guo nohup Command Bourne automatically terminates background processes when you log out (csh allows them to continue) ‏ To keep the background processes to continue in sh, use $ nohup command [Arg] & $ jobs [options] -l Only the last job to be started is printed $ jobs –l to see CSC

Xuan Guo kill $ kill [-sigspec] pid if signal is not specified the default signal is SIGTERM (15) ‏ SIGKILL (9) is useful if the process refuses to die If a workstation quits responding, try logging in from a different workstation and signaling the non-responsive process. $(sleep 20; echo done) & $kill pid CSC

Xuan Guo Signaling processes: kill $ kill -l [sigspec] $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGIOT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR CSC

Xuan Guo Waiting for child processes CSC A shell may wait for one or more of its child processes to terminate by using built-in wait command: wait [pid] $ (sleep 30; echo done 1) & [1] 429 $ (sleep 30; echo done 2) & [2] 431 $ echo done 3; wait; echo done 4 done 3 done 1 [1]- Done ( sleep 30; echo done 1 )‏ done 2 [2]+ Done ( sleep 30; echo done 2 )‏ done 4

Xuan Guo Finding a command: $PATH If the command is a shell built-in such as echo or cd it is directly interpreted by the shell. if the command begins with a / –shell assumes that the command is the absolute path name of an executable –error occurs if the executable is not found. if not built-in and not a full pathname –shell searches the directories in the PATH –from left to right for the executable current working directory may not be in PATH CSC

Xuan Guo PATH variable If PATH is empty or is not set, only the current working directory is searched for the executable. Homebrewed utilities: –Some Unix users create their own utilities –Stored in their bin directory –Place their bin directory ahead of all others –Their version of the utility is executed –PATH=/homebrewed/bin:$PATH CSC

Xuan Guo Termination and Exit codes Every Unix process terminates with an exit value By convention, a 0 value means success and a non-zero value means failure All built-in commands return 1 when they fail CSC

Xuan Guo Termination and Exit codes The special variable $? contains the exit code of the last command execution. Any script written by you should contain the exit command: $ exit [number] If the script does not exit with a exit code, the exit code of the last command is returned by default. CSC

Xuan Guo Common Core Built-in commands $ eval arguments –The eval shell command executes the output of the command as a regular shell command. $ eval `echo x=5` $ echo $x 5 CSC

Xuan Guo Common Core Built-in commands $ exec [ command ] [ Arg... ] The exec shell command causes the shell's image to be replaced with the command in the process' memory space. –As a result, if the command terminates, the shell also ceases to exist; If the shell was a login shell, the login session terminates. CSC The shell is the program which actually processes commands and returns output. A terminal refers to a wrapper program which runs a shell. Decades ago, this was a physical device consisting of little more than a monitor and keyboard.

Xuan Guo Common Core Built-in commands $ shift –This command causes all of the positional parameters $2..$n to be renamed $1..$(n-1) and $1 is lost. –Useful in processing command line parameters. CSC

Xuan Guo Common Core Built-in commands CSC $ cat script3.sh #!/bin/bash echo first argument is $1, all args are $* shift echo first argument is $1, all args are $* $ script3.sh a b c d first argument is a, all args are a b c d first argument is b, all args are b c d

Xuan Guo umask Command Every Unix process has a special quantity called umask value. –Default value: 022 octal Whenever a file is created –E.g. made by vi or by redirection –File permissions (usually 666) masked (deduct) with umask value –Example: 022 to produce the permission 644 CSC

Xuan Guo umask Command $ umask [mode] To see current umask value: –$ umask To change umask value: –$ umask octalValue CSC

Xuan Guo Review Covered core shell functionality –Built-in commands –Scripts –Variables –Redirection –Wildcards –Pipes –Subshells –Background processing CSC