Shell 基础

行内操作

  • ^a: jump to BOL

  • ^e: jump to EOL

  • ^u: delete the line

  • ^k: delete to EOL

  • ^w: delete a word forward

  • alt+f: jump a word forward

  • alt+b: jump a word backward

  • ^r: search history

  • alt+.: complete second parameter

任务控制

  1. 执行command

  2. ^z挂起当前 job

  3. bg后台继续该 job

  4. fg召回前台

后台运行命令

command &

或者如果你不想看到任何输出,使用

command &> /dev/null &
  • 如此你可以继续使用当前 shell

  • 使用bg查看是否有任务在后台运行

  • 使用jobs查看后台任务

  • 使用fg将任务召回前台

  • 不能退出 shell,否则进程会被杀掉

  • 使用disown丢掉进程,可以退出 shell

又:

nohup command &> /dev/null &

等价于以上的操作。单纯的nohup command会在当前目录创建一个隐藏文件以写入命令的输出。以上命令将程序的输出重定向至比特桶丢弃。

同时输出到 console 和文件

将命令输出重定向到文件:

SomeCommand > SomeFile.txt  # overwrite
SomeCommand >> SomeFile.txt # append

将命令输出 (stdout) 及报错 (stderr) 重定向到文件:

SomeCommand &> SomeFile.txt
SomeCommand &>> SomeFile.txt

同时输出到 console 和文件:

SomeCommand 2>&1 | tee SomeFile.txt     # overwrite
SomeCommand 2>&1 | tee -a SomeFile.txt  # append

如何正确重定向:https://segmentfault.com/q/1010000002454596 这里的 1,2 其实是文件描述符,在 linux 中,这几个文件描述符有特殊含义:

  • 0 -> stdin

  • 1 -> stdout

  • 2 -> stderr

执行

<cmd> 2>&1 > log.txt #1
<cmd> > log.txt 2>&1 #2

放在>后面的&表示重定向的目标不是一个文件,而是一个文件描述符。也就是说,将 stderr 重定向到文件描述符为 1 的那个文件(也就是 stdout)。

备注

引自 http://www.gnu.org/software/bash/manual/bashref.html#Redirections

Note that the order of redirections is significant. For example, the command

ls > dirlist 2>&1 directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command

ls 2>&1 > dirlist directs only the standard output to file dirlist, because the standard error was made a copy of the standard output before the standard output was redirected to dirlist.

          || visible in terminal ||   visible in file   || existing
  Syntax  ||  StdOut  |  StdErr  ||  StdOut  |  StdErr  ||   file
==========++==========+==========++==========+==========++===========
    >     ||    no    |   yes    ||   yes    |    no    || overwrite
    >>    ||    no    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
   2>     ||   yes    |    no    ||    no    |   yes    || overwrite
   2>>    ||   yes    |    no    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
   &>     ||    no    |    no    ||   yes    |   yes    || overwrite
   &>>    ||    no    |    no    ||   yes    |   yes    ||  append
          ||          |          ||          |          ||
 | tee    ||   yes    |   yes    ||   yes    |    no    || overwrite
 | tee -a ||   yes    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    || overwrite
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
|& tee    ||   yes    |   yes    ||   yes    |   yes    || overwrite
|& tee -a ||   yes    |   yes    ||   yes    |   yes    ||  append

Ref:

  1. https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file

  2. https://www.gnu.org/software/bash/manual/bash.html#Redirections

从系统中踢出某个用户

# See the pid of the user's login process.
$ who -u
yychi    tty1         2020-02-19 21:06          460

# Let him know he will be kick off.
$ echo "You'll be kick off by system administrator." | write yychi

# Kick off.
$ kill -9 460

# Done.

Ref: https://www.putorius.net/kick-user-off-linux-system.html

Reference