Skip to content

Instantly share code, notes, and snippets.

@ibreathebsb
Created September 4, 2018 01:40
Show Gist options
  • Save ibreathebsb/65b7f58c2b71491cf479b8240a11a367 to your computer and use it in GitHub Desktop.
Save ibreathebsb/65b7f58c2b71491cf479b8240a11a367 to your computer and use it in GitHub Desktop.

输入输出重定向

  1. 0 1 2 分别表示标准输入(键盘) 标准输出(屏幕) 标准错误输出
  2. > file 将原本输出到标准输出的内容以覆盖的方式写入到file中,>> file追加的方式写入到file中
  3. 2> file 与上述类似,不过只会处理错误信息
  4. 将正常信息和错误信息分开输出ls -al > ok 2> bad
  5. 将正常信息和错误信息合并到一个文件输出ls -al > ok 2>&1
  6. 忽略所有信息,黑洞/dev/null, ls -al > /dev/null >2&1
  7. 标准输入 用文件内容来代替键盘输入 cat > test < ./file
  8. 自定义输入结束 cat > test << 'STOP', 当输入STOP时会结束输入

多个命令的执行

  1. ;: 不关心上一个命令是否执行成功,直接执行下一个 command1; command2; command3
  2. &&: 只有上个命令返回0才会执行下个命令
  3. ||: 只有上个命令返回不是0才会执行下个命令
  4. 常用模式command1 && command2 || command3

eg:创建文件

ls /tmp/a || mkdir /tmp/a && touch /tmp/a/b
# or a better one 
ls /tmp/a > /dev/null 2>&1 || mkdir /tmp/a && touch /tmp/a/b

``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment