Shell中的一些常用指令整理汇总(1)[nohup/&/head/wc/cat]

Shell中的一些常用指令整理汇总(1)

nohup 命令:不中断地运行指令

man nohup查看一下帮助文档:

NOHUP(1)                         User Commands                        NOHUP(1)

NAME
       nohup - run a command immune to hangups, with output to a non-tty

SYNOPSIS
       nohup COMMAND [ARG]...
       nohup OPTION

DESCRIPTION
       Run COMMAND, ignoring hangup signals.

       --help display this help and exit

       --version
              output version information and exit

       If  standard input is a terminal, redirect it from /dev/null.  If stan-
       dard output is a terminal, append output to  ‘nohup.out’  if  possible,
       ‘$HOME/nohup.out’ otherwise.  If standard error is a terminal, redirect
       it to standard output.  To save output to FILE, use  ‘nohup  COMMAND  >
       FILE’.

       NOTE:  your  shell  may  have  its  own version of nohup, which usually
       supersedes the version described here.  Please refer  to  your  shell’s
       documentation for details about the options it supports.

AUTHOR
       Written by Jim Meyering.

可以看出,nohup的作用是,让后面执行的cmd不受终端关闭或者账户退出的影响。如果不指定输出,那么默认会把标准输出为terminal的部分内容重定向到一个nohup.out文件,也可以用>对保存输出文件进行指定。如果时远程执行shell指令,且担心关闭xshell等终端后程序跟着自动停止的,可以实用nohup指令避免这种情况。

& 指令:后台运行程序

对于不需要交互的且时间较长的程序,可以放在后台运行,从而空出终端的交互界面。

如:

$ ls &
[1] 30048
anaconda3  anaconda-ks.cfg  bash  install.log  install.log.syslog  myinit
[1]+  Done                    ls --color=auto

对于ls这个指令,后台运行,给了一个pid号,然后运行结束。

head指令:查看文件前面几行

首先创建一个test.txt文件:

vim test.txt

写入如下内容:

123
124
125

通过head命令查看前面2行

head -2 test.txt
123
124

wc : 统计文档字数/行数/字节数/字符数

仍然是对test.txt,我们利用wc查看文档的字符数统计信息:

WC(1)                            User Commands                           WC(1)

NAME
       wc - print newline, word, and byte counts for each file

SYNOPSIS
       wc [OPTION]... [FILE]...
       wc [OPTION]... --files0-from=F

DESCRIPTION
       Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.  With
       no FILE, or when FILE is -, read standard input.

       -c, --bytes
              print the byte counts

       -m, --chars
              print the character counts

       -l, --lines
              print the newline counts

       --files0-from=F
              read input from the files specified by NUL-terminated names in file F; If F is - then read  names  from
              standard input

       -L, --max-line-length
              print the length of the longest line

       -w, --words
              print the word counts

       --help display this help and exit

       --version
              output version information and exit

实验一下:

$ wc -lmcw test.txt
 3  3 12 12 test.txt

2019-08-01 10:57:20

cat 命令:连接和输入

cat可以用于将两个文件concat起来,也可以用于在键盘创建一个文件

CAT(1)                           User Commands                          CAT(1)

NAME
       cat - concatenate files and print on the standard output

SYNOPSIS
       cat [OPTION]... [FILE]...

DESCRIPTION
       Concatenate FILE(s), or standard input, to standard output.

       -A, --show-all
              equivalent to -vET

       -b, --number-nonblank
              number nonempty output lines

       -e     equivalent to -vE

       -E, --show-ends
              display $ at end of each line

       -n, --number
              number all output lines

       -s, --squeeze-blank
              suppress repeated empty output lines

       -t     equivalent to -vT

       -T, --show-tabs
              display TAB characters as ^I

       -u     (ignored)

       -v, --show-nonprinting
              use ^ and M- notation, except for LFD and TAB

创建方法如下:

cat > filename.txt

然后键盘接入内容,完成后ctrl+d退出。

比如我们创建了test2.txt:

$ head test2.txt
123
124
125
126


127

133
666

-n 标记行号

-s 压缩多行空行为一行

-b 只对non-blank非空行编号

$ cat -n test2.txt
     1	123
     2	124
     3	125
     4	126
     5
     6
     7	127
     8
     9	133
    10	666

$ cat -b test2.txt
     1	123
     2	124
     3	125
     4	126


     5	127

     6	133
     7	666
     
$ cat -s test2.txt
123
124
125
126

127

133
666

2019-08-01 11:15:26

原文地址:https://www.cnblogs.com/morikokyuro/p/13256677.html