命令学习
打开linux的终端,在其中可以输入一些字符串,其中特定的字符串就是命令,命令分为内部命令和外部命令。输入的命令会在PATH中寻找,而不会全盘搜索。如果没有在环境变量PATH中寻找到,会抛出错误 Command Not Found。
[root@node01 ~]# sawadika
bash: sawadika: command not found
[root@node01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
type命令:
type本身是一个内部命令,可以直接通过输入:help type 查看type的使用方法,如下图所示。在type后接空格在加上其他命令就可以了,会直接返回需要查询的命令的类型,或者路径。常用来判断某个命令是否是alias,keyword,function,file或者其他。
Example:type python
可以得到 python命令的路径。
file命令:
file命令是外部命令的一种,用以判别文件的类型。上面我们得到了 python的路径为 /usr/bin/python
Example: file /usr/bin/python
得到结果如下
我们可以看到python是ELF executable类型。因为python是一类解释型语言,本身并不能读到内存中去变成程序。而是先启动一个解释器,。去执行一行行的命令。
另外,我们输入type yum 得到yum的路径,然后file /usr/bin/yum,看一看到yum命令实际上是一个python脚本文件;如下图所示。当我们输入yum命令的时候,实际上是将python解释器启动起来。然后再将yum这个py文件一行行的执行。像这里的file,和yum命令都是外部命令,它们的形式可以是脚本文件,也可以是ELF executable
内部命令:
内部命令可以直接使用help [命令名称] 来查看帮助,内部命令一共用多少个呢,我们直接在shell中输入help可以得到所有内部命令的名称,如下图所示:
我们在shell中输入type echo 可以看到echo是一个shell builtin
[root@node01 ~]# type echo
echo is a shell builtin
[root@node01 ~]#
什么叫shell,shell是一类统称,是一个交互外壳。 shell是用户空间的一个程序,具体的实现方式有bash(Linux),cmd(windows),powershell(Windows10)
我们打开的终端实际上就是一个bash,下面我们看看bash是什么
[root@node01 ~]# type bash
bash is /bin/bash
[root@node01 ~]# file /bin/bash
/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
[root@node01 ~]#
bash是一个ELF 64-bit LSB executable文件。使用echo $$ 输出当前bash的pid(processID 进程号)
[root@node01 ~]# echo $$
3246
可以看到当前bash的pid。另外然后我们也可以使用ps命令查看当前所有进程:
[root@node01 ~]# ps -fe
UID PID PPID C STIME TTY TIME CMD
root 1057 1 0 Oct10 ? 00:00:00 login -- root
root 1059 1 0 Oct10 tty2 00:00:00 /sbin/mingetty /dev/tty2
root 1061 1 0 Oct10 tty3 00:00:00 /sbin/mingetty /dev/tty3
root 1063 349 0 Oct10 ? 00:00:00 /sbin/udevd -d
root 1064 1 0 Oct10 tty4 00:00:00 /sbin/mingetty /dev/tty4
root 1066 1 0 Oct10 tty5 00:00:00 /sbin/mingetty /dev/tty5
root 1068 1 0 Oct10 tty6 00:00:00 /sbin/mingetty /dev/tty6
root 1070 1057 0 Oct10 tty1 00:00:00 -bash
root 1409 1070 0 00:35 tty1 00:00:00 vi profile
root 3244 958 0 04:29 ? 00:00:00 sshd: root@pts/0
root 3246 3244 0 04:29 pts/0 00:00:00 -bash
postfix 3298 1034 0 05:02 ? 00:00:00 pickup -l -t fifo -u
root 3306 3246 0 05:44 pts/0 00:00:00 ps -fe
pstree命令可以查看进程树。我们在shell中输入bash,启动了一个bash,输入ps -fe 可以发现进程中多了一个pid为3308的进程,其父进程号为3246,正是原来的bash。
[root@node01 ~]# pstree
init─auditd───{auditd}
├─crond
├─login───bash───vi
├─master─┬─pickup
│ └─qmgr
├─5*[mingetty]
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash───pstree
└─udevd───2*[udevd]
[root@node01 ~]# bash
[root@node01 ~]# pstree
init─┬─auditd───{auditd}
├─crond
├─login───bash───vi
├─master─┬─pickup
│ └─qmgr
├─5*[mingetty]
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash───bash───pstree
└─udevd───2*[udevd]
[root@node01 ~]# ps -fe
UID PID PPID C STIME TTY TIME CMD
root 1057 1 0 Oct10 ? 00:00:00 login -- root
root 1059 1 0 Oct10 tty2 00:00:00 /sbin/mingetty /dev/tty2
root 1061 1 0 Oct10 tty3 00:00:00 /sbin/mingetty /dev/tty3
root 1063 349 0 Oct10 ? 00:00:00 /sbin/udevd -d
root 1064 1 0 Oct10 tty4 00:00:00 /sbin/mingetty /dev/tty4
root 1066 1 0 Oct10 tty5 00:00:00 /sbin/mingetty /dev/tty5
root 1068 1 0 Oct10 tty6 00:00:00 /sbin/mingetty /dev/tty6
root 1070 1057 0 Oct10 tty1 00:00:00 -bash
root 1409 1070 0 00:35 tty1 00:00:00 vi profile
root 3244 958 0 04:29 ? 00:00:00 sshd: root@pts/0
root 3246 3244 0 04:29 pts/0 00:00:00 -bash
postfix 3298 1034 0 05:02 ? 00:00:00 pickup -l -t fifo -u
root 3308 3246 0 05:47 pts/0 00:00:00 bash
root 3318 3308 0 05:48 pts/0 00:00:00 ps -fe
linux多用户多任务。 接受一个用户输入一个字符串,使用空格来切割字符串。(bash对于空格非常敏感)bash在解释字符串的时候经过七步扩展。
内部命令一共有xx个。
man命令
可以使用man命令来学习外部命令。
man是全屏交互,输入/需要查找的字符 查找list,n往下跳,N往回跳,q退出。
example: man ascii
ASCII(7) Linux Programmer’s Manual ASCII(7)
NAME
ascii - the ASCII character set encoded in octal, decimal, and hexadecimal
DESCRIPTION
ASCII is the American Standard Code for Information Interchange. It is a 7-bit code. Many 8-bit codes (such as ISO 8859-1, the Linux default character set)
contain ASCII as their lower half. The international counterpart of ASCII is known as ISO 646.
The following table contains the 128 ASCII characters.
C program '\X' escapes are noted.
Oct Dec Hex Char Oct Dec Hex Char
------------------------------------------------------------------------
000 0 00 NUL '\0' 100 64 40 @
001 1 01 SOH (start of heading) 101 65 41 A
002 2 02 STX (start of text) 102 66 42 B
003 3 03 ETX (end of text) 103 67 43 C
004 4 04 EOT (end of transmission) 104 68 44 D
005 5 05 ENQ (enquiry) 105 69 45 E
006 6 06 ACK (acknowledge) 106 70 46 F
007 7 07 BEL '\a' (bell) 107 71 47 G
010 8 08 BS '\b' (backspace) 110 72 48 H
011 9 09 HT '\t' (horizontal tab) 111 73 49 I
012 10 0A LF '\n' (new line) 112 74 4A J
013 11 0B VT '\v' (vertical tab) 113 75 4B K
014 12 0C FF '\f' (form feed) 114 76 4C L
015 13 0D CR '\r' (carriage ret) 115 77 4D M
016 14 0E SO (shift out) 116 78 4E N
017 15 0F SI (shift in) 117 79 4F O
020 16 10 DLE (data link escape) 120 80 50 P
021 17 11 DC1 (device control 1) 121 81 51 Q
022 18 12 DC2 (device control 2) 122 82 52 R
023 19 13 DC3 (device control 3) 123 83 53 S
024 20 14 DC4 (device control 4) 124 84 54 T
025 21 15 NAK (negative ack.) 125 85 55 U
026 22 16 SYN (synchronous idle) 126 86 56 V
027 23 17 ETB (end of trans. blk) 127 87 57 W
030 24 18 CAN (cancel) 130 88 58 X
031 25 19 EM (end of medium) 131 89 59 Y
032 26 1A SUB (substitute) 132 90 5A Z
033 27 1B ESC (escape) 133 91 5B [
034 28 1C FS (file separator) 134 92 5C \ '\\'
035 29 1D GS (group separator) 135 93 5D ]
036 30 1E RS (record separator) 136 94 5E ^
037 31 1F US (unit separator) 137 95 5F _
040 32 20 SPACE 140 96 60 `
041 33 21 ! 141 97 61 a
042 34 22 " 142 98 62 b
043 35 23 # 143 99 63 c
044 36 24 $ 144 100 64 d
045 37 25 % 145 101 65 e
046 38 26 & 146 102 66 f
047 39 27 ´ 147 103 67 g
050 40 28 ( 150 104 68 h
051 41 29 ) 151 105 69 i
052 42 2A * 152 106 6A j
053 43 2B + 153 107 6B k
054 44 2C , 154 108 6C l
055 45 2D - 155 109 6D m
056 46 2E . 156 110 6E n
057 47 2F / 157 111 6F o
060 48 30 0 160 112 70 p
061 49 31 1 161 113 71 q
062 50 32 2 162 114 72 r
063 51 33 3 163 115 73 s
064 52 34 4 164 116 74 t
065 53 35 5 165 117 75 u
066 54 36 6 166 118 76 v
067 55 37 7 167 119 77 w
070 56 38 8 170 120 78 x
071 57 39 9 171 121 79 y
072 58 3A : 172 122 7A z
073 59 3B ; 173 123 7B {
074 60 3C < 174 124 7C |
075 61 3D = 175 125 7D }
076 62 3E > 176 126 7E ~
077 63 3F ? 177 127 7F DEL
我们可以看到ascii码一个有128个字符的表示方法。binary 二进制的,octal 八进制的,hexadecimal 十六进制的,decimal 十进制的。
输入 man utf-8
Encoding
The following byte sequences are used to represent a character. The sequence to be used depends on the UCS code number of the character:
0x00000000 - 0x0000007F:
0xxxxxxx
0x00000080 - 0x000007FF:
110xxxxx 10xxxxxx
0x00000800 - 0x0000FFFF:
1110xxxx 10xxxxxx 10xxxxxx
0x00010000 - 0x001FFFFF:
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
0x00200000 - 0x03FFFFFF:
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
0x04000000 - 0x7FFFFFFF:
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
The xxx bit positions are filled with the bits of the character code number in binary representation. Only the shortest possible multi-byte sequence which can
represent the code number of the character can be used.
The UCS code values 0xd800–0xdfff (UTF-16 surrogates) as well as 0xfffe and 0xffff (UCS non-characters) should not appear in conforming UTF-8 streams.
Example
The Unicode character 0xa9 = 1010 1001 (the copyright sign) is encoded in UTF-8 as
11000010 10101001 = 0xc2 0xa9
and character 0x2260 = 0010 0010 0110 0000 (the "not equal" symbol) is encoded as:
11100010 10001001 10100000 = 0xe2 0x89 0xa0
UTF-8拿多个个字节表示一个字符(称得上字符集的是ascii码,是使用1个字节代表一个字符,一个字节8位,第一位永远为0。utf-8叫扩展字符集属于变长字符集。)
例如:
110xxxxx 10xxxxxx 表示该由110表示由2个字节组成,每个字节前2位都是10。第一个字节位的表示需要几个字节来表示该字符。
1110xxxx 10xxxxxx 10xxxxxx 表示由该字符由三个字节组成,把所有的xxxx拼接在一起就是代表了编码。
kernel内核。完成底层的一些操作,比如读文件。用户态到内核态的切换。应用程序执行到要读文件的时候,会调用kernel。用户空间->内核空间。