0% found this document useful (0 votes)
30 views

Unit - III: Using The Unix

This document discusses various Unix command line concepts including: 1. The structure of Unix commands, including command terminators like ; and & to run commands sequentially or in the background. 2. Meta characters like <, >, |, and how they control command flow and I/O redirection. 3. Creating shell scripts to bundle commands together and accept arguments through $0 and $*. 4. Using output from commands as arguments by enclosing them in backticks. 5. Shell variables and how they can be accessed, modified, and exported for use in subshells. 6. Additional I/O redirection operators like 2> to redirect standard error. 7. Common

Uploaded by

Siva Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Unit - III: Using The Unix

This document discusses various Unix command line concepts including: 1. The structure of Unix commands, including command terminators like ; and & to run commands sequentially or in the background. 2. Meta characters like <, >, |, and how they control command flow and I/O redirection. 3. Creating shell scripts to bundle commands together and accept arguments through $0 and $*. 4. Using output from commands as arguments by enclosing them in backticks. 5. Shell variables and how they can be accessed, modified, and exported for use in subshells. 6. Additional I/O redirection operators like 2> to redirect standard error. 7. Common

Uploaded by

Siva Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Unit - III

Using the Unix


Contents
 Command Line Structure
 Meta Characters
 Creating New Commands
 Command Arguments and Parameters
 Program Output as Arguments
 Shell Variables
 More on I/O Redirection
 Looping in Shell Programs
Command Line Structure
• The simplest command is a single word
,usually naming a file for execution

• A command usually ends with a newline, but


a semicolon ; is also a command terminator
• is identical to typing the commands on
different lines.In particular who does not run
until date has finished
• Try sending the output of “date;who” through
pipe
• This might not be what you expected, because
only output of who goes to wc.
• Parenthesis can be used to group commands
• Data flowing through pipe can be tapped and placed in a file
with the tee command

• Another Command terminator is & , used to run a long running


command in background while you continue to type interactive
commands
• The various special characters interpreted by
shell such as <,>,|,;,& control how shell runs
Meta Characters
Creating New Commands
• 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.
• Suppose you intend to count users frequently with the
pipeline. i.e

– The first step was to create a ordinary fie that contains ‘ who |wc –l’
i.e.

– Run the shell with its input coming from the file nu instead of the
terminal
i.e
– Make nu executable
– And thereafter you can invoke with
• To make nu part of repertoire, move it to private
bin directory and add /usr/you/bin to search path

• Of course, your path should be set properly by the


.profile ,so you dont have to reset it every time you
login
Command Arguments and Parameters

• Suppose we want to make a program called ex


to change the mode of a file to executable. so,
is a shorthand for
• The only new thing we need to know is how to
tell cx what the name of the file is ,since it will
be different each time cx is run.
• The shell provides a shorthand “$*” that mean all the arguments.
• The proper way to define cx then is
• With $* added to repertoire, you can make some convenient shell files lc,m.
• The arguments to shell files need not be file names.
• For example, consider searching a telephone directory. If
you have a file named that contains lines like
/usr/you/lib/phone-book that contains lines like

then the grep command can be used to search it.


Let’s make a directory assistance program, where we will
call 411 in honor of the telephone directory assistance
number where we live.
• So if you revise 411 to look like

The $* will be replaced by the arguments , but it


will be passed to grep as a single argument even if it
contains spaces.
• A novel use of $0 is in the implementations of
the programs 2,3,4.. Which print their output
in many columns

• The implementations of 2,3,.. are identical


,infact they are links to the same file
• The –t option turns of the heading at the top of the
page and the –ln option sets the page length to n lines.
• The name of the program becomes the number of
columns argument to pr,so the output is printed a row
at a time in the number of columns specified by $0.
Program Output as Arguments
• Let us turn now from command line arguments within
a shell file to generation of arguments.
• Certainly file name expansion from meta characters
like * is the most common way to generate
arguments, but another good way is by running a
program.
• The output of any program can be placed in a
command line by enclosing the invocation in back
quotes `...`.

• A small change illustrates that `...` is interpreted


inside “.... “
• Suppsoe you want to send mail to a list of
people whose login names are in the file
mailinglist.

• A slightly different approach is to convert the


fiel mailinglist into a program that prints the
lsit of names.
• Now the mailing the letter to people on the
list becomes

• The second version of mailinglist with a pick


program to modify the userlist interactively,
sends the letter to don and mb.
Shell Variables

• The shell has variables , which in shell jargon,


sometimes called parameters
• Shell variables can be created, accessed and
modified
– is and assignment that changes
the search path
– The value of a variable is extracted by preceding
the name by a dollar sign.
• One of the common uses of variables is to
remember long strings such as parenthesis.

• The shell built-in command set displays the


values of all your defined variables
• The value of a variable is associated with the shell that creates it, and is
not automatically passed to shell’s children.

• A shell fiile cannot change the value of a variable.,because the shell file is
run by sub-shell.
• There are times when using a shell file to
change shell variables is useful
– To add a new directory to your PATH
• The shell therefore provides a command
‘.’(dot) that executes the command in a file in
the current shell rather than in a sub-shell.
• The other way to set the value of a variable in a sub-shell is to assign
to it explicitly on the command line before the command itself.

• The . mechanism is used to change the value of a variable


permanently , while the inline assignment should be used for
temporary changes
• Consider again searching /user/games for
commands, with the directory not in your
PATH.
• When you want to make the value of a
variable accessible in sub-shells, the shell’s
export command is used.
More on I/O Redirection
• Te standard error was invented so that error
messages would always appear on the terminal.

• Every program has three default files established


when it starts,numbered by small integers called
file descriptors.The standard input 0, the standard
output 1 and the standard error output 2
• Sometimes programs produce output on the
standard error even when they work properly.
• It is also possible to merge two output streams
Looping in Shell Programs

• Shell is a programming language. It has


variables,loops and decision making and so
on.

• For example, the for statement to echo file


names one per line is just,
• Another example, to compare the old version of
chapter2(kept in the directory old) with the current one.

• Depending on the size of the fpr, it’s sometimes better to


write it all on one-line
• Don’t use for loop when individual command
loops over filename

is inferior to

• The links to a single file that can be made,


once the file 2 has been written by
• A more interesting use of for , we could use
pick to select which files to compare with
those in the backup directory.

You might also like