linux study notes (five) - Understanding and learning BASH

Understanding and learning BASH

This understanding BASH Shell

Bash shell functions

  • Command editing capabilities (history): As long as the command line, press the "arrow keys" you can find instructions before / after the input of so many record instruction where is it? .Bash_history in your home folder within it.
  • Command and file completion feature: ([tab] key benefits)
  • Alias ​​command functions: (alias) alias lm = 'ls -al'
  • Programmed script: (shell scripts)

Query whether the instruction Bash shell built-in commands: type

Example 1: ls inquiries about whether this is a bash built-in command?

[dmtsai@study ~]$ type ls
复制代码

Shell variable function

Access and set the variables: echo, variable setting rules, unset

echo $PATH

Set variables:

[dmtsai@study ~]$ echo ${myname}
<==这里并没有任何数据~因为这个变量尚未被设置!是空的!
[dmtsai@study ~]$ myname=VBird
[dmtsai@study ~]$ echo ${myname}
VBird <==出现了!因为这个变量已经被设置了
复制代码
  • Variables with a variable content in the "=" are linked as follows: "myname = VBird"
  • Equal on both sides can not be directly connected to the blank character, an error as follows: "myname = VBird" or "myname = VBird Tsai"
  • Variable names only letters and numbers, but can not start with a character is a number, the following error: "2myname = VBird"
  • If the variable is a variable amplification content, the available "variable name" Variable} or {accumulating contents, as follows: "PATH =" PATH ": / home / bin" or "PATH = {PATH}: / home / bin "
  • If the variable needs to be performed in another subroutine, you need to export to the variable becomes an environment variable: "export PATH"
  • Way to cancel variable to use unset: "unset variable name" myname cancel settings such as: "unset myname"

: How to enter your current kernel module directory?

[dmtsai@study ~]$ cd /lib/modules/$(uname -r)/kernel # 以此例较佳!
复制代码

In fact, the above instruction can be said to be made two actions, that is:

  1. First operates in the backward apostrophe "uname -r" and get core version 3.10.0-229.el7.x86_64
  2. The above results into the original command, so as to give instructions: "cd /lib/modules/3.10.0-229.el7.x86_64/kernel/"

Environment variable function

Observation env environment variables and common environment variables Description

Observing (including environmental variables and custom variables) all variables set

The results show the impact of language variables (locale)

Variable keyboard read, and the array declaration: read, array, declare

  • read

    Example 1: Let a content input by a user of the keyboard, the content into the variable named atest [dmtsai Study @ ~] read atest This is a test <== The cursor will wait for you input!  Please enter the text on the left to see [dmtsai @ study ~]echo $ {} This atest IS A Test <== you just entered data has become a variable content!

  • declare / typeset

Example 1: Let the results be summed variable sum 100 + 300 + 50

declare 或 typeset 是一样的功能,就是在“宣告变量的类型”。如果使用 declare 后面并没有接任何参数,那么 bash 就会主动的将所有的变量名称与内容通通叫出来,就好像使用 set 一样啦!

[dmtsai@study ~]$ sum=100+300+50
[dmtsai@study ~]$ echo ${sum}
100+300+50 &lt;==咦!怎么没有帮我计算加总?因为这是文字体态的变量属性啊!
[dmtsai@study ~]$ declare -i sum=100+300+50
[dmtsai@study ~]$ echo ${sum}
450
复制代码
  • An array (Array) type variable

Example: var set the above-mentioned [1] ~ var [3] variable.

[dmtsai@study ~]$ var[1]="small min"
[dmtsai@study ~]$ var[2]="big min"
[dmtsai@study ~]$ var[3]="nice min"
[dmtsai@study ~]$ echo "${var[1]}, ${var[2]}, ${var[3]}"
small min, big min, nice min
复制代码

Limit relations with file systems and procedures: ulimit

Delete the contents of variables, replace and replace

Example 1: Let lowercase path custom variables set the same as the PATH content

[dmtsai@study ~]$ path=${PATH}
[dmtsai@study ~]$ echo ${path}
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/dmtsai/.local/bin:/home/dmtsai/bin
复制代码

Example 2: Suppose I do not like local / bin, so you want to delete a directory before the fall, how to display?

[dmtsai@study ~]$ echo ${path#/*local/bin:}
/usr/bin:/usr/local/sbin:/usr/sbin:/home/dmtsai/.local/bin:/home/dmtsai/bin
复制代码

Examples VI: The sbin variable content within the path of substitute uppercase SBIN:

[dmtsai@study ~]$ echo ${path/sbin/SBIN}
/usr/local/bin:/usr/bin:/usr/local/SBIN:/usr/sbin:/home/dmtsai/.local/bin:/home/dmtsai/bin
复制代码

This part is easy to understand and more! Keywords that two slash, slash the middle of the two old string

The latter is a new string, so the results will appear as part of the above-mentioned special font Hello!

[dmtsai@study ~]$ echo ${path//sbin/SBIN}
/usr/local/bin:/usr/bin:/usr/local/SBIN:/usr/SBIN:/home/dmtsai/.local/bin:/home/dmtsai/bin
复制代码

If two slashes, then it becomes all the content will be replaced in line with Oh!

We see this as part of a summary to explain:

Variable arrangement Explanation
{Keyword} variable #
{Variable} keyword ## If the data is in line with variable content from scratch, "keyword", the shortest data will be deleted in line with the data if the contents of the variable from the beginning of the line with "keyword", in line with the longest data will be deleted
{Variable} keyword%
{} Keyword variable %% If the variable content matching "keyword" forward data from the tail, the shortest data will be deleted if the variable content in line with the forward end of the line with data from the "keyword", in line with the longest data will be deleted
{Variable / string old / new string}
{String variable // old / new string} If the variable content in line with the "old string" is "the first old string will be replaced by new string" If the variable content in line with the "old string" is "all of the old string will be replaced by new string"

Bash Shell operating environment:

bash environment configuration file

Instruction reads the environment configuration file: source

Universal characters and special symbols

Symbol Meaning

  • 代表“ 0 个到无穷多个”任意字符                       
    复制代码

? Stands for "a must have" any character
[] also represents "a must have in brackets" characters (non-arbitrary character). For example, [ABCD] Representative "must be a character that may be a, b, c, d which any one of four [-] If the minus sign in parentheses stands for" all characters "in the coding sequence for example. [0-9] represents all numbers between 0 and 9, since the encoded digital language is continuous! [^] If the first character in the square brackets for the index symbol (^), it means that "reverse select" , for example, you must have a character representative of abc, as long as non-a, the other characters b, c of the accepted meaning.

In addition to the universal character, bash environment, special symbols, what does? Let us first look at aggregate:

Redirecting the data flow

  1. Standard input (stdin): code 0, using <or <<;

  2. Standard output (stdout): code 1,> or >>;

  3. Standard error (stderr): code 2, 2> or 2 >>; To understand stdout and stderr, we first conduct a paradigm exercise: Example 1: Observe your system root directory (/) directory of each filenames, permissions and attributes, and recorded

    [Study dmtsai @ ~] ll / == The screen will display information file name [dmtsai @ study ~]LL /> ~ / rootfile == screen does not have any information [dmtsai @ study ~] $ ll ~ / rootfile == have a new file is created! -rw-rw-r--. 1 dmtsai dmtsai 1078 Jul 9 18:51 / home / dmtsai / rootfile

1>: The method covers the "correct data" is output to the specified file or device; 1 >>: the cumulative method to "correct data" is output to the specified file or device; 2>: to cover the method of the "data error" is output to the specified file or device; 2 >>: the method of accumulating the output "error data" to the specified file or device;

Examples of five: the instruction of all data written to the file named in the list

[dmtsai@study ~]$ find /home -name .bashrc > list 2> list  ==错误
[dmtsai@study ~]$ find /home -name .bashrc > list 2>&1  ==正确
[dmtsai@study ~]$ find /home -name .bashrc &> list  ==正确
复制代码

standard input: <<< understand and stderr and stdout after, then that <what is it? Ha ha! In its simplest argument is that "originally needed, change the keyboard instead of the data by file content" means. We start with the following operating cat command to look at what is called the "keyboard" it!

Examples of six: a simple process using the cat command to create a file

[dmtsai@study ~]$ cat < catfile
testing
cat file test
< ==这里按下 [ctrl]+d 来离开
[dmtsai@study ~]$ cat catfile
testing
cat file test
复制代码

Judgment pursuant to an order of execution:;, &&, ||

  • cmd; cmd (without considering the correlation instruction command is given continuously)

     在某些时候,我们希望可以一次执行多个指令,例如在关机的时候我希望可以先执行两次sync 同步化写入磁盘后才 shutdown 计算机,那么可以怎么作呢?这样做呀:
    复制代码

[root@study ~]# sync; sync; shutdown -h now

At some point, we hope to execute multiple instructions once, for example, shut down when I hope to be performed twice sync synchronization is written to disk before shutdown computer, how you can make it? Doing it: [root @ study ~] # sync; sync; shutdown -h now

$? (Command return value) and && or ||

cmd1&&cmd2

If cmd1 finished and correct implementation of (? = 0), then started cmd2. 2. If cmd1 finished and is wrong (? ≠ 0), then cmd2 not executed.

cmd1 ||cmd2

If cmd1 finished and correct implementation of (? = 0), the cmd2 not executed. 2. If cmd1 finished and is wrong (? ≠ 0), then started cmd2.

Example 1: Check the directory using ls / tmp / abc exists, if there is a touch create / tmp / abc / hehe

[dmtsai@study ~]$ ls /tmp/abc && touch /tmp/abc/hehe
ls: cannot access /tmp/abc: No such file or directory
复制代码

ls explained very simply can not find the directory, but did not touch error, indicate touch and did not execute

Command line (pipe)

Capture command: cut, grep

Example 1: The PATH variable out, I'll find the fifth path.

[dmtsai@study ~]$ echo ${PATH}
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/dmtsai/.local/bin:/home/dmtsai/bin
[dmtsai@study ~]$ echo ${PATH} | cut -d ':' -f 5
#如同上面的数字显示,我们是以“ : ”作为分隔,因此会出现 /home/dmtsai/.local/bin
#那么如果想要列出第 3 与第 5 呢?,就是这样:
[dmtsai@study ~]$ echo ${PATH} | cut -d ':' -f 3,5

范例一:将 last 当中,有出现 root 的那一行就取出来;
[dmtsai@study ~]$ last|grep 'root'
范例二:与范例一相反,只要没有 root 的就取出!
[dmtsai@study ~]$ last | grep -v 'root'
复制代码

Sort command: sort, wc, uniq

范例一:个人帐号都记录在 /etc/passwd 下,请将帐号进行排序。
[dmtsai@study ~]$ cat /etc/passwd | sort

范例一:使用 last 将帐号列出,仅取出帐号栏,进行排序后仅取出一位;
[dmtsai@study ~]$ last | cut -d ' ' -f1 | sort| uniq

范例一:那个 /etc/man_db.conf 里面到底有多少相关字、行、字符数?
[dmtsai@study ~]$ cat /etc/man_db.conf | wc
131 723 5171

#输出的三个数字中,分别代表: “行、字数、字符数”
复制代码

Two-way redirection: tee

[dmtsai@study ~]$ last | tee last.list | cut -d " " -f1
复制代码

This paradigm allows us to keep a copy of the last output file to last.list;

Character conversion command: tr, col, join, paste, expand

tr 可以用来删除一段讯息当中的文字,或者是进行文字讯息的替换!
复制代码

Example 1: The message last output, all lowercase characters turned into uppercase:

[dmtsai@study ~]$ last | tr '[a-z]' '[A-Z]'
复制代码

In fact, no single quotes can also be performed, such as: "last tr [az] [AZ]"

join 看字面上的意义 (加入/参加) 就可以知道,他是在处理两个文件之间的数据, 而且,
复制代码

Mainly in dealing with "two documents, among them," the same data "of the line, before he put together" means.

这个 paste 就要比 join 简单多了!相对于 join 必须要比对两个文件的数据相关性, paste 就
复制代码

Direct "stick together the two rows, and to intermediate [Tab] button spaced"

expand 这玩意儿就是在将 [tab] 按键转成空白键啦。
复制代码

Zoning command: split

Example 1: My / etc / services there are more than six hundred K, if you want into a 300K file?

[dmtsai@study ~]$ cd / | split -b 300k /etc/services services
[dmtsai@study tmp]$ ll -k services*
-rw-rw-r--. 1 dmtsai dmtsai 307200 Jul 9 22:52 servicesaa
-rw-rw-r--. 1 dmtsai dmtsai 307200 Jul 9 22:52 servicesab
-rw-rw-r--. 1 dmtsai dmtsai 55893 Jul 9 22:52 servicesac
复制代码

Parameter substitution: xargs

On the minus sign - use

Command line is very important in a continuous treatment program in bash! In addition, the analysis of log file which is very important part, so please pay special attention! Further, among the command line, often used to as a preceding instruction stdout stdin this time, when certain instructions require to use the file name (e.g., tar) to be treated, the stdin and stdout can use the minus sign "-" instead, for example:

[root@study ~]# mkdir /tmp/homeback
[root@study ~]# tar -cvf - /home | tar -xvf - -C /tmp/homeback
复制代码

The above example is: "I will / home inside the file to his pack, but the record is not packaged data to a file, but sent to stdout; after line, the tar -cvf - / home sent to the back of the tar - xvf - ". This latter - is access to a command stdout before, so we do not need to use a filename! This is a very common example of Oh! Attention please!

Guess you like

Origin blog.csdn.net/weixin_34396103/article/details/91394408