Vertical'|', double vertical'|', & and && in linux

Keywords: ftp Linux less

For beginners, these meanings may only know a few of them. Let's take a look at them.

1. The vertical line'|'is used as a pipe character in linux, and the output of the command before'|' is used as the input after'|'. For instance

[18066609@root~]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

After adding | head -n1

[18066609@root~]$ cat /etc/passwd|head -n1
root:x:0:0:root:/root:/bin/bash

2. Multiple commands with double vertical lines'|', divided by double vertical lines'|', are executed according to the following rules. If the former command is true, the latter command will not be executed. If the former command is false, the latter command will continue to be executed.

For example, input 1 is less than 2 double vertical output b, the front is true, the back is not executed without output b, input 1 is greater than 2 double vertical echo b, the front is false output B.

[18066609@root~]$ [[ 1 -lt 2 ]]||echo b
[18066609@root~]$ [[ 1 -gt 2 ]]||echo b
b

Another practical example is to determine whether a file exists, create it if it does not exist, and execute nothing if it exists.

[18066609@root~]$ ls
[18066609@root~]$ [[ -f 1.txt ]] || touch 1.txt
[18066609@root~]$ ls
1.txt
[18066609@root~]$ [[ -f 1.txt ]] || touch 2.txt
[18066609@root~]$ ls
1.txt

3. & Execute multiple commands at the same time, regardless of whether the command is executed successfully or not.

[18066609@root~]$ [[ 1 -gt 2 ]] & echo b
[1] 32303
b
[18066609@root~]$ 
[1]+  Exit 1                  [[ 1 -gt 2 ]]

4. & & can execute multiple commands at the same time, and will not execute the following commands when encountering the wrong commands. If there has been no error, then the execution is completed.

[18066609@root~]$ [[ 1 -gt 2 ]] && echo b
[18066609@root~]$ 
[18066609@root~]$ [[ 1 -lt 2 ]] && echo b
b

 

Posted by mastercool on Mon, 04 Feb 2019 02:57:16 -0800