1. Trang chủ
  2. » Công Nghệ Thông Tin

linux crash course chapter 08 3

42 116 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 42
Dung lượng 589,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Some history• Very early UNIX shell was written by Steve Bourne – called the Bourne Shell sh • Another popular shell from UNIX days was the C shell csh – a C programmer’s best friend • L

Trang 1

Chapter 8:

The Bourne Again Shell

It’s a command interpreter, it’s a programming language, and it makes a

mean martini

Trang 3

Some history

• Very early UNIX shell was written by Steve

Bourne – called the Bourne Shell (sh)

• Another popular shell from UNIX days was the C shell (csh) – a C programmer’s best friend

• Linux sought to emulate Bourne and make it

better – Bourne Again Shell (bash)

• UNIX stole from bash, David Korn released the Korn shell (ksh)

• POSIX standards …

Trang 4

Starting bash

• When bash is called, various

startup files are run to issue

commands and define environmental variables

• Which startup file(s) begin

depends upon how bash is called

• Use these startup files to make

the shell work for you

Trang 6

Login shells, con’t

• Commands in those three files can override the defaults in

/etc/profile

• Once one of those files are

executed, control is passed to the user

• When the user logs out, bash runs

~/.bash_logout

– Usually clears temporary information

Trang 7

Interactive nonlogin shells

• Shells that you spawn yourself by typing bash

Trang 8

Noninteractive shells

• These are the shells used to run

scripts

• These shells do not run any of the

aforementioned startup files

• They do however inherit the calling

shell’s environmental variables marked for export

• So basically anything you set for the login shell is set for the

noninteractive shell

Trang 9

Working with Startup Files

• In the end, these startup files are just shell scripts

• Obey the same rules and conventions that scripts must use for the

particular shell you’re using

• Most important files are

probably bashrc and bash_profile

• Simplify – have bash_profile

call bashrc

Trang 10

Startup Files, con’t

• Just edit the startup files in

your favorite editor

• When done, you can apply changes

to your current shell using

either or source

• Otherwise, logout and login again

Trang 11

Symbol Commands in bash

• ( ) – run contents in a separate subshell

– Creates a separate process with unique ID

• $( ) – command substitution

– Runs contents and replaces with output

• (( )) – evaluate contents as

arithmetic expression or equation

• $(( )) – same as above, just

expressions (no equal signs)

Trang 12

Redirection Revisited

• Standard Input < (or 0<)

• Standard Output > (or 1>)

• Standard Error 2>

• Recall that we can redirect standard out and standard error to different

locations

• When you use the pipe ( | ), remember

it only passes standard output to

standard input; standard error still goes to it’s normal location

Trang 13

Redirection con’t

• Ex: file x exists, file y does not cat x y | tr “[a-z]” “[A-Z]”

THIS IS FILE X

cat: y: No such file or directory

• Notice that only standard out is piped to tr

• So how would we pipe standard

error as well?

Trang 14

Redirection con’t

• Duplicating streams

• [n]>&[m]

– Redirects stream n to stream m

• So to send std error to std out, 2>&1

• So for our previous example

cat x y 2>&1 | tr “[a-z]” “[A-Z]”

THIS IS FILE X

CAT: Y: NO SUCH FILE OR DIRECTORY

Trang 15

Writing simple scripts

• A shell script is, in its simplest form, just a list of shell commands

in a file

• File must have read and execute

permissions to call implicitly

• If script is in PATH, just call it from command line

• If not in PATH, include pathname

(such as /myscript)

Trang 16

Shell Scripts

• To specify what shell (or program)

to use to execute commands, make

first line:

#![absolute path to program/shell]

• Without this line, whatever shell you are in when calling the script

is used

• To make comments in the script,

begin the line with #

Trang 17

Shell Scripts con’t

• When a command is issued, fork

creates a new process (subshell)

• If the command is a binary

executable, exec makes a system

call to run the binary

• If the command is a shell script, the subshell itself runs the script

• When done, subshell closes and

returns control to calling shell

Trang 18

Separating and Grouping Commands

• You can have multiple commands on

a single command line, they just need to be separated

– ;

– NEWLINE

– &

• To continue a long command on

several lines, end a line with \ and press return

Trang 19

Separating and Grouping con’t

• To group commands to control

execution, use parenthesis

• Ex:

(a; b; c) | (d; e)

Commands a, b, and c are executed first, and once c finishes standard output is piped to the result of commands d and e

• Each set of parenthesis runs in its own subshell

Trang 20

• Variable names can consist of

letters, digits and underscores– Cannot start with a number

Trang 21

Shell Variables con’t

• In bash, to assign a value to a

variable:

VARIABLE=value

• Notice there are no spaces around

the equal sign

• If VARIABLE does not exist, it is

created and set

• If it does exist, the current value

is overwritten

Trang 22

– HOME – contains user’s home directory path

– PATH – contains user’s path

• Change these carefully – you can

confuse the shell and lock yourself out

of places

Trang 24

Shell Variables con’t

• To remove the value of a

variable, just set it equal to a null string

Trang 25

Shell Variable Attributes

• You can use declare or typeset to define attributes when you create

a variable

-a: create an array

-f: create a function

-i: create an integer

-r: make variable readonly

-x: make variable global (export)

Trang 26

Attributes con’t

• Most of these we’ll come back to

when we start doing shell scripting

• Making a variable readonly can also

be accomplished by the readonly

Trang 27

Keyword Variables

• HOME – your home directory

• PATH – your path

• MAIL – location of mail storage

• PS1 – your primary prompt

• PS2 – secondary prompt

• IFS – input field separator (BEWARE!!!)

• Plus many more … just type set to see current shell variables (keyword

variables usually all caps)

Trang 28

• Those processes fork more processes

• Etc … so we get a tree-like structure

• To see this structure use ps forest

Trang 29

Processes con’t

• When a process (such as the shell)

calls another process (like a command

or script), it calls fork to create a new process, then goes to sleep

• If the process is sent to the

background, fork is called but sleep

is not

• If a builtin is called, neither fork nor sleep is called

Trang 30

Processes con’t

• fork creates a new process,

assigns it a unique PID, and

associates it with its parent via the PPID

• When a process forks a child

process, the current shell

variables *are not* passed on,

unless marked for export

Trang 31

• bash contains command history

mechanism inherited from the C Shell

• Use the builtin history to view your current history

• The history is, by default, stored in your home directory in a file

called bash_history

• History entries are ordered, the last entry being the most recently executed

Trang 33

• fix command

• fc –l lists recent commands

• fc –l [string] lists most recent

commands, starting with one that

Trang 34

Readline Completion

• The Readline library brings us

the wonders of tab completion

Trang 35

• An alias is just another name for another command (or commands)

• Ideally, the alias is shorter

and/or easier to remember

• Syntax (bash):

alias alias=‘commands_to_alias’

• Ex: alias ll=‘ls -l colors’

• Single vs double quotes –

variable substitution

Trang 36

• Like a shell script stored in memory

• Set like a variable (and use unset

to delete)

• [function] funct_name()

{

}

• Or use typeset or declare with –f

• Like a function? Wanna keep it?

 bashrc

Trang 37

Command Line Processing

Trang 38

Brace Expansion

• Used to specify multiple

filenames when pathname expansion doesn’t really apply

Trang 40

Arithmetic Expansion

• $((expression)) is evaluated

• Any variable names inside are

substituted for their values

• You can omit the $’s in front of the variable names within the

expression

Trang 41

• Command is run in a separate

subshell, usual rules apply

Trang 42

Process Substitution

• Substitutes a process where a

filename is required

• <(command) places the output of

the command as a filename argument

• >(command) places the input of the

command as a filename argument

• Ex: uniq <(cat list | sort)

Ngày đăng: 06/02/2018, 09:55

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN