1 / 23

UNIX and Shell Programming (06CS36)

UNIX and Shell Programming (06CS36). Unit 3 continued…. 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

sharne
Download Presentation

UNIX and Shell Programming (06CS36)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. Common Environment Variables

  6. Common Environment Variables

  7. 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

  8. 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

  9. 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

  10. 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 file2overrides the alias Shrinivas R. Mangalwede, G.I.T., Belgaum

  11. 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

  12. 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

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

  14. 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=txtin korn $_ is a shorthand feature to represent the directory used by the previous command. $ mkdir progs $ cs $_ Shrinivas R. Mangalwede, G.I.T., Belgaum

  15. 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

  16. 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 +okeyword. 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

  17. 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

  18. 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

  19. .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

  20. 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

  21. 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

  22. 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

  23. End of Session

More Related