UNIX and Shell Programming (06CS36)

Slides:



Advertisements
Similar presentations
Chapter 9: The TC Shell Everything you thought you knew is now wrong.
Advertisements

NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Chapter Seven Unix Shell Environments1 System Programming UNIX Shell Environments.
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
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
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Guide to Linux Installation and Administration, 2e1 Chapter 6 Using the Shell and Text Files.
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Using Linux Commands 2 Lab#5
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.
Welcome to CSE  Name: Di Cao   Classroom: DL357  Class Time: T 8:30am - 9:18am  Office.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter 9 Part II Linux Command Line Access to Linux Authenticated login using a Linux account is required to access a Linux system. The Linux prompt will.
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 the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
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.
UNIX and Shell Programming (06CS36) Unit 1 Continued… Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S. Gogte Institute of.
Workbook 6 – Part 1 The Bash Shell
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.
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 Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts.
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.
June 1, 1999Customizing the UNIX Environment1 Introduction to UNIX G. Customizing the UNIX Environment.
Unix Shell Environments February 23rd, 2004 Class Meeting 6.
UNIX shell environments CS 2204 Class meeting 6 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
User Environments Objectives –to provide appropriate environments for different types of users Contents –different login programs –user profiles –restricted.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Customizing the Shell Environment. UNIX Shells Two characteristics of shells –Interactive: prompts ($) and waits for your response/requests –Noninteractive:
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
1 Day 18 Bash and the.files. 2 The.files ls shows you the files in your directory –Or at least most of them. –Some files are hidden. Try: ls –a –This.
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.
Jozef Goetz, expanded by Jozef Goetz, 2006 Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
Linux+ Guide to Linux Certification, Second Edition
Configuration your environment Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration.
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.
Class Introduction. Agenda Syllabus Topics Text etc.
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.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Shells.
SUSE Linux Enterprise Desktop Administration
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
System Programming and administration CS 308
Shell Environments.
John Carelli, Instructor Kutztown University
(Chapter 2) John Carelli, Instructor Kutztown University
CSCI The UNIX System Shell Startup and Variables
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
UNIX and Shell Programming (06CS36)
Unix Shell Environments
UNIX and Shell Programming (06CS36)
UNIX and Shell Programming (06CS36)
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

UNIX and Shell Programming (06CS36) Unit 3continued… Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S. Gogte Institute of Technology, Belgaum. INDIA. mangalwede@yahoo.com

Customizing the Environment The Shells – A Re-look Environment Variables Common Environment Variables – their usage Aliases – Shorthand names for commands Command History In-line Command Editing – vi-like capability Miscellaneous Features - Using set –o - Tilde Substitution The Initialization Scripts - The Profile - The rc File Shrinivas R. Mangalwede, G.I.T., Belgaum

The Shells UNIX shell is both an interpreter and a scripting language. An interactive shell runs a non-interactive shell when executing a shell script. Bourne Shell – Strong programming features, weak interpreter. C Shell – Improved interpretive features, wasn’t suitable for programming. Korn Shell – Combines best of the two. Has features like aliases, command history. But lacks some features of C. Bash Shell – Superset that combined the features of Korn and C Shells. Conforms to POSIX shell specification. Shrinivas R. Mangalwede, G.I.T., Belgaum

Environment Variables Shell variables are of two types: Local and environment. PATH, HOME, SHELL etc. are examples of environment variables. Environment variables are available in the user’s total environment including the sub-shells that run the shell scripts. Local variables are more restrictive in scope. The value of a local variable is not available to child processes. (Eg. Sub shell) set: Displays all variables available in the current shell. env: Displays only environment variables. env is an external command that runs in a child process whereas set is a shell built-in. Shrinivas R. Mangalwede, G.I.T., Belgaum

Common Environment Variables Significance HOME Home directory – the directory a user is placed on login PATH List of directories searched by the shell to locate a command LOGNAME Login name of user USER MAIL Absolute pathname of user’s mailbox file MAILCHECK Mail checking interval for incoming mail TERM Type of terminal

Common Environment Variables Significance PWD Absolute pathname of current directory (Bash and Korn only) CDPATH List of directories searched by cd when used with a non-absolute pathname PS1 Primary prompt string PS2 Secondary prompt string SHELL User’s login shell

Some Examples To include the directory /home/srm/bin in the search use, PATH=$PATH:/home/srm/bin To display your home directory use, echo $HOME The location of the user’s mailbox is stored in MAIL. It is generally /var/mail or /var/spool/mail (Linux) MAILCHECK determines how often the shell checks the mailbox file for the arrival of new mail. Once the shell finds the file modified since last cjeck, it informs the user with the message, You have mail in /var/mail/srm Shrinivas R. Mangalwede, G.I.T., Belgaum

Some Examples The primary prompt string is $ and secondary prompt string is >. You can change the primary prompt string to C> as, $ PS1=“C>” C> . . . TERM indicates the terminal type. Every terminal has certain control characteristics that are defined in a separate control file in the /usr/share/lib/terminfo directory. IFS contains a string of characters that are used as field separators for command and arguments. They are normally the space, tab and newline characters. Shrinivas R. Mangalwede, G.I.T., Belgaum

Bash and Korn Shells $ PS1=‘[PWD] ‘ [/home/srm] cd progs [/home/srm/progs] _ Bash and Korn also support a history facility that treats a previous command as an event and associates it with a number. $ PS1=‘[!] ‘ $ PS1=‘[! $PWD] ‘ [42] _ [42 /home/srm/progs] _ $ PS1=“\h> “ // Host name of the machine saturn> _ Shrinivas R. Mangalwede, G.I.T., Belgaum

Aliases (bash and korn) Alias is a shorthand name that you can assign to frequently used commands $ alias dir=‘ls –l’ $ alias cdsys=“cd /usr/include/sys” You can also use aliasing to redefine an existing command. $ alias cp=“cp –i” Every time you invoke an alias, their aliased version will be executed. To use original external command, precede the command with a \ (backslash). $ \cp file1 file2 overrides the alias Shrinivas R. Mangalwede, G.I.T., Belgaum

Aliases (bash and korn) To display an alias definition use alias with the name, $ alias cp cp=“cp –i” You can list all aliases by using alias without arguments and unset an alias with the unalias statement. $ unalias cp Shrinivas R. Mangalwede, G.I.T., Belgaum

Command History (bash and Korn) Bash and Korn treat a previous command as an event and associate it with an event number. Using this number, you can recall previous commands, edit them if required and reexecute them. The history command displays the history list showing event number of every previously executed command By default, bash lists the complete command history while korn lists the last 16 most recently used commands. $ history 5 Bash $ history -5 Korn History is stored in $HOME/.bash_history or $HOME/.sh_history Shrinivas R. Mangalwede, G.I.T., Belgaum

Accessing previous commands By event numbers (! In bash and r in korn) $ !38 The command with event number 38 is displayed and executed (Use r 38 in korn) $ !38:p The command is displayed. You can edit and execute it $ !! Repeats previous command (Use r in korn) $ !-2 Executes command prior to the previous one ( r -2 in korn) By Context $ !v Repeats the last command beginning with v (r v in korn) Shrinivas R. Mangalwede, G.I.T., Belgaum

Substitution in previous commands If you wish to execute a previous command after some changes, you can substitute the old string with new one by substitution. If a previous command cp progs/*.doc backup is to be executed again with doc replaced with txt, $ !cp:s/doc/txt in bash $ r cp doc=txt in korn $_ is a shorthand feature to represent the directory used by the previous command. $ mkdir progs $ cs $_ Shrinivas R. Mangalwede, G.I.T., Belgaum

The History variables The command history will be maintained in default history files viz., .bash_history in Bash .sh_history in Korn Variable HISTFILE determines the filename that saves the history list. Bash uses two variables HISTSIZE for setting the size of the history list in memory and HISTFILESIZE for setting the size of disk file. Korn uses HISTSIZE for both the purposes. Shrinivas R. Mangalwede, G.I.T., Belgaum

Miscellaneous Features Using set –o set –o noclobber: Prevents overwriting of an existing file with > symbol. To override this protection, use | after the > ls –l >| file_list set –o ignoreeof: Prevents accidental logout when you press [Ctrl-d] to terminate standard input. A set option is turned off with set +o keyword. Tilde Substitution Tilde (~) acts as a shorthand representation of the home directory. A configuration file like .profile can be referred to both as $HOME/.profile and ~/.profile. Shrinivas R. Mangalwede, G.I.T., Belgaum

The initialization scripts The effect of assigning values to variables, defining aliases and using set options is applicable only for the login session; they revert to their default values when the user logs out. To make them permanent, use certain startup scripts. The startup scripts are executed when the user logs in. .profile (Bourne shell) .profile and .kshrc (Korn shell) .bash_profile (or .bash_login) and .bashrc (Bash) .login and .cshrc (C shell) You can also execute them by using a special command (called dot). $ . .profile Shrinivas R. Mangalwede, G.I.T., Belgaum

The initialization scripts When logging into an interactive login shell, login will do the authentication, set the environment and start your shell. In the case of bash, the next step is reading the general profile from /etc, if that file exists. bash then looks for ~/.bash_profile, ~/.bash_login and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. If none exists, /etc/bashrc is applied. When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists. Shrinivas R. Mangalwede, G.I.T., Belgaum

.profile and .bash_profile Sample entries $ cat .profile # User $HOME/.profile – commands executed at # login time MAIL=/var/mail/$LOGNAME PATH=$PATH:$HOME/bin:/usr/ucb:. PS1=‘$ ‘ PS2=> TERM=vt100 Stty stop ^S intr ^C erase ^? Echo “Today’s date is `date`” Shrinivas R. Mangalwede, G.I.T., Belgaum

The rc file Normally the profiles are executed only once, upon login. The rc files are designed to be executed every time a separate shell is created. There is no rc file in Bourne, but bash and korn use one. This file is defined by an environment variable BASH_ENV in Bash and ENV in Korn. export BASH_ENV=$HOME/.bashrc export ENV=$HOME/.kshrc Korn automatically executes .kshrc during login if ENV is defined. Bash merely ensures that a sub-shell executes this file. If the login shell also has to execute this file then a separate entry must be added in the profile: . ~/.bashrc Shrinivas R. Mangalwede, G.I.T., Belgaum

The rc file You should define command aliases, variable settings, and shell options in your rc file. Some sample entries of an rc file are alias cp=“cp –i” alias rm=“rm –i” set –o noclobber set –o ignoreeof set –o vi The rc file will be executed after the profile. However, if the BASH_ENV or ENV variables are not set, the shell executes only the profile. Shrinivas R. Mangalwede, G.I.T., Belgaum

To conclude, Environment-related features of the shell Common Environment Variables Environment variables specific to bask and korn Aliases Command History In-line Command editing set –o and Tilde substitution The initialization scripts The Profile file The rc file

End of Session