unix - class4 - advance shell scripting-p1
TRANSCRIPT
UNIX
Advance Shell Scripting
Presentation By
Nihar R Paital
Nihar R Paital
Introduction
Advance features of shell scripting/commandsuch as:
Local and Global Shell variable Customizing User Environment Functions User interface Conditional execution File Descriptors traps Multiple command line args handling
Nihar R Paital
/dev/null
This is special Linux file which is used to send any unwanted output from program/command.Syntax: command > /dev/nullExample:
$ ls > /dev/null
Run the following two commands$ ls > /dev/null$ rm > /dev/null1) Why the output of last command is not redirected to /dev/null
device?
Nihar R Paital
Local Shell variable
$a=20$echo $aOutput : 20$/bin/sh # Entering into New Shell$ echo $aOutput : Empty Line Printed due to a is not defined in new shell$ a=50$ echo $aOutput : 50$ exit #Returned to Old Shell by exiting Old Shell$echo $aOutput : 20$
Local and Global Shell variable
Nihar R Paital
Global Shell Variable
To set global varible you have to use export command.Syntax: export variable1, variable2,.....variableN $a=500 # Create local variable a with value 500$echo $aOutput : 500$export a # a became global variable by export command$/bin/sh # Entering into New Shell$ echo $aOutput : 500 # Value of a is constant from old to new as it is global$ exit$echo $aOutput : 500$
Local and Global Shell variable
Nihar R Paital
Customizing User Environment
The most basic means of customization that the Korn shell provides are
Aliases Synonyms for commands or command strings that you can define for convenience.
Options Controls for various aspects of your environment, which you can turn on and off.
Variables Place-holders for information that tell the shell and other programs how to behave under various circumstances. To customize the environment various built-in shell variables are available.
Nihar R Paital
Customizing User Environment
>To change the values of variables permanently , define it in .profile file.
The .profile File This is a file of shell commands, also called a shell script, that the Korn
shell reads and runs whenever you log in to your system. Various environment variables can be defined in this file
Alias can be defined in .profile file
Customizing User Environment
Nihar R Paital
Aliases
Alias is a synonym for a command or command string Syntax: alias new=original Ex:-
alias search=grepalias cdnew=‘cd /xyz/x1/x2’
>Quotes are necessary if the string being aliased consists of more than one word
>it is possible to alias an alias, aliases are recursive
Ex:- alias c=cdnew
Type alias without any arguments, to get a list of all the aliases you have defined as well as several that are built-in.
The command unalias name removes any alias definition for its argument
Customizing User Environment
Nihar R Paital
set command.
set command
– Used for display all the environment variables.– Shows the current values of system variables.– Also allows conversion of arguments into positional
parameters. – Syntax : set
Customizing User Environment
Nihar R Paital
Set Options
Options let you change the shell's behaviour A shell option is a setting that is either "on" or "off." The basic commands that relate to options are set -o optionnames and set +o optionnameswhere optionnames is a list of option names separated by blanksThe - turns the named option on, while the + turns it offOption Descriptionemacs Enter emacs editing modeignoreeof Don't allow use of [CTRL-D] to log off; require the exit commandnoclobber Don't allow output redirection (>) to clobber an existing filenoglob Don't expand filename wildcards like * and ? (wildcard
expansion is sometimes called globbing)nounset Indicate an error when trying to use a variable that is undefinedvi Enter vi editing modextrace traces shell scriptingnoexec finds syntax error without executing script To check the status of an option, type set -o
Customizing User Environment
Nihar R Paital
Shell Variables
Shell variables can specify everything from your prompt string to how often the shell checks for new mail
built-in variables have names in all capital letters The syntax for defining variables is
$ varname=value if the value is more than one word, it must be surrounded by quotes To delete a variable type the command
$ unset varnameEx:
$ a=20$ echo $aOutput: 20$ unset a$ echo $aOutput: Empty Line
Customizing User Environment
Nihar R Paital
Print Command
To check value of a variable print built-in command can be used
Print command is strongly recommended over echo because its options are the same on all UNIX systems, whereas echo's options differ between BSD-derived and System V-derived UNIX versions.
Ex:- print “$x”
Customizing User Environment
Nihar R Paital
System Variables or Built-in Variables
PATH– Search path referred by Unix for any command. – echo $PATH
HOME– Indicates the home directory for the user.– echo $HOME
In the bash shell, command history is controlled by which group of thefollowing environment variables.HISTCMD, HISTFILE, HISTSIZE, HISTFILESIZE
HISTFILE - Name of history file, on which the editing modes operate.
HISTSIZE– Number of lines kept in history file
Customizing User Environment
Nihar R Paital
System Variables (Contd).
FCEDIT– Pathname of editor to use with the fc command.
PS1– Used for displaying & changing the primary prompt. – echo $PS1
PS2– Used for changing the secondary prompt.
MAIL– Name of file to check for incoming mail (i.e., your mail file)
MAILCHECK– How often, in seconds, to check for new mail (default 600
seconds, or 10 minutes)
Customizing User Environment
Nihar R Paital
System Variables (Contd).
SHELL– Pathname of the shell you are running
PWD– Current directory
HOME– Users home directory
Customizing User Environment
Nihar R Paital
Environment Variables
Environment Variables are known to all kinds of subprocesses Any variable can become an environment variable. First it must be
defined as usual; then it must be exported with the command$ export varnames
To find out environment variables and their values ,type$ export
Customizing User Environment
Nihar R Paital
The Environment File
Although environment variables will always be known to subprocesses,
the shell must define which other variables, options, aliases, etc., are to communicated to subprocesses.The way to do this is to put all such definitions in a special file called the environment file instead of your .profile.
1. Decide which definitions in your .profile you want to propagate to subprocesses. Remove them from .profile and put them in a file you will designate as your environment file.
2. Put a line in your .profile that tells the shell where your environment file is:ENV=envfilename
3 . For the changes to take effect, type either . .profile or login. In either case, your environment file will be run when the shell encounters the ENV= statement.
Customizing User Environment
Nihar R Paital