Linux learning (7) - Shell base

table of Contents

Shell Overview

echo command

The first Shell Script

Aliases and shortcuts

Command history

Output redirection

Input redirection

Pipe character

Wildcards


 

Video tutorial lesson but learning Mu: https://www.imooc.com/video/4524

This section summarizes the study and made notes.

 

Shell Overview

1. What is Shell

Command received input commands translated into binary, invoke the operation.

  • Shell is a command-line interpreter that provides users send a request to run a program interface system-level program, the user can use the Shell to start the Linux kernel, suspend, stop or even write some programs.

  • Shell is still a quite powerful programming language, easy to write, easy to debug, flexibility is strong. Shell is an interpreted scripting language, you can call on Shell Linux system commands directly.

2, Shell classification

  • Bourne Shell: Since 1979 began using Unix Bourne Shell, Bourne Shell master file called sh. Basically eliminated.

  • C Shell: C Shell mainly in the BSD version of Unix systems using C language and its syntax is similar to the name.

  • The two main types of grammar are Bourne Shell and C, both of grammar is not compatible with each other. Bourne family including sh, ksh, Bash, psh, zsh; C family including: csh, tcsh.

3, using the echo $ SHELL view with what Shell.

Example 1: echo $ SHELL command to see what is the use Shell.

Official bash the machine used.

4. Use vi / etc / shells command to see which Shell supports.

Example 1: vi / etc / shells command , see the support of Shell.

 

echo command

grammar:

echo [选项] [输出内容]
	选项:
		-e 支持反斜线控制的字符转换
Control characters effect
\a Output warning sound
\b Backspace key, the delete key is left
\n Newline
\r enter
\t Tabs, namely the Tab key
\ v Vertical tab
\ Onnn According to the octal ASCII character code table output. Where 0 is the number zero, nnn is three octal
\ socialization According to a hexadecimal character ASCII code table output. Where hh is the two-digit hexadecimal number.

Example 1: echo "Hello Linux" command output "Hello Linux".

Example 2: echo -e "a \ nb \ nc" command , line feed output.

Example 3: echo -e "\ e [ 1; 31m world is so big, I want to see \ e [0m." Command to display color.

 

The first Shell Script

Press Ctrl + X to leave.

All commands are as follows:

With # is a comment, "#! / Bin / bash" must be written on. "Nano hello.sh" command creates hello.sh file and edit content.

Script execution:

  • Given execute permission, run directly

    • chmod 755 hello.sh

    • ./hello.sh

  • Bash script execution by calling

    • bash hello.sh

 

Aliases and shortcuts

You can set an alias to call long command, and thus will not have to keep entering long command.

Aliases can be set by the alias command.

grammar:

alias
	查看系统中所有的命令别名
alias 别名='原命令'
	设定命令别名
vi ~/.bashrc
	写入环境变量配置文件,设置别名永久生效。让配置别名不用重启就直接生效,使用[source .bashrc]命令
unalias 别名
	删除别名

Since the implementation of aliases and may have the same system commands, such as rm, so their execution is a sequential.

Command takes effect sequence:

  • First priority execution command executed by an absolute or relative path.

  • Second priority execution alias.

  • Third overall execution of internal commands Bash

  • Find fourth overall implementation of the first command sequence found in accordance with the definition of $ PATH environment variable directory

Example 1: alias command , view the system in all existing alias.

Example 2: alias lh = 'ls -lh ' command , the "ls -lh" command creates an alias named "lh" is used to display details humanized file.

But this set of alias is temporary, ie after the restart alias will disappear.

Example 3: vi ~ / .bashrc source /root/.bashrc command and the command , set up a permanent alias.

"Source /root/.bashrc" command can make the above settings to take effect immediately, without restarting permanent alias.

La test command

Example 4: unalias lh command , delete the alias named lh.

Common shortcuts:

  • Ctrl + C forcibly terminate the current command
  • Ctrl + L to clear the screen
  • Ctrl + A command line cursor to the first
  • Ctrl + E cursor to the end of the command line
  • Ctrl + U delete from the cursor position to the beginning of the line
  • Ctrl + X to command into the background
  • Ctrl + R Search command history

 

Command history

grammar:

history [选项] [历史命令保存文件]
	选项:
		-c 清空历史命令
		-w 把缓存中的历史命令写入历史命令保存文件~/.bash_history

Example 1: history command to view the current commands.

Example 2: history -c command , empty the history of command.

Example 3: cat ~ / .bash_history command , see the history command to save the file.

Call history command:

  • Use the down arrow to call the previous history command

  • Use "! N" repeat the n th command

  • Use "!!" repeats a command execution

  • Use "! String" repeat the last command beginning with that string

History command completion:

  • Use Tab to speed up the input, a command to facilitate troubleshooting.
  • In Bash, the command and file completion is very convenient and commonly used functions, as long as when we enter a command or file, press the "Tab" key will automatically completion.

 

Output redirection

Example 1: ls> test.log command to save the contents of the results of the ls command to test.log file, but is covered by the original content.

Example 2: ls >> test.log command , the document added test.log result of the content of the ls command.

Example 3: lss 2> test.log command , LSS is a wrong command, the contents of the result generated test.log saved to a file cover.

Example 4: abc 2 >> test.log command , abc is a bad command, but the error is appended to test.log file instead of overwriting.

If it is correct command, ">> 2" will not save the output to a file.

Example 5: ls> test.log 2> & 1 command and lss> test.log 2> & 1 command , wherein the former is correct command, which is a wrong command, the contents will result in a covered manner saved to a file test.log

Example 6: ls >> test.log 2> & 1 and lss >> test.log 2 & 1 command , wherein the former is correct command, a command which is wrong, will result in the contents of additional ways to save a file test.log

Example 7: ls &> test.log command and lss &> test.log order to cover the way of the correct results and incorrect results are saved to a file test.log.

Example 8: ls & >> test.log command and lss & >> test.log command to append a way to correct erroneous results and the results are saved to a file test.log.

Example 9: ls >> success.log 2 >> error.log command and lss >> success.log 2 >> error.og command , the former is the correct command, which is wrong command, save the right to produce the results of the upcoming success .log file, save the result to the error.log file error.

Example 10: ls &> / dev / null command , the result will be generated regardless of right or wrong are thrown away.

 

Input redirection

Statistics syntax:

wc [选项][文件名]
        通过Ctrl+D显示统计结果
	选项:
		-c 统计字节数
		-w 统计单词数
		-l 统计行数

1 case: wc command , byte counts typing, words, lines.

Two cases: wc -l command , statistics line.

Example 3: wc error.log command , and other characters in the line count statistics file.

 

Pipe character

You can use the pipe multi command sequentially.

Multi-order command execution:

Multi-order command execution syntax:

命令1 ; 命令2 ; 命令3
	多个命令顺序执行,命令之间没有任何逻辑联系。
命令1 && 命令2
	逻辑与。
	当命令1正确执行,则命令2才会执行。
	当命令1执行不正确,则命令2不会执行。
命令1 || 命令2
	逻辑或。
	当命令1执行不正确,则命令2才会执行。
	当命令1正确执行,则命令2不会执行。

Pipe character syntax:

命令1 | 命令2
	命令1的正确输出作为命令2的操作对象,命令2必须能够操作命令1的输出

Example 1: ls; lss; cd / command , performs several sequential order, regardless of whether the correct command are executed.

Example 2: ls && cd / command and lss && ls command , only 1 correct execution of the command, the command will execute correctly 2, 1 error if the command, the command will not be executed 2.

Example 3: ls || cd / lss || ls command and the command , if a command to execute correctly, the command will not be executed 2; 1 Error if the command, the command 2 is executed.

Example 4: ls && echo yes || echo no command , if the output of the ls command Yes correct, incorrect output no.

Example 5: netstat -an | grep ESTABLISHED command , view the user is connected.

 

Wildcards

Example 1: find install.log * command , query matching install.log file.

Example 2: echo `date` command and echo $ (date) commands , system commands can be executed, it is recommended that the latter $ (), as is easily seen Backticks single quotes.

Example 3: d = date $ d command and command , i.e., the output variable.

Published 500 original articles · won praise 77 · views 160 000 +

Guess you like

Origin blog.csdn.net/cnds123321/article/details/104982678