Download - The Unix Shell
The Unix Shell
OperatingSystem
shell The shell is a command interpreterIt forms the interface between auser and the operating system
When you log in to a Unix system,a shell starts running. You interactwith the shell
this is the shell prompt
the SHELL environment variable tellswhich shell is being used
this is where the shellprogram is located/bin/bash
Unix Shells
Shell name Program (Command) namerc rcBourne Shell shC Shell cshBourne Again Shell bashZ shell zshKorn Shell kshTC tcsh
Unix Shells
Shell name descriptionrc a very simple shell - similar to shBourne Shell developed at Bell labs - popularC Shell bsd unix - based on C languageBourne Again Shell open source version of sh - linux/osX Z shell extended sh - very large shellKorn Shell like sh but extensive scripting languageTC c shell with emacs-like command line
you can change shells by typing the shell command
return to the default shellby typing “exit”
The shell command line
prompt showscurrent directory.~ is your home directory
commandlist of arguments
command options (usually preceded with a hyphen)
What happens when you type a command?
The shell parses the command lineIt looks for a program that matches the commandIt starts a new process and runs that programWhile the program executes, the shell sleepsWhen the program is done, the shell wakes upIt displays its promptIt waits for the user to type another command
Commandstdin stdout
Commands generally get their input fromstdin and send their output to stdout
if you run the who command, the system tells you who is logged in and at what terminal.
but . . . this is really a file in the Unix file system thatrepresents a real device, in this case a terminal
commands read from and write to this file!
the cat command is a good example. It takes its input from a fileand outputs to stdout.
if you type the command with no parameters, it takes its inputfrom stdin. It will do this until you type ctrl-D (end of file).
Redirection
You can cause the shell to get its input from some placeother than stdin or send its output to some place other than stdout by using redirection.
Redirecting standard output
command [arguments] > filename
redirect output to newduh.txt
Concatenating Files with cat command
Redirecting standard input
command [arguments] < filename
cat takes its input from the file suppliesand writes to standard output
Appending standard ouputto a file
command [arguments] >> filename
Pipes
The shell uses a pipe to connect the output of onecommand to the input of another command.
Using a pipe
command_a [arguments] | command_b [arguments]
The tr command translates each character in itsinput stream to the corresponding character in itsoutput stream.
Running a command in the background
command_a [arguments] &
the & tells the shell to run the command inthe background. This means that the shellprompt will appear immediately and you cantype in new commands without waiting for the background command to finish.
Some useful Unix Commands
cal
cal month yearcal yearcal
displays a monthly calendar
cat
cat [options] file-list
concatenates files end to end
-e marks end of each line with a $-n displays line numbers
cd
cd [directory]
change to the specified directory
cd with no arguments changes to your home directory
chmod
chmod [options] mode file-list
symbolic u user + add permission g group - remove permission o other a all
changes permissions
chmod
chmod [options] mode file-list
absolute xxx - a binary encoded value
777 - everyone can read, write, execute 755 - owner can read, write, execute, others can read, execute
changes permissions
cp
cp [options] source-file destination-file
-i interactive, prompt if this will overwrite an existing file-r recursive, for directories, copies everything
copies files
diff
diff [options] file-1 file-2
compares files
find
find directory-list criteria
recursively searches in a given directory
-name file-name-type file-type-user user-name
finger
finger [options] user-list
displays information for logged in users
-l detailed listing-s brief listing
grep
grep [options] pattern [file-list]
searches files for a given pattern
-c display the number of lines that match-i ignore case-l display the filenames where a match is found-n displays each line with its line number in the file
grep uses regular expressions in its pattern matching
Consider the file testregex that contains the lines
ringringingbringingtalktalkingwalking
Simple strings
Consider the file testregex that contains the lines
ringringingbringingtalktalkingwalking
> grep ring testregexringringingbringing
Period - represents any character
Consider the file testregex that contains the lines
ringringingbringingtalktalkingwalking
> grep .ing testregexringringingbringingtalkingwalking
[ ] - represents a set of characters
Consider the file testregex that contains the lines
ringringingbringingtalktalkingwalking
> grep [tw] testregextalktalkingwalking
^ - matches a string at the beginning of a line
Consider the file testregex that contains the lines
ringringingbringingtalktalkingwalking
> grep ^ri testregexringringing
$ - matches a string at the end of a line
Consider the file testregex that contains the lines
ringringingbringingtalktalkingwalking
> grep ing$ testregexringringingbringingwalkingtalking
head
head [number] file-list
displays the first number lines of a file
kill
kill [signal-number] PID-list
kills a process
ln
ln [option] existing-file link-name
create a link to a file
by default ln creates a hard link. Hard links must bein the same directory as the file being linked to.
the -s option creates a symbolic link. Symbolic links canbe across file systems and directories.
ls
ls [options] file-list
list information about one or more files
-a list all entries, including invisible files-l show all file information-r shows all subdirectories
t r w x r w x r w x links owner group size date_last_modified filename
type of filed directory- regular fileb block devicec character devicel symbolic linkp pipes socket
file
ow
ner
gro
up
oth
er
mkdir
mkdir [option] directory-list
make a new directory
-p if the parent directory does not already exist, the create it.
more
more [options] [file-list]
display a file, one screenful at a time
-n number output lines
mv
mv [options] existing-name new-name
move (rename) a file
-f moves regardless of file permissions-i prompts if move would overwrite an existing file
ps
ps [options]
displays the status of existing processes
-a display all processes associated with a terminal-e displays some environment information-l long display-x displays daemon processes
rm
rm [options] file-list
remove a file
-f removes files for which you do not have write permission-i prompts before removing each file-r recursive
rmdir
rmdir directory-list
remove an empty directory
tail
tail [number] [file]
display the last number lines of a file
who
who
display logged in users