Tải bản đầy đủ (.ppt) (42 trang)

linux crash course chapter 08 3

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (202.22 KB, 42 trang )

Chapter 8:
The Bourne Again Shell
It’s a command interpreter, it’s a
programming language, and it makes a
mean martini


In this chapter …








Background
Startup files
Basic Scripting
Variables
Processes
History
Aliases


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 …


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


Login shells
• Login shells are called with the
--login option
• We don’t usually do this – it’s done
for us
• Will first run /etc/profile, which
contains global default settings
• Next, it will attempt to run
~/.bash_profile
~/.bash_login
~./profile



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


Interactive nonlogin shells
• Shells that you spawn yourself by
typing bash
• Runs ~/.bashrc
– This file is usually called by ~/.bash_profile
for login shells
– Often this file will also run /etc/bashrc, which
again contains system defaults


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


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


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


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)


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



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?


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


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)


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 #


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



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


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


Shell Variables
• Variables are stored in memory

and contain data
• Variables can be pre-defined by
operating system, shell, or can
be user created
• Variable names can consist of
letters, digits and underscores
– Cannot start with a number


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


Keyword Variables
• Certain variables are set already by
the shell
• Have very standard names and are
essential
• Ex:
– 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


Shell Variables
• To reference a variable’s contents,
proceed the variable name with a $
• Ex:
echo HOME
HOME
echo $HOME
/home/jhowell

• $variable is actually short for $
{variable}


Shell Variables con’t
• To remove the value of a
variable, just set it equal to a
null string
– Ex. myvar=

• To remove a variable altogether,
use unset
– Ex. unset myvar



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)


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×