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.

Slides:



Advertisements
Similar presentations
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Advertisements

CS 497C – Introduction to UNIX Lecture 11: - The File System Chin-Chih Chang
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.
UNIX chapter 04 UNIX Shells Mr. Mohammad Smirat. Introduction The shell is the software that listens to commands typed in at the terminal and translates.
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.
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
Guide To UNIX Using Linux Third Edition
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
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.
Parameter substitution ParameterMeaning $parameter or ${parameter} Substitute the value of parameter ${parameter:- value} Substitute the value of parameter.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
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.
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.
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
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.
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
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
CS465 - UNIX The Bourne Shell.
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.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
User Environments Objectives –to provide appropriate environments for different types of users Contents –different login programs –user profiles –restricted.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Customizing the Shell Environment. UNIX Shells Two characteristics of shells –Interactive: prompts ($) and waits for your response/requests –Noninteractive:
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Shell Control Statements and More
Compunet Corporation Introduction to Unix (CA263) Your Environment By Tariq Ibn Aziz Dammam Community College.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Files and Directories in UNIX The first file in UNIX file system is “root” or “/”
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Comp 145 – Introduction to UNIX $200 $400 $600 $800 $1000 $200 $400 $600 $800 $1000 $200 $400 $600 $800 $1000 $200 $400 $600 $800 $1000 UNIX Processes.
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.
Shell Script Assignment 1.
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
Linux Shell Script Programming
Chapter 5 Bourne Shells Scripts
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

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 The environment is maintained all the time until the user logs off

Subshell Subshell is a new shell that is executed by the login shell in order to run a desired program A subshell has no knowledge of local variables that were assigned values by the parent shell The subshell cannot change the value of a variable in the parent shell $ cat varfile x=5 echo :$x: $ x=10 $ varfile :5: $ echo $x 10

Subshell (continue.) X=10. login shell (parent shell) X=5. varfile (subshell)...

Exporting variables Format: export variables Make the value of a variable known to a subshell $ cat varfile2 echo x = $x echo y = $y $ x=100 $ y=10 $ varfile2 x = y = $ export x $ varfile2 x = 100 y =

Exporting variables (continue.) login shell vartest3 (subshell) y=10... X= y=10... exported variables exported variables local variables local variables copied

Exporting variables (continue.) There is no way to change the value of a variable in a parent shell from within a subshell $ cat varfile3 x=500 y=5 $ varfile3 $ echo $x $y 50 5

Exporting variables (continue.) login shell vartest4 (subshell) y=10... X= x=50 y=5... y=10... exported variables exported variables local variables local variables copied

Exporting variables (continue.) Once a variable is exported, it remains exported to all subshells that are subsequently executed $ cat varfile4 x=50 y=5 z=1 export z vartest5 $ cat vartest5 echo x = $x echo y = $y echo z = $z

Exporting variables (continue.) $ varfile4 x = y = 10 z = 1 $

Exporting variables (continue.) login shell vartest4 (subshell) y=10... x= x=50 y=5... y=10 z=10... vartest5 (subshell) y=10 z=1... exported variables exported variables local variables local variables exported variables local variables

Exporting variables (continue.) If a variable gets exported again in a subshell, the changed value of the variable will get passed down to all subshells (from the subshell that rexported the variable) $ cat varfile6 x=50 y=5 z=1 export y z $ varfile6

Exporting variables (continue.) login shell vartest4 (subshell) y=10... x= x=50... y=5 z=1... vartest5 (subshell) y=5 z=1... exported variables exported variables local variables local variables exported variables local variables

Local and Exported variables Summary 1. Any variable that is not exported is a local variable whose existence will not be known to subshells 2. Exported variables and their values are copied into a subshell's environment, where they may be accessed and changed. However, such changes have no affect on the variables in the parent shell 3. If a subshell explicitly exports a variable, then changes made to that variable affect the exported one. If a subshell does not explicitly export a variable, then changes made to that variable affect a local one, even if the variable was exported from a parent shell

Local and Exported variables Summary (continue.) 4. Exported variables retain this characteristic not only for directly spawned subshells, but also for subshells that affect the exported one. If a subshell affect a local one, even if the variable was exported from a parent subshell 5. A variable can be exported any time before or after it is assigned a value

export with no arguments export with no arguments prints a list of the variables that are explicitly exported by the current shell $ export export x export z

Current Directory There no way to change the current directory of a parent shell from a subshell $ cat cdfile cd /usr/bin/ pwd $ pwd /home/sbenayed/UNIX $ cdfile /usr/bin $

Variables used by the shell VariableMeaning CDPATHThe directories to be searched whenever cd is executed without a full path as argument HOMEThe directory that cd changes to when no argument is supplied IFSThe Internal Field Separator characters; used by the shell to delimit words when parsing the command line, for the read and set commands, when substituting the output from a back-quoted command, and when performing parameter substitution. Normally, it contains the three characters space, horizontal tab, and newline

Variables used by the shell (continue.) VariableMeaning MAILThe name of a file that the shell will periodically check for the arrival of mail. If new mail arrives, then the shell will display its You have mail message. MAILCHEC K The number of seconds specifying how often the shell is to check for arrival of mail in the file MAIL or in the files listed in MAILPATH. The default is 600. A values of 0 causes the shell to check before displaying each command prompt.

Variables used by the shell (continue.) VariableMeaning MAILPATHA list of files to be checked for the arrival of mail. Each file is delimited by a colon, and can be followed by a percent sign (%) and a message to be displayed when mail arrives in the indicated file (You have mail is the default) PATHA colon delimited list of directories to be searched when the shell needs to find a command to be executed. The current directory is specified as :: (if it heads the list, : suffices) PS1The primary command prompt, normally "$ " PS2The secondary command prompt, normally ">"

Variables used by the shell (continue.) Variable Meaning SHACCTThe name of a file that the shell will use to write accounting information for commands that it executes. This information can later be analyzed using the acctcom command SHELLThe name of the shell (e.g. bin/sh). This variable is used by vi and ed to determine the shell to startup when you escape to the shell or execute a shell command. It's also used by the shell on startup to determine if it should run restricted. This determination is made based upon whether the letter "r" appears in the name of the shell

The. Command Format:. File dot command executes the contents of file in the current directory $ cat vars UNIX=/home/abuzneid/UNIX $ vars $ echo $UNIX $. vars $ echo $UNIX /home/abuzneid/UNIX $

The exec Command Format: exec program exec command replaces the current program with the new one Startup time of an exec'ed program is quicker

I/O Redirection and Subshells shell executes in a subshell commands like if, for, while and until if their input and/or output is redirected or piped $ for x in 1 2 3; do echo $x; done $ echo $x 3 $ for y in 1 2 3; do echo $y; done > /tmp/foo $ echo $y $

Change Standard I/O to a file To change the standard input to a file: exec < file To change the standard output to file: exec > file To reassign standard input back to terminal: exec < dev/tty To reassign standard input back to terminal: exec < dev/tty

Shell Script: wcl Count the number of lines in a file To view the source code of wcl click herehere $ wcl /etc/passwd 15

(...) construct Groups a set of commands to set them to be executed by a subshell Input and output can be piped to and from this construct, and I/O can be redirected Used to send a set of command to the background $ (cd UNIX; ls) addi Documents monitor personal varfile3 cdfile greetings~ myln rem vars $

{...; } construct Groups a set of commands to set them to be executed by the current shell Input and output can be piped to and from this construct, and I/O can be redirected A space must follow the left hand brace, and a semicolon must appear after the last command $ x=10 $ (x=100) $ echo $x 10 $ { x=100; } $ echo $x 100

Another Way to Pass variables to a Subshell Proceed name of the command with the assignment of as many variables like: var1=value1 var2=value program is identical to: (var1=value1; var2=value2; export var1 var2; program)

.profile File The login shell executes two special files on the system /etc/profile.profile in the home directory $ cat $HOME/.profile stty dec PATH=/bin:/usr/bin:/usr/ucb:/etc:/usr/etc:/usr/et c/install:. export PATH TERM echo This is.profile umask 077 $

/etc/profile File $cat /etc/profile #ident /10/03 SMI" /*SVr */ # The profile that all logins get before using their own.profile. trap "" 2 3 export LOGNAME PATH if [ "$TERM" = "" ] then if /bin/i386 then TERM=sun-color else TERM=sun fi export TERM fi

/etc/profile File (continue.) # Login and -su shells get /etc/profile services. trap "" 2 /bin/mail -E case $? in 0) echo "You have new mail." ;; 2) echo "You have mail." ;; esac fi esac umask 022 trap 2 3

References UNIX SHELLS BY EXAMPLE BY ELLIE QUIGLEY UNIX FOR PROGRAMMERS AND USERS BY G. GLASS AND K ABLES UNIX SHELL PROGRAMMING BY S. KOCHAN AND P. WOOD