Linux Shell Script Programming

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Chapter Seven Unix Shell Environments1 System Programming UNIX Shell Environments.
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
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
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.
Using Linux Commands 2 Lab#5
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
Using Linux Commands 2 Lab#5. Sort command Sort the lines of text files. $ sort fileName by default it will sort in normal order(alphabetical 0-9 A-Z.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Introduction to Shell Script 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.
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.
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.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Linux+ Guide to Linux Certification, Third Edition
Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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 11: Shell.
Workbook 6 – Part 2 The Bash Shell
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
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.
UNIX shell environments CS 2204 Class meeting 6 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Introduction to Programming Using C An Introduction to Operating Systems.
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
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.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
Linux+ Guide to Linux Certification, Second Edition
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Linux Administration Working with the BASH Shell.
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Implementation of a simple shell, xssh
UNIX To do work for the class, you will be using the Unix operating system. Once connected to the system, you will be presented with a login screen. Once.
Introduction to Shells
Andy Wang Object Oriented Programming in C++ COP 3330
Implementation of a simple shell, xssh
System Programming and administration CS 308
Some Linux Commands.
C151 Multi-User Operating Systems
Shell Script Assignment 1.
CSE 374 Programming Concepts & Tools
CHAPTER 8 (skip , ) CHAPTER 10
John Carelli, Instructor Kutztown University
UNIX Reference Sheets CSE 2031 Fall 2010.
CSE 303 Concepts and Tools for Software Development
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Review.
LPI Linux Certification
Presentation transcript:

Linux Shell Script Programming Chapter 3: Shell Basics Linux Shell Script Programming

Linux Shell Script Programming Chapter Objectives In this chapter, you will: Understand the shell login and logout files Learn about shell variables Become familiar with the shell environment Learn about shell builtin commands Additional Commands Learn about shell grammar Understand command types Linux Shell Script Programming

Understanding the Shell Login and Logout Files When you login, the shell executes and provides you with a shell prompt The default shell is the Bourne-Again Shell, or bash There are startup files in the Linux directory hierarchy used to set up the user’s environment Startup files are read and executed as the user logs in A logout file is read and and executed when a user logs out Linux Shell Script Programming

Understanding the Shell Login and Logout Files If present, the shell reads and executes the /etc/profile script file The /etc/profile script file is read and executed for each user Place commands here that you want to affect all users Place command statements at the end so you won’t interfere with existing commands The permissions on /etc/profile are read and write for root and read for all other users Linux Shell Script Programming

Understanding the Shell Login and Logout Files After the shell reads /etc/profile, for all users, ~/.bash_profile, in each user’s home directory, $HOME, is read The ~ represents each user’s own home directory Another script file, ~/.bashrc, is executed if it exists The ~/.bash_logout script file is executed as a user logs out Linux Shell Script Programming

Learning about Shell Variables A shell variable is a keyword that is set by the shell Each shell variable is used for a specific purpose A shell variable is typically entered in uppercase You precede the shell variable name with a $, as in $PATH Linux Shell Script Programming

Learning about Shell Variables Examples of shell variables: PWD – the most recent working directory set with the cd command OLDPWD – the previous working directory set with the cd command BASH – the full path of the bash shell RANDOM – generates a random number between 0 and 32,767 HOSTNAME – the current hostname of a Linux system Linux Shell Script Programming

Learning about Shell Variables Examples of shell variables IFS – Internal Field Separator, which is used as a separator between shell words PATH – contains the list of directories used to search for commands when executing them HOME – each user’s home directory, typically /home/username, where username is a user’s login name PS1 – shell prompt TMOUT – the amount of time the shell will wait without user input before exiting the shell Linux Shell Script Programming

Learning about Shell Variables Linux Shell Script Programming

Becoming Familiar with the Shell Environment When you execute a command using a partial path, the shell looks to the PATH shell variable Each directory on the path is separated by a colon Search order is left to right Search stops when a match is found If the directory is not found, then an error message is displayed Linux Shell Script Programming

Becoming Familiar with the Shell Environment When using the full path, the shell does not use the PATH variable Instead the shell goes directly to the directory using the specified Example: /bin/cp Linux Shell Script Programming

Becoming Familiar with the Shell Environment The File System Hierarchy standard was developed by several vendors Used to standardize system directories Important use was to use /usr/local/bin to store executable programs and scripts used by all users Use of $HOME/bin to execute a script or program in a user’s home directory Linux Shell Script Programming

Learning about Shell Builtin Commands A builtin command is part of the bash shell Compiled into the shell You cannot access them directly Perform man on the bash to get their manual help pages Linux Shell Script Programming

Learning about Shell Builtin Commands Examples: ./filename – where filename is a script that is read and executed cd – changes the current directory declare – defines a variable echo – displays output exec – causes a command to replace the current shell with no new PID exit – exits a shell with an exit status Linux Shell Script Programming

Learning about Shell Builtin Commands Examples: export – exports a variable to a spawned shell history – displays previous commands kill – sends a signal to a PID let – evaluates an arithmetic expression logout – exits a login shell pwd – displays current working directory read – read characters from the keyboard readonly – makes a variable readonly so it cannot be changed Linux Shell Script Programming

Learning about Shell Builtin Commands Examples: return – returns an exit status set – set and display a shell variable shift – moves positional parameters test – evaluates expressions trap – catches a signal sent to the shell Linux Shell Script Programming

Linux Shell Script Programming Additional Commands Accessed as command contained in a directory (eg. /bin, /usr/bin, etc…) Examples: Shell Programs: sh, csh, ksh, bash Directories / Files: mkdir, rmdir, rm, cd, ls, pwd, cp, mv Files: cat, lpr, touch, more, vi, uniq, sort, grep, cut, paste, diff Misc: cal, date, echo, history, man, who Linux Shell Script Programming

Learning about Shell Grammar Defined as rules that are required for proper shell operation Uses strict syntax Fundamental building blocks Blank Word Name Metacharacter Control operator Reserved word Linux Shell Script Programming

Learning about Shell Grammar A blank is a space or tab used to separate items A word is a sequence of characters considered to be a single unit to the shell A name is a word consisting of letters, numbers and underscore A metacharacter is a character used for a unique and specific purpose by the shell Linux Shell Script Programming

Learning about Shell Grammar Metacharacters | - the pipe symbol allows you to pass output to another command & - the ampersand allows you to run a process in the background ; - the semicolon allows you to separate commands () – the left and right parentheses allow you to run commands in a subshell < - less than redirects input > - greater than redirects output Linux Shell Script Programming

Learning about Shell Grammar Control operators: || - executes a command depending upon failure of another && - executes a command depending upon success of another & - execute in background ; - execute in sequence as separate commands () – execute in a subshell | - pass output to another command A reserved word has special meaning to the shell and cannot be used for another purpose. Example: if, then, else, fi Linux Shell Script Programming

Understanding Command Types Command types rely upon an understanding of shell grammar Command types: Simple command – set of words separated by blanks Examples: ls, touch, pwd, clear Pipelines – sequence of simple commands separated by the pipe symbol Examples: ls | more, who | sort | more List – sequence of one or more pipelines separated by one of these ; & && || terminated by one of these ; & or newline Linux Shell Script Programming

Understanding Command Types Linux Shell Script Programming

Understanding Command Types The ; Operator Causes commands to be executed sequentially in the same shell Example: ls; who The & Operator Causes commands to be executed in a subshell Subshell – spawned from the parent shell Example: ls & pwd Linux Shell Script Programming

Understanding Command Types The && Operator Causes a command to execute only if the preceding command completes successfully (exit status of 0) Example: rm file4.txt && ls The ls command will only execute if rm file4.txt completes successfully The || Operator Causes a command to execute only if the preceding command completes unsuccessfully (exit status of 1) Example: rm file4.txt || pwd The ls command will only execute if rm file4.txt completes unsuccessfully Linux Shell Script Programming

Understanding Command Types Command Types – compound command Allow you to perform calculations, assign variables, perform decision tests and create loops Includes previous command types and Group commands Expressions Decision constructs Looping structures Linux Shell Script Programming

Understanding Command Types Group commands – two forms: (list) – execute list in a subshell Example: (pwd; ls) { list; } – execute list in the current shell Example: { rm file5.txt; pwd; } Expressions used to assign values, perform calculations and test for values – two forms: ((expression)) – evaluates an arithmetic operation Example: ((y=5 + 4)) [[expression]] – tests file attributes, perform string and numeric comparisons Linux Shell Script Programming

Understanding Command Types Linux Shell Script Programming

Understanding Command Types Linux Shell Script Programming

Understanding Command Types Linux Shell Script Programming

Linux Shell Script Programming Chapter Summary The shell can be invoked by either logging in or by entering the name of the shell at the shell prompt. The shell uses startup files to customize the user’s environment. The shell has its own variables that are reserved specifically for its use. They must be entered in uppercase. The File System Hierarchy Standard supports placing scripts used by all users in the /usr/local/bin directory and having users place their own scripts in ~/bin. Linux Shell Script Programming

Linux Shell Script Programming Chapter Summary Shell builtin commands are commands that are compiled as part of the shell. The basic building blocks of shell grammar are: blanks, words, names, metacharacters, control operators, and reserved words. The shell provides for several command types which are simple commands, pipelines, lists, and compound commands Linux Shell Script Programming