说明;写Linux Command Line 学习笔记系列文章本意只是记录自己学习 《Linux Command Line 》 这本书的过程中看到的一些自己没有留意到的地方,因此绝大多数内容只是记录了相关知识点而没有实际扩展内容,纯粹是为了自己后期回顾时有迹可循。另外,因为直接看的是原版书,所以很多地方中英混杂。更详细地学习建议去阅读原书即可。
Working With Commands
-
查看命令类型
type
命令有四种可能的形式:
- 可执行程序,可以编译成二进制文件,诸如用C 和C++ 语言写成的程序, 也可以是由脚本语言写成的程序,比如说shell,perl,python等等
- 内建于shell 自身的命令,如ls
- shell 函数
- 命令别名
-
查看绝对路径
which
只对可执行程序有效果,这里不包括内部的命令(cd)和别名。
-
帮助
man help
-
显示相似命令
apropos
-
简要介绍
whatis
-
创造命令
alias
Redirection
-
I/O redirection allows us to change where output goes and where input comes from. Normally, output goes to the screen and input comes from the keyboard, but with I/O redirection, we can change that.
-
Redirecting Standard Output
ls > tmp.txt
(只会对输出重定向而不会对错误信息重定向),每写一次都会覆盖。
追加使用ls >> tmp.txt
-
Redirecting Standard Error
文件描述符
- 文件描述符在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。
- While we have referred to the first three of these file streams as standard input, output and error, the shell references them internally as file descriptors 0, 1 and 2, respectively.
- The file descriptor “2” is placed immediately before the redirection operator to perform the redirection of standard error to the file.
commond 2> tmp.txt
-
Redirecting Standard Output And Standard Error
commond > tmp.txt 2>&1
新版bash
commode &> tmp.txt
;&>>tmp.txt
-
Disposing Of Unwanted Output
扔掉错误信息和状态信息
redirecting output to a special file called “/dev/null” 垃圾桶
commond 2> /dev/null
-
Redirecting Standard Input
-
cat
- Concatenate files- 使用cat 来写一小段话并存到一个文件中
cat > lazy_dog.txt(此时shell进入输入状态) 输入内容会存到文件中,使用ctrl-d结束输入即可
- 使用cat 来写一小段话并存到一个文件中
-
Pipelines
- The ability of commands to read data from standard input and send to standard output is utilized by a shell feature called pipelines
- Using the pipe operator “|” (vertical bar), the standard output of one command can be piped into the standard input of another
-
Filters
sort
- Sort lines of text
uniq
- Report or omit repeated lines
grep
- Print lines matching a pattern
wc
- Print newline, word, and byte counts for each file
head
- Output the first part of a file
tail
- Output the last part of a file- tail has an option which allows you to view files in real-time.使用
-f
选项 - Using the “-f” option, tail continues to monitor the file and when new lines are appended, they immediately appear on the display. This continues until you type Ctrl-c.
tee
- Read from standard input and write to standard output and files
- tail has an option which allows you to view files in real-time.使用
-
Seeing The World As The Shell Sees It
-
expansions 扩展
-
Pathname Expansion路径名扩展
-
每当我们在shell下键入一条命令,并按下Enter,shell扩展(Expansions)会对我们键入的命令字符串进行一系列处理,然后才执行此命令。shell扩展是shell内建的处理程序,它发生在命令执行之前,因此与我们键入的命令无关
-
shell把整条命令按功能分割为多个独立单元,每个独立单元作为整体对待,叫做一个word,也称为token
-
With expansion, you enter something and it is expanded into something else before the shell acts upon it
-
使用
echo
进行理解,依次输入ls
和echo
-
-
Tilde Expansion "~"
- 如
cd ~foo
- 如
-
Arithmetic Expansion 算术扩展
使用格式
$((expression))
echo $((2 + 2))
结果是4
,但是无法计算小数支持的运算方法
+ Addition - Subtraction * Multiplication / Division (but remember, since expansion only supports integer arithmetic, results are integers.) % Modulo, which simply means, “ remainder.” ** Exponentiation
注意
$((5/2))
会报错,$((5%2))
返回1
-
brace expansion 花括号({})扩展
can create multiple text strings from a pattern containing braces 最强大的扩展
花括号扩展是首先被执行的扩展,格式有两种,字符串输出,或序列输出
- 举例1
echo Front-{A,B,C}-Back Front-A-Back Front-B-Back Front-C-Back
contain a leading portion called a preamble and atrailing portion called a postscript.
The brace expression itself may contain either acomma-separated list of strings, or a range of integers or single characters.
- 举例2
echo Number_{1..5} Number_1 Number_2 Number_3 Number_4 Number_5
- 举例3
echo {001..15} 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015
- 举例4
echo a{A{1,2},B{3,4}} baA1b aA2b aB3b aB4b
-
shell仅仅是把花括号中的字符串以逗号,分隔,然后从左至右输出各个字符串,并不会对字符串做任何语法解释处理
-
花括号扩展中的一对花括号不能被引号引住,否则字符串会被原样输出,并且花括号中需要至少有一个不带引号的逗号或者一个正确的序列表达式
-
在花括号扩展中使用反斜杠
\
转义特殊字符
-
parameter and variable expansion 参数和变量扩展
-
It's a feature that is more useful in shell scripts than directlyon the command line.
-
Many of its capabilities have to do with the system's ability to storesmall chunks of data and to give each chunk a name.
-
Many such chunks, more properlycalled variables, are available for your examination.
-
参数扩展的基本格式是
${parameter}
,扩展的结果是${parameter}
被替换为相应的值。$是前导符,parameter是一个可以存储值的参数 -
参数扩展
-
在shell中,符号
$
用作参数扩展、命令替换、算数扩展的前导符,就是说$
符号告诉shell接下来要匹配的可能是这三种扩展中的一种。当然前提是这里的符号$
没被转义,也没被引号引用。 -
字符对{、}定义了扩展范围,虽然它不是必须的,但坚持一直使用是个好习惯,它可以保护变量名
parameter为值大于9的数字,比如
${10}
表示命令行传入的第10个参数parameter是一个变量名,其后面紧跟一个可能会被解释成变量名一部分的字符
-
-
参数的形式
-
变量名
变量我们很熟悉,此时parameter是一个变量,扩展的结果是
${parameter}
被替换为parameter的值。可以对parameter进行赋值,或修改其值,parameter根据shell中变量名命名规则命名,可能需要使用花括号{}明确变量名范围。同时,如果你真的是想在屏幕输出
${LANG}
这几个字符,可以使用转义符,像这样echo \${LANG}
可能你还见过
${LANG:-strings}
或${parameter/pattern/string}
这种用法,它可以让你在显示变量值之前对变量进行替换或修改。 -
数字
此时parameter为数字,称作位置参数,扩展的结果是
$n
被替换为命令行传入的第n个参数在脚本中,可以使用
$n
引用从命令行传入的第n个参数,不能使用赋值语句对其赋值,内建命令set、shift用于对位置参数进行控制。当脚本中有函数(functions)时,在函数内部,$n
表示传递给此函数的第n个参数 -
特殊字符
-
-
![](https://img.haomeiwen.com/i177622/9ea9bc2e2c48863e.png)
-
command substitution 命令替换
use the output of a command as an expansion
ls -l `which cp` 等价于 ls -l ${which cp}
-
Quoting 引号
引号可以控制不想要的扩展
- 双引号
- If you place text inside double quotes, all the special characters used by the shell lose their special meaning and are treated as ordinary characters.
- 无效扩展 word-splitting, pathname expansion, tilde expansion, and brace expansion
- 依旧有效的扩展 $; \ ;``. parameter expansion, arithmetic expansion, and command substitution
- 为了体会双引号的功能,可以尝试命令
echo $(cal)
和echo "$(cal)"
- The fact that newlines are considered delimiters by the word-splitting mechanism causes an interesting, albeit subtle, effect on command substitution
- 单引号
- If we need to suppress all expansions, we use single quotes 使所有扩展都无效
- 体会命令
echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"
echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
- 双引号
-
Escaping Characters 转义字符
- 想要正常使用“$”, “!”, “&”, “ “, 可以使用
\
- 除了转义字符外,
\
可以有如下功能( backslash escape sequences)\a 蜂鸣 \b Backspace \n Newline. On Unix-like systems, this produces a linefeed. \r Carriage return 回车 \t Tab
- 在shell 如果需要
echo
识别\t
,需要使用-e
参数
- 想要正常使用“$”, “!”, “&”, “ “, 可以使用
![](https://img.haomeiwen.com/i177622/ef1e74963ce110a5.jpg)
网友评论