Linux review notes of bash shell (5) on the role of minus sign -

    Please reprint from the source: http://eksliang.iteye.com/blog/2105677   

    Pipeline commands are very important in bash's continuous processing program, especially when the studout (standard output) of the previous command is used as the stdin (standard input) this time, it is too important, some commands need to use File names , such as the split command (split) of the previous document, and the tar (packaging) command, etc.! At this time, this file assumes studout or stdin. At this time, this studout or stdin can be replaced by a minus sign (-).

 Example 1: Use ls -al / to record all 3 lines of the output information into a file

[root@bogon bash]# ls -al / | split -l 3 - s
[root@bogon bash]# wc -l s*
   3 saa
   3 sab
   3 sac
   3 sad
   3 leaves
   3 pure
   3 sag
   2 saw
  23 total
#Generally speaking, if stdout (standard output)/stdin (standard input) is required, but there is no file, and some are just "-", then that "-" will be regarded as stdout or stout

 

Example 2: tar -cvf tarName.tar ./bash is like this, see how I use "-" to replace the standard output of other files

[root@bogon ~]# tar -cvf   ./bash
tar: Cowardly refusing to create an empty archive
Try `tar --help' or `tar --usage' for more information.
#There is an error here, there is no way, because the syntax is wrong
[root@bogon ~]# tar -cvf  - ./bash
./bash/
./bash/sag
./bash/saf
./bash/sae
./bash/aa.txt
./bash/sab
./bash/saa
./bash/sad
./bash/cc.txt
./bash/bb.txt
./bash/test.txt
.....
#I saw that I didn't use "-" at this time to replace the original output to the file, and the standard output to the screen

 

    Example 3: Comprehensive Example

[root@bogon ~]# tar -cvf - ./bash | tar -xvf -
./bash/
./bash/sag
./bash/saf
...! (omit)
./bash/saf
./bash/sah
./bash/sac
tar: ./bash: file changed as we read it
./bash/sae
.......!omit)
./bash/sah
./bash/sac

  The above example means that I package the file ./bash, but the packaged file is not recorded to the file, but sent to the standard output (stdout); after the pipeline, tar -cvf - ./bash is passed to the following tar -xvf-. The latter "-" uses the stdout of the previous command as stdin, so there is no need to use a file here. This is a very common example, because when we write a script, we don't need to write a temporary file.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326502953&siteId=291194637
Recommended