Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.

Slides:



Advertisements
Similar presentations
CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
Advertisements

A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 4: More Command Line Interface (CLI) Chapter 7: The Linux Shell By Fred R. McClurg Linux.
Lesson 10-Controlling User Processes. Overview Managing and processing processes. Managing jobs. Exiting/quitting when jobs have been stopped.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
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 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.
CS 497C – Introduction to UNIX Lecture 20: - The Shell Chin-Chih Chang
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
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
UNIX command line. In this module you will learn: What is the computer shell What is the command line interface (or Terminal) What is the filesystem tree.
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
Linux in More Detail Shirley Moore CPS5401 August 29,
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.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
An Introduction to UNIX System --- Cosc513 Presentation n Instructor: Morteza Anvari n Author: Yonghong Pan n ID#: n Date: Jan.29, 2001.
An Introduction to Unix Shell Scripting
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.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
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.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
UNIX and Shell Programming (06CS36) Unit 1 Continued… Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S. Gogte Institute of.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux+ Guide to Linux Certification, Third Edition
CS465 - UNIX The Bourne Shell.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Lesson 9-Setting and Using Permissions. Overview Describing file permissions. Using execute permissions with a file. Changing file permissions using mnemonics.
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.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
Lesson 4-Mastering the Visual Editor. Overview Introducing the visual editor. Working in an existing file with vi. Understanding the visual editor. Navigating.
Introduction to Programming Using C An Introduction to Operating Systems.
Chapter Six Introduction to Shell Script Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.
Linux+ Guide to Linux Certification, Second Edition
Lesson 6-Using Utilities to Accomplish Complex Tasks.
Agenda Managing Processes (Jobs) Command Grouping Running jobs in background (bg) Bringing jobs to foreground (fg), Background job status (jobs) Suspending.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Linux Administration Working with the BASH Shell.
Implementation of a simple shell, xssh
Lesson 5-Exploring Utilities
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
Implementation of a simple shell, xssh (Section 1 version)
Implementation of a simple shell, xssh
Basic UNIX OLC Training.
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
Chapter 3 The UNIX Shells
LPI Linux Certification
Presentation transcript:

Lesson 8-Specifying Instructions to the Shell

Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing the functioning of the shell. Employing advanced user features.

An Overview of Shell A shell interprets and executes the syntax of the command- lines in a specific way. The kernel is the core program of UNIX/Linux, which schedules processes, allocates memory, and handles input/output and other peripherals. User cannot directly communicate with the shell.

The shell interacts with the kernel to execute a request. The shell is the middleman between the user and the kernel. A shell translates a user’s requests into kernel calls. The login shell is started when a user logs in and exits when the user logs out. An Overview of Shell

A shell is the interface between the user, utilities, the file system, and the kernel. The shell’s primary function is to read the command-line, examine its component, and interpret it according to its rules. The shell performs the given task and returns the prompt for further requests.

Execution of Commands in a Shell Interacting with the shell. Communicating with the shell. Identifying utilities for output redirection. Identifying utilities in pipelines. Starting processes to run utilities. Redirecting input and output.

Execution of Commands in a Shell Passing arguments to processes. Identifying tokens on the command-line. The exit code status after a utility execution. Using the model to interpret command-lines. Changing the behavior of a command execution.

Interacting with the Shell Entering a command from the keyboard is the basic way of communicating with the shell. For each utility requested by the user, the shell starts a new child process to execute the code of that utility. The child process inherits the environment variables like pid, user, etc. The “ps” utility can be used for obtaining the process identification numbers.

Communicating with the Shell The shell proceeds through a series of specific steps after a user issues commands. The complete command-line is first interpreted by the shell. The shell interprets the ENTER key as the completion of a command. The shell interprets “\” as an instruction not to interpret the special meaning of the single character that immediately follows it.

Communicating with the Shell The commands entered at the shell prompt usually include several words or tokens. The shell interprets some tokens as utilities and others as filenames. The command line interprets the “>”, “|”, and “<“ as special characters that control the input and output of a file.

The shell uses white space to identify the words or tokens of a command-line. The “$” sign is recognized by the shell as the start of a new variable. Communicating with the Shell

Identifying Utilities for Output Redirection The shell interprets the first word in the command-line of the shell as a utility. In a C shell, when assigning value to variables, it should not include any spaces around the “=“ sign. The value of a set variable can be displayed by adding a $ sign ahead of the variable name. The shell interprets the token following the pipe as a utility and the token following the redirection operator as a file.

Identifying Utilities in Pipelines A pipeline is a set of one or more utilities that handle data independently. The “semicolon” can be used to indicate the end of one pipeline. A shell can run one pipeline after another on a single command-line by separating them with semicolons. The first token after the semicolon begins a new pipeline, and hence must be a utility.

Identifying Utilities in Pipelines The logical AND (&&) operator can instruct the pipeline to run the next utility based on the success or failure of the preceding pipeline. The command-line is successful only if both are executed. A token following the && operator is interpreted as a utility. The logical OR (||) operator executes only one of the two utilities in the command-line.

Identifying Utilities in Pipelines The shell can easily interpret a variable in all tokens since a $ sign precedes them. The “–x” option tells the shell to explain how it interprets the command-line before executing it.

Starting Processes to Run Utilities The shell is an active process and runs in the foreground. The resources allocated to a running process are called process space or process image. The shell makes an exact copy of the process space, including environment variables, when running a utility.

Starting Processes to Run Utilities A new child process space is an exact copy of the shell. The child process inherits the input, output, error destination, and variable information from the parent.

Redirecting Input and Output The shell interprets the > as an instruction to redirect the output from the screen to a file. When redirecting output to a file, a new file is created, depending on the shell and the noclobber variable set. Existing files can be protected by setting the noclobber variable.

Redirecting Input and Output The csh shell can be instructed to overwrite an existing file by placing an exclamation point (!) before the redirection symbol (>). In bash and ksh shells, the pipe following the redirect is an instruction to overwrite even if noclobber is set off. The “–i” option, when used with the mv and cp utilities, protects files from accidental removal. A utility, uses the keyboard as default input.

Redirecting Input and Output The input, the output and the error files all are connected to the default output, the monitor. An error message is displayed on the screen if a command is not able to execute. An error message can be redirected to a file by using the “2>” and a filename to the command-line.

The bash, ksh, and sh uses “>” or “1>” to redirect output to a file. The standard error and output can both be redirected to the same file using the “>&” and specifying the filename in the command line. Redirecting Input and Output

Passing Arguments to Processes The “ls” utility interprets an argument as a file if it is not preceded by a minus sign. Any tokens left over on the command-line when the shell has completed the interpretation are passed as an argument to the associated utility. The shell also interprets command options as arguments. Two or more option flags can be specified on the command- line as one argument.

Identifying Tokens on the Command-Line The “*” wild card character instructs the shell to include all the filenames. The C shell variable path and the family variable PATH contain a list of directories that the respective shells search to locate the code for each requested utility. The /bin directory usually contains all the executables.

Identifying Tokens on the Command-Line The strings utility ignores all machine code and outputs only the strings of ASCII characters that it finds. The shell does not search the path to locate the utility if the absolute path of the utility is specified.

The Exit Code Status After a Utility Execution The shell interprets the variable “?” as the exit code of the last process. Exit codes other than zero are error codes. Every time a process completes its execution and exits, it informs its parent about the status of the exit code.

Using the Model to Interpret Command-Lines When redirecting output to a file and if the noclobber variable is set to off, no error message is displayed when the file is being overwritten. A utility that requires a filename as an argument will start reading from the input if the filename is not specified. A dash (-) argument instructs the sort utility to read from the input.

Changing the Behavior of a Command Execution When the shell executes a child process in the foreground, it waits for a child process to complete execution and then displays the prompt. A command consisting of utilities, arguments, and redirection terminated by ENTER is called a job. A job can be placed in the background by appending it with the & sign.

Changing the Behavior of a Command Execution The ps command can be used for listing all current processes. A current process in the foreground can be suspended by pressing CTRL-Z. The csh, tcsh, bash, and ksh shells allow a user to suspend a job midstream and return to it later. The fg command allows a job to be brought back to the foreground.

Shell Command-Line Expansion Using shell characters to expand filenames. Creating and using local variables. Passing environment variables to child processes.

Using Shell Characters to Expand Filenames Some characters are interpreted by the shell as wildcard characters, while others can be used for specifying a range of characters. The filename expansion of the filename-matching feature allows the selection of many filenames while entering only one name with special characters embedded. The * and ? are interpreted by the shell as special characters.

Using Shell Characters to Expand Filenames The asterisk (*) character can be used for matching any number of characters, while the question mark (?) is used only for matching a single character. Shell variable names and values are stored in the memory and are hence available regardless of the directory location.

Using Shell Characters to Expand Filenames The shell also allows a range of letters or characters to be specified with the help of square brackets. The curly brace characters, “{“ and “}”, are also used by the bash shell and modern ksh shells for matching and creating multiple filenames from one pattern. The curly braces match existing filenames if each match is specified in the braces, but does not expand ranges.

Creating and Using Local Variables Local and environmental are the two different kinds of variables identified by the shell. The “set” or “env” command lists the variables that are set in the shell’s memory. In a csh or tcsh shell, the set command is used for declaring a variable and assigning a value to it.

Creating and Using Local Variables In ksh, bash, or sh shell, a variable is directly defined and assigned a value without the set command. The shell interprets the $ character as an instruction to locate in the shell’s memory a variable that has the name of the character string that follows the $. The variable must be enclosed in single quotes if it includes any spaces.

Passing Environment Variables to Child Processes There are two types of variables - the local variable and the global variable. The local variables of a shell are not passed to a child process. In a C shell, the “setenv” command is used for setting an environmental variable. The set, env, or “printenv” commands can be used for listing the environmental variables.

Passing Environment Variables to Child Processes The “unset” command can be used for removing a local variable. An environmental variable can be removed with the help of the “unsetenv” command. The “export” command is used for making a local variable available to a child process.

Passing Environment Variables to Child Processes An environmental variable modified by the child process is not reflected in the parent’s environmental variables. The shell also allows a variable to be created and exported at the same time. The variables set in a child process are lost once the child process exits. The child shell takes the memory of the variable when it exits.

Customizing the Functioning of the Shell Using and modifying the search path. Creating personalized shell prompts.

Using and Modifying the Search Path The “path” or “PATH” variable is searched when a user requests for a utility. The path is a local variable and is usually assigned a value in the startup script. The C shell also maintains a PATH environmental variable, which also holds the path, and passes it to child processes. In the C shell, two variables are intertwined where change in one is automatically reflected in another.

Using and Modifying the Search Path A single dot is used for denoting the current directory and can be set in the path variable. A colon (:) at the beginning or the end of a path string is interpreted by the sh family of shells as an instruction to search the current directory. An empty field using two colons (::) can explicitly request the current directory, anywhere in the path, using a dot.

Creating Personalized Shell Prompts In a tcsh or csh shell, the prompt variable is used for modifying the display prompt. The “man” and “info” pages describe the collection of variables that can be used in constructing a prompt. The ksh and the bash shells use the value in the variable PS1 as its prompt.

Employing Advanced User Features Completing filenames: The variable filec in a tcsh shell, when set in the environment, instructs the shell to search for matching filenames. When a shell cannot distinguish between two existing files, it either displays all matching files or simply flashes or produces beeps. Filename completion can also be used for files, directories, and executables.

Employing Advanced User Features Completing filenames (continued): The filename-completion variable can be set in the Korn shell by executing either the “set –o vi” or the “set –o vi- tabcomplete” command. The “set –o vi” or “set +o posix” commands can be used for turning on the file-completion feature if it is not working. Many C shells include filename completion, but use the ESC key to trigger completion of filenames.

Employing Advanced User Features Evaluating shell variables: The bash and ksh shells also provide built-in variables that are useful in interacting with the shell. The SECOND shell variable can be used for determining the number of seconds since the shell was started. In a bash shell, the PROMPT_COMMAND variable allows a user to execute any command just before it displays the prompt. A dot file is a run-control file for a specific utility or shell.

Employing Advanced User Features Customizing shell startup files: The csh shell can be customized with the help of the.cshrc file in the /etc directory since it is always read at startup. The bash shell reads the file.bashrc whenever it starts.

Employing Advanced User Features Customizing shell startup files (continued): A system setup to start a ksh file reads the.kshrc file at startup. The.kshrc file is not read if the ENV environmental variable is not set. The ksh shell is programmed to read at startup whatever file is the value of the ENV variable.

Summary All shells accomplish the primary task of interpreting the commands issued by a user. A shell process executes code that resides in a file in a system directory. A child process started by the shell for each utility execution inherits the input, output, and error destinations, as well as environmental variables.

Summary Redirecting input and output from the default destination to files and other utilities is one of the functions of a shell. A command-line inside back quotes is interpreted and executed as a complete command line. Local variables are not passed to child processes, while environmental variables are passed to child processes.

The most recent shells include file completion, which allows us to type part of a file, directory, or utility name. A user can place instructions in the startup files to tailor how the shell functions. Summary