Upload
hanhan
View
226
Download
0
Embed Size (px)
Citation preview
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 1 of 24
Command Shells • Description of different types of shell Bourne shell (sh)
• Original UNIX shell written by Steve Bourne of Bell Labs, which is available on all UNIX systems.
• Does not have the interactive facilities provided by modern shells. (e.g. C shell and Korn shell)
• Permits shell scripts to be written and executed. C shell (csh)
• Provides a C-like language with which to write shell scripts.
TC shell (tcsh) • Available in the public domain. • Provides all the features of the C shell together with
emacs style editing of the command line.
Korn shell (ksh) • Shell written by David Korn of Bell labs, which is now
provided as the standard shell on UNIX systems. • It provides all the features of the C and TC shells
together with a shell programming language similar to that of the original Bourne shell.
• It is the most efficient shell. • Advice: use this as your standard interactive shell
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 2 of 24
Bourne Again SHell (bash)
• This is a public domain shell written by the Free Software Foundation under their GNU initiative.
• Widely used within the academic community. • Provides all the interactive features of the C shell (csh)
and the Korn shell (ksh). • Its programming language for shell scripts is
compatible with the Bourne shell (sh). • If Bourne shell (sh) is used for shell programming,
bash is recommended as a complete shell environment.
• Facilities offered by different types of shell
* not the default setting for this shell
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 3 of 24
• Switch to using another shell
In order to switch to another shell for the remainder of the login session, enter the shell command name at the system prompt:
khumbu% ksh
$
This switches from the current shell to the Korn shell. • Example
• Shells available for use on Linux OS (similar choice on
Solaris OS)
Default shell is bash
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 4 of 24
• chsh - allows user to change shell
• bash shell configuration file:
.bash_profile read once only, when you login
.bash_history records last # commands typed to bash shell
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 5 of 24
Commands • Using at to Schedule Delayed Commands
khumbu% at 16:35 at> cat pline.c | mail [email protected] at> <EOT> warning: commands will be executed using /bin/csh job 951842100.a at Tue Feb 29 16:35:00 2004 khumbu%
• Removing Jobs scheduled with at khumbu% at -l 763250700.a Wed Mar 9 16:05:00 2004 khumbu% at -r 763250700.a khumbu% at -l khumbu%
• Requesting Notification of Background Job Status Changes with notify
khumbu% notify [ %job ]
• Splitting Files
$split [ -n ] [ in-file [ out-file ] ]
This command reads the text file in-file and splits it into several files, each consisting of n lines (except possibly the last file).
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 6 of 24
• Encrypting a File
%crypt [ key ] < clearfile > encryptedfile
• key is any phrase. • The encryption key need not be complex. In fact, the
longer it is, the more time it takes to do the decryption.
• Decrypting a File
$crypt [ key ] < encryptedfile> clearfile
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 7 of 24
Archives • Creating an archive file:
tar cvf tarfile.tar filenames (directory_name)
• The command options cvf modify the way the tar
command works. • By convention the name of a tarfile archive ends in .tar • When you create a tarfile of a directory all the
subdirectories are added • Often tarfiles are very large and in order to reduce
their size, they should be compressed • When creating a tarfile, never specify a full pathname
(i.e. with first character /) for the input files, as tar will attempt to recreate the files in a directory with exactly the same name when you extract them. Use a relative pathname instead.
• Examples:
1. Create a tarfile containing several files:
tar cvf data.tar *.dat
2. Create a tarfile of a directory:
tar cvf project.tar Project
project.tar includes directory Project & its content: Project/Makefile Project/main.c Project/screen.c Project/setup/configuration
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 8 of 24
3. Create a tarfile containing several directories: tar cvf book.tar text illustrations
Creates a tarfile book.tar containing the directories text and illustrations.
• Making a tarfile smaller
1. Excluding redundant files (that can be recreated from files contained in the tarfile)
Examples:
.o (object) files - can be re-created by compiling the program source code.
.dvi files - can be re-produced by processing TeX and LaTeX source files. .ps (postscript) files - can be re-generated by drawing and publishing packages.
2. Compressing a tarfile (using command compress)
compress project.tar
Creates a file called project.tar.Z. • Examining the contents of a tarfile
1. Display the contents of a tarfile without unpacking it
tar tvf project.tar
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 9 of 24
2. Display the contents of a compressed tarfile without uncompressing and unpacking it using zcat command zcat project.tar.Z | tar tvf -
• Unpacking a tarfile
tar xvf project.tar
Unpacks the contents of the tarfile in the current directory Note: Any file or directory with the same name as the contents of the tarfile will be overwritten! Advice: Go to an empty directory before unpacking a tarfile
• Unpack a compressed tarfile without uncompressing it (using zcat command)
zcat project.tar.Z | tar xvf -
The zcat command recreates the uncompressed tarfile, which is then piped into the tar command to extract the files. tar reads from standard input the uncompressed tarfile because the name of the tarfile is given as “–“ Note: Using zcat command leaves the compressed .Z tarfile intact.
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 10 of 24
• Unpack archived files without uncompressing the tarfile zcat project.tar.Z | tar xvf - Project/main.c
Extracts the file Project/main.c from the tar archive project.tar.Z into the current directory
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 11 of 24
UNIX Online Documentation • UNIX Manual - man pages
Enter man followed by the name of the command to search and offer documentation for Note: Use ctrl-Z to stop manual display
…
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 12 of 24
• Using apropos
apropos equivalent to man - k Example: Find information on commands relating to floppy disks
• UNIX man page organisation
1 User commands 2 System calls 3 Library functions 4 Special files 5 File formats 6 Games 7 Miscellaneous 8 Administrative commands
Note: You can learn useful information by reading the introduction to each section of manual pages
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 13 of 24
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 14 of 24
Processes • Running processes in the foreground
When a command is entered at the shell prompt, the shell forks (creates) a child process in which the command will be executed. The parent process (the shell) waits for the command to complete and for its child process to die before allows the user to enter another command, displaying the shell prompt. Commands entered and executed in this way are known as foreground processes.
• Process ID (pid)
PID = A unique integer value that identifies a process.
• Cancelling a foreground process To cancel the process that is currently running, enter the key combination:
Ctrl-c (or ^C)
find / -name '*.ps' -print
^C find . -name '*.ps' –print
The user seeks files with extension .ps starting from the root directory instead from the current directory and cancels the command
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 15 of 24
• Suspending a foreground process
To suspend the process that is currently running, enter the key combination:
Ctrl-z (or ^Z)
This stops the current foreground process. Note: If Bourne shell is used, the process has to be killed later on. Other shells provide a facility to restart the process in the foreground or as a background job.
find / -name *.c -print find: cannot read /dev/diag
... ^Z [1]+ Stopped find / -name *.c -print
ps 22926 p9 S 0:03 -Tcsh (Tcsh) 29857 p9 T 0:00 find / -name *.c -print 29859 p9 R 0:00 ps
* do something urgent * fg
The user enters a command to find files with the extension .c starting at the root directory. Realising that this process takes long time to run the user stops it while he/she does something else.
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 16 of 24
The user checks to see that the process still exists then do something urgent The suspended process can be restarted in the foreground or run as a background job.
• Commands
fg Bring program into the foreground to continue running
& run in parallel ps view current active processes - a Show all processes (terminals) - u User-orientated output
%CPU - amount of CPU used %MEM - amount of RAM used SIZE - virtual memory used RSS - real memory used TIME - CPU time used STAT - process status:
R Running S Sleeping D Uninterruptible sleep T Stopped Z Zombie process (ended but has yet to free up
its resources) C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 17 of 24
jobs Show processes that are running kill kill active processes
Example:
throung:~/c$ kill 14420
lpr Printing files -Ppr Send printout to printer pr pr Formatting printouts lpq View printer queue
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 18 of 24
• Placing foreground processes in the background
1. Suspend the foreground process. 2. Enter the bg (background) command to move the
process into the background. lpr -Pps23 part[1-8].ps ^Z Stopped bg [1] lpr -Pps23 interface.ps & jobs [1] Running lpr -Pps7 interface.ps
The user starts a foreground process to print several files. The process is then stopped and the job moved into the background. The state of this job is then checked.
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 19 of 24
• Running processes as background jobs
Any process can be run in the background as a job while the user continues to have access to the shell prompt Add at the end of the command line the character:
& (ampersand)
find /usr/man -name *.1 -print > sect1 &
[1] 2548
The shell forks a child process to run the command and displays the job number ([n]) and the PID. The shell prompt returns and further commands can be given end executed. Advices:
1. Redirect the standard output for a command that is being run in the background to a file. This prevents the output from the command appearing on your screen and interrupting your current work. 2. If the command is likely to produce error messages, the standard error can be redirected as well in order to prevent error messages from appearing on the screen. 3. Do not run as a background job an interactive command that requires the user to type anything at the terminal. The job would stop and wait for user input indefinitely.
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 20 of 24
• Commands
find Search for files in directory hierarchy (full word options!)
Create a list of all files and directories below the current directory:
find . -print
Limit the output to C source files only
Important Applications and Tools
rlogin Start a terminal session on a remote host
Assumes that the account name on the remote system is the same as on the home system
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 21 of 24
telnet Communicate with remote system via TELNET protocol
finger See who is logged in to a remote system / find information about a specific user.
gcc C compiler (e.g. gcc pline.c -o pline)
GNU C/C++ compiler
manang% gcc ptest.c –o ptest
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 22 of 24
ftp File transfer protocol. Copying files from other Internet sites
Some sites have anonymous ftp capabilities. This means that there is no need for an account on these machines in order to access data.
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 23 of 24
• Common ftp service commands
Connection-Related Commands open Open a connection to specified host. close Close current open connection. quit Close current connection and exit ftp.
File Transfer-Related Commands
binary Change file representation type to binary. ascii Change file representation type to ASCII. put Transfer a single file from the local to the
remote host. mput Transfer multiple files from the local to
the remote host. get Transfer a single file from the remote to
the local host. mget Transfer multiple files from the remote to
the local host.
File and Directory-Management Commands
cd Change the remote host's current working directory (UNIX cd).
lcd Change the local host's current working directory (UNIX cd).
cdup Change remote's current working directory to be the parent directory (UNIX cd ..).
dir List the remote's current working directory (UNIX ls).
Pwd Print the remote's current working directory (UNIX pwd).
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004
EE206: Software Engineering IV 2.2 Shells, Archives and Processes page 24 of 24
mkdir Make a new directory on the remote (UNIX mkdir).
rmdir Delete a directory on the remote host (UNIX rmdir).
rename Change the name of a remote file or directory (UNIX mv).
delete Delete a remote file (UNIX rm, with one file specified).
mdelete Delete multiple remote files (UNIX rm, with multiple files).
Miscellaneous Commands
? Obtain help about ftp. ! Escape shell.
Notes: Transferring ASCII text files between
UNIX machines is slightly faster if binary type is used
Using binary type to transfer ASCII text files between UNIX and non-UNIX machines may corrupt the file.
C:\Gabi\EE206\2003-2004\worddocs\202_UNIX_Intro.doc Last saved by Gabriel Muntean 09/02/2004