Linux Sequential Commands

Para execução sequencial no Linux, basta colocar um “;” como no exemplo abaixo. Importante salientar que deste modo, mesmo que um comando não seja bem sucedido, seguirá sua sequência e executará o próximo comando:

[root@oel7 ~]# date ; ll ; uptime
Wed Jan 27 05:45:57 -03 2021
total 12
-rw-------. 1 root root 1708 Apr 16  2020 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 24  2020 Desktop
drwxr-xr-x. 2 root root    6 Apr 24  2020 Documents
drwxr-xr-x. 2 root root    6 Apr 24  2020 Downloads
-rw-r--r--. 1 root root 1742 Apr 16  2020 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 24  2020 Music
drwxr-xr-x. 2 root root    6 Apr 24  2020 Pictures
drwxr-xr-x. 2 root root    6 Apr 24  2020 Public
drwxr-xr-x. 2 root root    6 Apr 24  2020 Templates
-rwxrwxr-x. 1 root root   41 Jan 26 05:44 variavel.sh
drwxr-xr-x. 2 root root    6 Apr 24  2020 Videos
 05:45:57 up 3 min,  1 user,  load average: 2.04, 1.38, 0.58
[root@oel7 ~]# ll sdfdf.txt ; asfdsf ; date
ls: cannot access sdfdf.txt: No such file or directory
bash: asfdsf: command not found...
Wed Jan 27 05:58:49 -03 2021

Caso usemos o “&&”, aí cria-se a dependência que o comando posterior só executará caso o comando anterior tenha sido bem sucedido:

[root@oel7 ~]# date && ll
Wed Jan 27 05:49:06 -03 2021
total 12
-rw-------. 1 root root 1708 Apr 16  2020 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 24  2020 Desktop
drwxr-xr-x. 2 root root    6 Apr 24  2020 Documents
drwxr-xr-x. 2 root root    6 Apr 24  2020 Downloads
-rw-r--r--. 1 root root 1742 Apr 16  2020 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 24  2020 Music
drwxr-xr-x. 2 root root    6 Apr 24  2020 Pictures
drwxr-xr-x. 2 root root    6 Apr 24  2020 Public
drwxr-xr-x. 2 root root    6 Apr 24  2020 Templates
-rwxrwxr-x. 1 root root   41 Jan 26 05:44 variavel.sh
drwxr-xr-x. 2 root root    6 Apr 24  2020 Videos
[root@oel7 ~]# ll dsfsdfsdf.txt && ll
ls: cannot access dsfsdfsdf.txt: No such file or directory
[root@oel7 ~]#

Já o “||” só executará o comando posterior caso o comando anterior tenha sido mal sucedido:

[root@oel7 ~]# ll sdfsdf.txt || date
ls: cannot access sdfsdf.txt: No such file or directory
Wed Jan 27 05:53:11 -03 2021
[root@oel7 ~]# uptime || date
 05:53:24 up 10 min,  1 user,  load average: 2.08, 1.91, 1.15
[root@oel7 ~]#

Leave a Comment

Your email address will not be published.