The Unix Shell

56
The Unix Shell

description

The Unix Shell. The shell is a command interpreter It forms the interface between a user and the operating system. shell. When you log in to a Unix system, a shell starts running. You interact with the shell. Operating System. this is the shell prompt. this is where the shell - PowerPoint PPT Presentation

Transcript of The Unix Shell

Page 1: The Unix Shell

The Unix Shell

Page 2: 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

Page 3: The Unix Shell

this is the shell prompt

the SHELL environment variable tellswhich shell is being used

this is where the shellprogram is located/bin/bash

Page 4: The Unix Shell

Unix Shells

Shell name Program (Command) namerc rcBourne Shell shC Shell cshBourne Again Shell bashZ shell zshKorn Shell kshTC tcsh

Page 5: The Unix Shell

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

Page 6: The Unix Shell

you can change shells by typing the shell command

return to the default shellby typing “exit”

Page 7: The Unix Shell

The shell command line

prompt showscurrent directory.~ is your home directory

commandlist of arguments

Page 8: The Unix Shell

command options (usually preceded with a hyphen)

Page 9: The Unix Shell

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

Page 10: The Unix Shell

Commandstdin stdout

Commands generally get their input fromstdin and send their output to stdout

Page 11: The Unix Shell

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!

Page 12: The Unix Shell

the cat command is a good example. It takes its input from a fileand outputs to stdout.

Page 13: The Unix Shell

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).

Page 14: The Unix Shell

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.

Page 15: The Unix Shell

Redirecting standard output

command [arguments] > filename

Page 16: The Unix Shell

redirect output to newduh.txt

Page 17: The Unix Shell

Concatenating Files with cat command

Page 18: The Unix Shell

Redirecting standard input

command [arguments] < filename

Page 19: The Unix Shell

cat takes its input from the file suppliesand writes to standard output

Page 20: The Unix Shell

Appending standard ouputto a file

command [arguments] >> filename

Page 21: The Unix Shell
Page 22: The Unix Shell

Pipes

The shell uses a pipe to connect the output of onecommand to the input of another command.

Page 23: The Unix Shell

Using a pipe

command_a [arguments] | command_b [arguments]

Page 24: The Unix Shell

The tr command translates each character in itsinput stream to the corresponding character in itsoutput stream.

Page 25: The Unix Shell
Page 26: The Unix Shell

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.

Page 27: The Unix Shell

Some useful Unix Commands

Page 28: The Unix Shell

cal

cal month yearcal yearcal

displays a monthly calendar

Page 29: The Unix Shell

cat

cat [options] file-list

concatenates files end to end

-e marks end of each line with a $-n displays line numbers

Page 30: The Unix Shell

cd

cd [directory]

change to the specified directory

cd with no arguments changes to your home directory

Page 31: The Unix Shell

chmod

chmod [options] mode file-list

symbolic u user + add permission g group - remove permission o other a all

changes permissions

Page 32: The Unix Shell

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

Page 33: The Unix Shell

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

Page 34: The Unix Shell

diff

diff [options] file-1 file-2

compares files

Page 35: The Unix Shell

find

find directory-list criteria

recursively searches in a given directory

-name file-name-type file-type-user user-name

Page 36: The Unix Shell

finger

finger [options] user-list

displays information for logged in users

-l detailed listing-s brief listing

Page 37: The Unix Shell

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

Page 38: The Unix Shell

grep uses regular expressions in its pattern matching

Consider the file testregex that contains the lines

ringringingbringingtalktalkingwalking

Page 39: The Unix Shell

Simple strings

Consider the file testregex that contains the lines

ringringingbringingtalktalkingwalking

> grep ring testregexringringingbringing

Page 40: The Unix Shell

Period - represents any character

Consider the file testregex that contains the lines

ringringingbringingtalktalkingwalking

> grep .ing testregexringringingbringingtalkingwalking

Page 41: The Unix Shell

[ ] - represents a set of characters

Consider the file testregex that contains the lines

ringringingbringingtalktalkingwalking

> grep [tw] testregextalktalkingwalking

Page 42: The Unix Shell

^ - matches a string at the beginning of a line

Consider the file testregex that contains the lines

ringringingbringingtalktalkingwalking

> grep ^ri testregexringringing

Page 43: The Unix Shell

$ - matches a string at the end of a line

Consider the file testregex that contains the lines

ringringingbringingtalktalkingwalking

> grep ing$ testregexringringingbringingwalkingtalking

Page 44: The Unix Shell

head

head [number] file-list

displays the first number lines of a file

Page 45: The Unix Shell

kill

kill [signal-number] PID-list

kills a process

Page 46: The Unix Shell

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.

Page 47: The Unix Shell

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

Page 48: The Unix Shell

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

Page 49: The Unix Shell

mkdir

mkdir [option] directory-list

make a new directory

-p if the parent directory does not already exist, the create it.

Page 50: The Unix Shell

more

more [options] [file-list]

display a file, one screenful at a time

-n number output lines

Page 51: The Unix Shell

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

Page 52: The Unix Shell

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

Page 53: The Unix Shell

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

Page 54: The Unix Shell

rmdir

rmdir directory-list

remove an empty directory

Page 55: The Unix Shell

tail

tail [number] [file]

display the last number lines of a file

Page 56: The Unix Shell

who

who

display logged in users