Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 42 trang
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
Nội dung
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 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 not run any of the aforementioned startup files • They 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 Processes • Processes have a hierarchy similar to the file system • init process (PID 1), is the root process • It then forks other processes (its children) • Those processes fork more processes • Etc … so we get a tree-like structure • To see this structure use ps forest 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 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 History • 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 History con’t • There are lots of ways to take advantage of the history • Use the up and down arrows to cycle it • Use !event_num to re-execute a previous command • There’s also a lot more with fc and the Readline Library fc • fix command • fc –l lists recent commands • fc –l [string] lists most recent commands, starting with one that contains string • fc [string] will open an editor to edit command • FCEDIT variable lets you set the editor Readline Completion • The Readline library brings us the wonders of tab completion • Command completion • Pathname completion • Start typing, then hit tab to auto-complete • If there is more than one match, hit tab again to see a list Aliases • 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 functions • 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 Command Line Processing • History Expansion • Alias Substitution • Command Line Expansion – – – – – – – – Brace Expansion Tilde Expansion Parameter/variable Expansion Arithmetic Expansion Command Substituion Word Splitting Pathname Expansion Process Substitution Brace Expansion • Used to specify multiple filenames when pathname expansion doesn’t really apply • Ex: mkdir ver_{A,B,C,Final} Expands to mkdir ver_A ver_B ver_C ver_Final Tilde Expansion • ~ becomes the path to your home directory • ~username becomes to the path to username’s home directory • ~+ becomes the current working directory • ~- becomes the previous working directory 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 Command Substitution • $(command) is replaced with the output of command • Could also be written as `command` • Command is run in a separate subshell, usual rules apply Process Substitution • Substitutes a process where a filename is required • (command) places the input of the command as a filename argument • Ex: uniq