很高兴和你相遇
这里正在记录我的所思所学
订阅免费邮件通讯接收最新内容
首页 归档 想法 通讯 播客 工具 简历 关于

Linux Command Line 学习笔记 2

说明;写Linux Command Line 学习笔记系列文章本意只是记录自己学习 《Linux Command Line 》 这本书的过程中看到的一些自己没有留意到的地方,因此绝大多数内容只是记录了相关知识点而没有实际扩展内容,纯粹是为了自己后期回顾时有迹可循。另外,因为直接看的是原版书,所以很多地方中英混杂。更详细地学习建议去阅读原书即可。

Working With Commands

  • 查看命令类型 type

    命令有四种可能的形式:

    1. 可执行程序,可以编译成二进制文件,诸如用 C 和 C++ 语言写成的程序,也可以是由脚本语言写成的程序,比如说 shell,perl,python 等等
    2. 内建于 shell 自身的命令,如 ls
    3. shell 函数
    4. 命令别名
  • 查看绝对路径 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 结束输入即可
        
    • 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

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进行理解,依次输入lsecho

  • 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 个参数

      • 特殊字符

  • 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参数

本文作者:思考问题的熊

版权声明:本博客所有文章除特别声明外,均采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 (CC BY-NC-ND 4.0) 进行许可。

如果你对这篇文章感兴趣,欢迎通过邮箱或者微信订阅我的 「熊言熊语」会员通讯,我将第一时间与你分享肿瘤生物医药领域最新行业研究进展和我的所思所学所想点此链接即可进行免费订阅。


· 分享链接 https://kaopubear.top/blog/2017-09-09-LinuxCommandLine2/