linux system administration - Chapter V
Linux-bashshell
Outline
What is shell
system shell Linux as the operating system shell, to provide a user interface using the operating system. It is a command language interpreter and collectively command programming language.
shell is the interface between the user program and the Linux kernel, the Linux kernel imagine if a center of the sphere, that is, around the outer shell of the kernel. When the transfer command from the shell or other program to Linux, the kernel will react accordingly.
A shell is a command language interpreter, it has its own built-in shell commands, shell system can also be invoked by other applications. User-input command at the prompt and then passed by the shell first explain the Linux kernel.
Linux The shell has a plurality of types, the most common ones are the Bourne shell ( SH ), C shell ( csh ) and the Korn shell ( ksh ).
Three kinds of shell have advantages and disadvantages. Bourne shell is UN IX originally used in the shell , and in each UN IX can be used on. Bourne shell in the shell is quite good programming, but in dealing with the interaction with the user doing as good as several other shell .
Linux operating system, the default shell is the Bourne Again shell , it is the Bourne shell extensions, referred to as Bash , the Bourne shell is fully backward compatible , and Bourne shell increase based on the much enhanced features. Bash on / bin / bash , which has many features that can provide such as command completion, command editing, and command history list and other features, it also contains a lot of C shell and the Korn shell of the advantages, there is a flexible and powerful programming interfaces , while there are very friendly user interface .
bash shell is the default shell Linux
5.1 bash shell Tutorial
5.1.1 SHEEL grammar
Bash sheel Run
Shell command option parameters are separated by a space between each basic constituent elements.
[Root @ Redhat7 ~] # ls -a / tmp / parameters can be written with a plurality of
5.1.2 View shell currently in use
[root@Redhat7 ~]# echo $SHELL
/bin/bash
5.1.3 View SHELL system available
[root@Redhat7 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
5.1.4 bash characteristics completion
Enter the first few Bash sheel command or parameter, you can press the tab completion.
5.1.5 Bash Features: Command Shortcut
Use the most is
Ctrl + c to terminate the foreground task
Ctrl + l clear clear screen and equivalents
Ctrl + a cursor jumps to the first line
Ctrl + e cursor jumps to the end
History View System Command History
-w command history is saved to the history file
-c Clear Command History
-d command to delete the history of the N-th row
[root@Redhat7 ~]# history
342 ls /etc/sysconfig/network-scripts/ifcfg-eno16777736
343 cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
344 ls /etc/sysconfig/network-scripts/ifcfg-eno16777736
! Line numbers, fixed-line command execution
[root@Redhat7 ~]# !344
ls /etc/sysconfig/network-scripts/ifcfg-eno16777736
/etc/sysconfig/network-scripts/ifcfg-eno16777736
! Command, recently ordered the execution
[root@Redhat7 ~]# !ls
ls /etc/sysconfig/network-scripts/ifcfg-eno16777736
/etc/sysconfig/network-scripts/ifcfg-eno16777736
5.1.6 bash features - alias
Using command aliases users often make complex commands Using the simplistic, can use it "alias command alias name =" command to create belong Your Own
command aliases, to cancel ⼀ command aliases, it is unalias Using the alias name command.
1 // Definitions temporary alias, if1 ⽹ card for viewing eth0 alias
[xuliangwei the root @ ~] # = IF0 Alias 'the ifconfig eth0'
[xuliangwei the root @ ~] # IF0
// 2. Permanently define the system efficiency ⽣ alias, the alias command add ⾄ current user environment configuration files Use
[xuliangwei the root @ ~] # echo "Alias the ifconfig = 'the ifconfig eth0'" >> ~ / .bashrc
// As will alias command to add ⾄ / etc / bashrc, Use of all households ⽣ effect
// 3. cancellation temporary alias
[root @ xuliangwei ~] # unalias ifconfig
/ Etc / bashrc global environment variables
Home directory .bashrc individual user environment variables
Global environment and the individual user environment variables = Global equal mom make you go blind, individual final decision or you.
5.1.7 Bash: command execution process
Internal Command: shell command that comes with the program.
External command: the executable program in a path of the system PATH variable.
When we perform it commands, command Perform the entire process is as follows:
1. determine whether the command Perform an absolute path
2. determine whether there is an alias command
3. Use Analyzing the user input command START is the internal or external command
4. Perform internal commands directly, detect the presence of an external command buffer
5. detecting the PATH, there is YES, no matter if the error
Type the command type detection
[root@Redhat7 ~]# type -a cd
cd is a shell built
cd Shi / usr / bin / cd
[root@jsmongodb ~]# type -a mongod
mongod is /app/mongodb3.0/bin/mongod
The PATH variable definition, is to tell the location for Using Bash to perform to an external command stored, Bash will make individual scanned in these paths.
1 modify environment variables personal command
Personal modify environment variables
[root@jsmongodb ~]# cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
be
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
MONGODB_HOME=/app/mongodb3.0
export MONGODB_HOME
PATH=$MONGODB_HOME/bin:$PATH
export PATH
[root@jsmongodb ~]# echo $PATH
/app/mongodb3.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2 modify global command environment variables
// PATH composed of a plurality of paths, each path Using colon interval between values on these paths add and delete operations will affect the look of Linux Bash command interpreter.
Global modify environment variables
// modify the PATH variable
[xuliangwei the root @ ~] # PATH = / Soft / bin: $ PATH
// is writing / etc / profile configuration files permanently ⽣ effective
echo 'export PATH = / soft / bin: $ PATH' >> / etc / profile
5.1.8 Bash characteristics path expand
Using the Linux Shell launched under the path includes braces, separated by commas, braces ⾥ this content will be expanded to form the list.
[root@Redhat7 ~]# rm -rf /tmp/{zz,yy}
[root@Redhat7 ~]# mkdir /tmp/zz/a/b /tmp/yy/a/b -pv
mkdir: directory has been created "/ tmp / zz"
mkdir: directory has been created "/ tmp / zz / a"
mkdir: directory has been created "/ tmp / zz / a / b"
mkdir: directory has been created "/ tmp / yy"
mkdir: directory has been created "/ tmp / yy / a"
mkdir: directory has been created "/ tmp / yy / a / b"
[root@Redhat7 ~]# mkdir /tmp/{zz,yy}/a/b –pv
1. Characteristics
[root@Redhat7 ~]# echo {1.55}
{1.55}
[root@Redhat7 ~]# echo {1..55}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
. Subsequent to is meant.
[root@Redhat7 ~]# mkdir -pv /iso7.2/syjhct/iso2019{1..12}
mkdir: create directory has "/iso7.2/syjhct"
mkdir: create directory has "/iso7.2/syjhct/iso20191"
mkdir: create directory has "/iso7.2/syjhct/iso20192"
mkdir: create directory has "/iso7.2/syjhct/iso20193"
mkdir: create directory has "/iso7.2/syjhct/iso20194"
mkdir: create directory has "/iso7.2/syjhct/iso20195"
mkdir: create directory has "/iso7.2/syjhct/iso20196"
mkdir: create directory has "/iso7.2/syjhct/iso20197"
mkdir: create directory has "/iso7.2/syjhct/iso20198"
mkdir: create directory has "/iso7.2/syjhct/iso20199"
mkdir: create directory has "/iso7.2/syjhct/iso201910"
mkdir: create directory has "/iso7.2/syjhct/iso201911"
mkdir: create directory has "/iso7.2/syjhct/iso201912"
5.1.9 shell metacharacters
shell addition wildcard outside the shell after the first advance is responsible for parsing the processed results to the command line outside the shell as well as a series of their other special characters.
shell metacharacters |
|
character |
Explanation |
= |
Variable name = value, assign values to variables. Note = about keeping up with the variable name and value, without spaces |
$ |
The replacement value of the variable, $ variable name is replaced with the value of shell variables; |
> |
prog> file redirect standard output to a file. |
>> |
prog >> file will be appended to the standard output file. |
< |
prog <file acquired from the standard input file in the file |
| |
Pipe command, for example: p1 | p2 to p1 p2 standard input standard output as |
& |
Command running in the background, the biggest advantage is that without waiting for the end of the command, you can continue to enter commands in the same command line |
() |
In sub-shell command execution |
{} |
Run in the current shell, or in the alternative to define a variable range (e.g., above $ {name} variable usage). |
; |
Command terminator. E.g. P1; p2 represents a first implementation of p1, p2 then performed |
&& equivalent and |
At the end of a previous command, if the return value is true, proceed to the next command. |
|| equivalent or |
At the end of a previous command, if the return value is false, proceed to the next command. |
! |
The command execution history |
~ |
home directory |
Special symbol (#)
Note the symbol (Hashmark [Comments])
! 1. In the beginning of the line shell file as shebang mark, # / bin / bash;
2. Elsewhere use as a comment, in a row, behind the content # and will not be executed unless;
3. However, with single / double quotes, # a # character itself as having no annotation effect.
Special symbols (.)
Dot (dot command [period]).
1. bash builtin equivalent source, such as:
#!/bin/bash
./mongod
5.1.10 shell wildcard
* On behalf of any length matching character
? Representatives matches any single character
[] Represents the specified range of characters list
Whether ;; front command is completed later will execute the command
&& front end of a command, if the return value is true, proceed to the next command. *
|| the end of a previous command, if the return value is false, proceed to the next command. *
Any single character [list] in the list matches a [xyz] and only one character and between b a to b, and only x or y or z, as: axb, ayb, azb.
[! List] matches [! 0-9] with a list of any other single character between a and b a b and only one character, but can not be a number, such as axb, aab, ab, etc.
[C1-c2] matches any single character in c1-c2 a [0-9] and b ba has between one and only one character, the character is a number between 0-9, such as A0B, A1B, ... , a9b.
[root@Redhat7 ~]# touch /syjjjj/{xz,xy,yz}
[root@Redhat7 ~]# ls /syjjjj/
x xy xyz xz y yz z
[root@Redhat7 ~]# ls /syjjjj/[xyz]
/ Syjjjj / x / syjjjj / y / syjjjj / z
[root@Redhat7 ~]# ls /syjjjj/[x,z]
/ Syjjjj / x / syjjjj / z
[root@Redhat7 ~]# ls /syjjjj/[x-z]
/ Syjjjj / x / syjjjj / y / syjjjj / z
[root@Redhat7 ~]# ls /iso7.2/Packages/*DB*
/iso7.2/Packages/perl-DBD-MySQL-4.023-5.el7.x86_64.rpm
/iso7.2/Packages/perl-DBD-Pg-2.19.3-4.el7.x86_64.rpm
/iso7.2/Packages/perl-DBD-SQLite-1.39-3.el7.x86_64.rpm
5.1.11 Bash features: the escape character
Shell interpreter provides a wealth comes in handy escape character to achieve character processing and command substitution.
4 class frequently-used escape character
The backslash (\): the backslash after the screen for ⼀ variables into a string.
Single quotation marks ( ''): wherein all variables escaped as a simple string.
Double quotes ( ""): wherein the variable attribute retention, without escaping into ⾏.
Backtick ( ``): the basic need, the results of which command returns YES. Difficult to use, general use $ (command)
[root@Redhat7 ~]# echo the directory is $PWD
the directory is /root
The backslash (\): the backslash after the screen for ⼀ variables into a string.
[root@Redhat7 ~]# echo the directory is \$PWD
the directory is $PWD
[root@Redhat7 ~]# syj=hct
[root@Redhat7 ~]# echo $syj
hct
[root@Redhat7 ~]# echo \$syj
$ SYJ
[root@Redhat7 ~]# echo $syj+$syj
hct hct +
[root@Redhat7 ~]# echo '$syj+$syj'
$ + $ SYJ SYJ