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

Lecture 2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture 2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

TOPICS

 Special Keys and Control Characters


 Redirecting input and output
 Unix Files
 System startup and Login shell
Review of last Lecture
Questions..
1. You are logged on as user unix61. Write
down four commands that the user has at
their disposal which would allow him/her to
know which shell they are working in?
 Ex 1
Log on to the system and find the following
files:
(i) /bin/login
(ii) /etc/passwd

 Ex 2
Type grep “userid” /etc/passwd or
echo $SHELL
What is your shell?
What is your prompt?
Special Keys and Control
Characters
Special Keys and Control
Characters
UNIX recognizes special keys and control-
character key strokes; and assigns them special
functions. Below is the list:

 DELETE: Acts as an erase key.


 BACKSPACE: Also acts as a erase key.
 CTRL-U: ^U erases the entire command
line.
Special Keys and Control
Characters (con’t)
4. CTRL-W: ^W erases the last word on the
command line.
5. CTRL-S: ^S stops the flow of output
on the display.
6. CTRL-Q: ^Q resumes the flow of output
stopped by CTRL-S.
7. CTRL-C: ^C interrupts a command or
process in progress.
Special Keys and Control
Characters (con’t)
8. CTRL-Z: ^Z suspends a command or
process in progress.
9. CTRL-D ^D generates an end-of-file
character.
Redirecting input and output
Redirecting input and output
 Almost all input comes from the keyboard
 Almost all output goes to the screen

Enter the commanddate

Now enter the command date > myfile


Redirecting output
Does anyone know how to view the contents of
myfile?

 ‘ >’ (output redirection symbol) redirects


output to a file.
Syntax:
command > filename
Redirecting output
 ‘ >> ’ appends the output to the file instead of
overwriting the file like ‘ > ’ would do.
Syntax:
command >> filename

Eg. echo $HOME >> myfile


Redirecting input
Enter the command
tr “[a-z]” “[A-Z]” < myfile

 ‘< ’ tells the command to take input from a


file instead of the keyboard.
Syntax:
command/program < filename
Redirecting both input and output
 Enter the command
tr “[a-z]” “[A-Z]” < myfile >myfile2
Ist the input is read from myfile and than the output writes to myfile2.

You can get both kinds of redirections.

Syntax: program< inputfile > outputfile


command < inputfile > outputfile
Redirecting output- The Pipe Symbol
 Finally we have the pipe symbol “ | ” . Two
commands can be combined in Unix using pipes.
The “ | ” tells the computer, take the output from
the command on the left, and use it as input for
the command on the right
Eg: ps – ef
ps –ef | more
Examples
 ps –ef | grep unix
 ps –ef | grep unix | grep 15

 who | sort

 who | sort | tee file2 | grep unix15


Standard Error Symbols
 The standard errors of UNIX commands are
also easily redirected.
 Bourne & Korn shells
 Use 2 >
 To append use 2 >>

 C Shell
 Use >&
 To append use >>&
Standard Error Symbols
Eg. Using the C shell
Enter the command l s –al
Enter: l s –al > errfl
Now enter pw d >& errfl

To append further error messages to this file,


for example:
ech o $HOME >& errfl
Character Action

> Redirect standard output


>& Redirect standard output and standard error
< Redirect standard input
>! Redirect standard output; overwrite file if it
exists

>&! Redirect standard output and standard error;


overwrite file if it exists

| Redirect standard output to another


command (pipe)

>> Append standard output


>>& Append standard output and standard error
Examples
 pwd
 pwd; date
 pwd; date > names
 (pwd; date) > names
 set noclobber (this prevents the redirect
output from overwriting existing files)
 cal > names
 cal >! names
 who >> names
UNIX Files
UNIX Files
 A file must have a name, and UNIX imposes
very little restrictions on file names.
 A filename can be up to 256 characters long,
consisting of any alphanumeric character on
the keyboard except the "/".
 However, try to keep filenames as short as
possible and do not include blanks in you file
name. Use the underscore (“_”) character
instead.
UNIX Files (con’t)
 Remember that UNIX is case sensitive, which
means it recognizes the difference between
upper-case and lower-case letters. For
instance, readme, Readme and README
would refer to three different files.
 You can also use extensions on your files, if it

makes identifying them a lot easier.


Eg: .txt for text file; .lab for lab file; etc
UNIX Files (con’t)
UNIX programs, especially compilers, look for
certain standard extensions so you should get
yourself acquainted with them.
 .h for header files; eg. stdio.h
 .c for source file; eg. hello.c

 Note that some UNIX files begin with a


period (“.”) . These are usually environment
or application files.
Creating and Displaying Files
 We can create files using the cat command.
We can also display files with the cat command
 The more command, or the combination of the
more and cat commands can also be use to
display a file.
 Syntax: more filename
 Syntax: cat filename | more
 We can also display the contents of files using
head, tail and pg commands.
Displaying Files
To display the first ten lines of a file use the
head command:
Syntax:head filename
Eg. head flnames
To display the last ten lines of a file use the tail
command:
Syntax:tail filename
Eg. tail flnames
Displaying Files(con’t)
 Similar to the more command we can use the
less and pg commands.
Syntax: less filename
Syntax: pg filename
 Eg. less flnames
 Eg. pg flnames
Displaying Files(con’t)
 Create a file called listing.etc.txt as follows
ls –l /etc > listing.etc.txt
Now try the following commands
(i) cat listing.etc.txt

(ii) more listing.etc.txt


(iii) head listing.etc.txt

(iv) tail listing.etc.txt


(v) pg listing.etc.txt
Listing Files
 The ls command will list files in the current
directory; except the hidden files.
Enter:
ls
ls – a
ls – l
ls – lR /etc
ls – lF /lib
Listing Files (Con’t)
 With the ls –l command the following should
be noted:
-rw-r--r--
• If the first position is a dash (-) then it is an
ordinary file
• If it is a d then it is a directory
• If it is a l then it is a link file
 ls –p command also identifies directories. All
directories have a backward slash at the end.
Copying Files
 To copy a file use the cp (copy) command.
Syntax:
cp filename newfilename
where
filename is the file you wish to copy and
newfilename is the file you are creating.

Eg: cp listing.etc.txt listing.etc.bk


Renaming Files
To rename a file use the mv (move)command
Syntax:
mv oldfilename newfilename
where
oldfilename is the original filename and
newfilename is the new filename.

Eg. mv practice2 workfl


Rename listing.etc.txt to listing.etc.text
Deleting Files
 To delete a file use the rm command.
Syntax: rm filename
where filename is the name of file you which to delete.

Eg: rm listing.etc.bk

 NOTE the rm command is very dangerous …and


should be executed with utmost care. We should
practise using the –i options, which will required
confirmation of the deletion operation.
Eg: rm -i listing.etc.bk
Creating Links between Files
A link in UNIX is a pointer to a file or directory.
There are two types of links :
 Symbolic links or Soft Link
 Hard Links
A symbolic link( soft link), is a special kind of file
that points to another file, much like a shortcut in
Windows.
A hard link is actually the same file that its links to
but with a different name (i.e. we have two copies of
the same file with different names.). We cannot
create a hard link for a directory.
Creating Links between Files (con’t)
The ln command creates a link (both hard and
symbolic), which "points" to the file
 To create a symbolic link to a file within the
same directory, type:
ln -s originalFile linkName
 To create a symbolic link to a file within the
another directory, go to the directory first, then
type:
ln –s pathname/originalfile linkname
(essentially you just specifying the correct path name for the file)
Creating Links between Files (con’t)
Example:
Linking a file within the same directory
Enter the command
ln –s listing.etc.txt listing.etc.ln
Example:
Creating a symbolic link from another directory
Enter the commands:
cd $HOME/funxdir
ln –s $HOME/listing.etc.txt listing.etc.ln
Printing Files
To print a file use the lp command
Syntax: lp filename
( this would print to the systems default
printer)

Syntax: lp -d printername filename


( here printername is the printer you choose
to print to)
Printing Files (con’t)
To know which printer on the system is the default
printer, use the command:
lpstat -d

To get the status information on all printers, use the


command:
lpstat -p
Printing Files (con’t)
To get the order of request of print job on the
printers, use
the command lpq

To get all information on all printer, use the


command:
lpstat -t
Printing Files (con’t)
To get a list of your print jobs, use the command:
lpstat –u userid

To cancel your print job, use the command:

cancel destination-request id
eg. cancel csprinter-10
Printing Files (con’t)
When you send a print job, the system checks
the /etc/printers.conf file to see the default
printer.

However there are two variables that you can used


( one or the other, but not both) that you can set
locally to hold the name of your default printer.
The variables are: LPDEST or PRINTER
Printing Files (con’t)
Enter: lp testfile Which printer is
selected?

Once the LPDEST or PRINTER variable is set the


system would check the one that was set first.

If neither is set the /etc/printers.conf file is


checked and the default printer is used.
System startup and Login shell
System startup and Login shell
 When the system starts up, the first process is init.
 init the ancestor of all subsequent UNIX processes
and the direct parent of each user's login shell.
 Each process has a PID, and init (sbin/init) being
the first process has a PID of 1.
 init initialises the system and then starts other
processes to set up standard input (stdin), standard
output (stdout) and standard error (stderr)
 stdin comes from the keyboard, stdout & stderr
goes to the screen.
System startup and Login shell
It is here that you are presented with a login prompt
 After entering your login name you are prompted
for a password
 The /bin/login program verifies your login
name and password using the passwd file.
 The login program sets up your working
environment, and sets the HOME, SHELL,
USER and/or LOGNAME with information
extracted from the passwd file.
System startup and Login shell
 When login has finished, it then executes the
program found in the last entry of the passwd
file. In most cases the value is one of the
following:
 /bin/sh – Bourne shell
 /bin/csh – C shell
 /bin/ksh – Korn shell
Questions…

Questions?
End of Lecture

You might also like