Professional Documents
Culture Documents
#!/bin/sh
echo hello world
$ chmod u+x my-first-script
• Try to exeute it directly
$ my-first-script
hello world
$
Command Arguments And Parameter
• A command is a program that tells the Unix system to do
something. It has the form: command [options]
[arguments]
• Where an argument indicates on what the command is
to perform its action, usually a file or series of files. An
option modifies the command, changing the way it
performs.
• When the shell executes a file of commands, each
occurrence of $1 is replaced by the first argument, each
$2 is replaced by the second argument, and so on
through $9.
Command-Line Arguments
• The command-line arguments $1, $2, $3,...$9 are positional
parameters, with $0 pointing to the actual command,
program, shell script, or function and $1, $2, $3, ...$9 as the
arguments to the command.
• Script uses various special variables related to command line −
#!/bin/sh
echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"
Example
• Here is an example of using the cal command with zero and
two arguments.
• If you type as
$ cal
– with zero argument it print current month and year
• Now if you give 2 arguments
$cal 11 2016
– It will print cal of current month.
Special Parameters $* $# and $@:
• There are special parameters that allow accessing all of the
command-line arguments at once. $* and $@ both will act
the same unless they are enclosed in double quotes, "".
• the "$*" special parameter takes the entire list as one
argument with spaces between
• the "$@" special parameter takes the entire list and
separates it into separate arguments but if “ “ are not use it
behave same as $*.
• $# is a special parameter which can be use to see total
number of positional parameter were set.
Exit Status
• The $? variable represents the exit status of the previous command.
• Exit status is a numerical value returned by every command upon its
completion. As a rule, most commands return an exit status of 0 if they
were successful, and 1 if they were unsuccessful.
• Some commands return additional exit statuses for particular reasons. For
example, some commands differentiate between kinds of errors and will
return various exit values depending on the specific type of failure.
• Following is the example of successful command −
$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Total Number of Parameters : 2
$echo $?
0
$
Some special Parameter
• $$ - PID of current shell
• $? - exit status of the last executed command.
• $! - PID of last background process.
• $#- Total number of positional parameter.
• $0 - Name of the command being executed.
• $* - List of all shell argument. Can’t yields each
argument separately.
• $@ - similar to $*, but yields each argument
separately when enclosed in double quotes.
Examples
1. Let create our own command in which we will change its
mode to executable.
– $echo ‘chmod +x $1’ >cx create cx as a file
– $ sh cx cx (making cx as exectable)
2. Consider searching a personal telephone directory. File
name is phone_book. And its contents are as follows:
Dial-a-joke 212-976-3838
Dial-a-prayer 212-246-4200
Dial santa 212-976-3636
Example continue
• Then we will use grep command to search for the
pattern given as the argument:
$ echo ‘grep $* phone-book’ >111
$ cx 111
$ 111 joke
Dial-a-joke 212-976-3838 (output)
$ 111 dial
Dial-a-joke 212-976-3838 (output)
Dial-a-prayer 212-246-4200
Dial santa 212-976-3636
Program Output as Arguments
• The output of any program can be placed in a
command line by enclosing the invocation in
backquotes ‘….’.
• For example
• $ echo “Today’s date and time is ‘date’.”
• Today’s data and time is Sun dec 20 00:02:15
EDT 2013.
• $
The Metacharacters
• Unix Shell provides various metacharacters which have
special meaning while using them in any Shell Script and
causes termination of a word unless quoted.
• For example ? matches with a single charater while
listing files in a directory and an * would match more
than one characters.
• Here is a list of most of the shell special characters (also
called metacharacters) −
• * ? [ ] ' " \ $ ; & ( ) | ^ < > new-line space tab A character
may be quoted (i.e., made to stand for itself) by
preceding it with a \.
Meta Characters
• The shell recognizes a characters as special.
They are as follows:
1. * Matches any string or group of characters.
A null or no character at all may also qualify
for representation by a *.
– Use ls *.lst to list all files with extension .lst.
2. ? matches a single character.
– Use cp ???.c progs to copy files whose filename
has 3 characters and the .c extension.
Pattern Matching – The Wild Cards
3. [ccc] matches a single character – ranges like
0-9 or a-z are legal.
– [cut] matches a single character – either a ‘c’, ‘u’,
or ‘t’.
– [!cut] matches a single character that is not ‘c’, ‘u’
or ‘t’.
4. [x-z] matches a single character from x to z.
– Use rm note[01][0-9] to remove files note00,
note01, …., note18, note19.
Pattern Matching – The Wild Cards
5. [!q-z] matches a single character that is not
within q to z.
• These metacharacters lose their meaning
when placed in the wrong place.
• The * doesn’t match all filenames beginning
with a . (dot), or the / of a pathname.
• To list all the hidden files having at least three
characters after the dot, use
ls .???*
Examples
Command Output
ls a* Lists all files beginning with character ‘a’
ls ?? Lists all files whose names are 2 characters long.
ls a?b? Lists all 4 characters filenames whose first character is ‘a’ and
third character is ‘b’
ls [kdgp]* Lists all files whose first character is ‘k’, ‘d’, ‘g’ or ‘p’.
ls [c-fmrv-z]* Lists all files whose first character is ‘m’ or ‘r’ or is in the
range c to f or v to z
ls [!d-m]* Lists all files whose first character is anything other than an
alphabet in the range d to m.
Process Execution Metacharacter
• To run more than one command at the $ prompt in stroke, we
separate them with a semicolon. For example,
– $ ls ; who ; banner Hi
• This would result in the execution of ls first, then who, and , lastly the banner
command display a big Hi.
• To execute some command in in a sub-shell, we can enclose
them in parentheses.
– $ (cd mydir ;pwd )
– /usr/aaa/mydir
– $pwd
– /usr/aaa
• Parentheses causes a subshell to be invoked, in which a change of directory is
executed. Back in the current shell, we are still in /use/aaa
Conditional Execution Using && and ||
• Characters with special meaning to shell " ' ` $ * [ ] ? ; > < & ( ) \
• Avoid special meaning with quoting
echo 'You have $20'
Creating New Command
• Given a sequence of commands that is to be repeated more
than a few times, it would be convenient to make it into a
“new” command with its own name, so you can use it like a
regular command.
• To create new command we need to do following things
• Suppose you intent to count users frequently with the
pipeline $who|wc-l
• Now the first step is to create an ordinary file that contains
‘who|wc-l’
– $echo ‘who|wc-l’ >nu
Creating New Command
• The shell is a program just like an who or wc, its name is sh. And since it’s
a program , you can run it and redirect its input.
• So run the shell with its input coming from the file nu instead of the
terminal
– $ cat nu
Who|wc-l
$ sh <nu
4
$
• The output is the same as it would have been if you had typed who|wc-l
at the terminal.
Creating New Command
• If a file is executable and if it contains text, then
the shell assumes it to be of shell commands.
Such file is called a shell file.
• All you have to do is to make nu executable.
$ chmod u+x nu
OR
$ chmod 744 nu
And thereafter you can invoke it with
$nu