站点图标 AI技术聚合

Linux Shell脚本多命令执行逻辑的示例详解

简介

Linux 中可以使用分号”;“、双and号”&&“和双竖线”||”来连接多个命令。根据场景的不同适当的使用合适的符号。

历史攻略

python:执行dos命令、Linux命令

命令连接符解析:

仅连接,表示运行先后,无逻辑关系:分号”;”,如command1 ; command2

逻辑与关系:&&,如command1 && command2

逻辑或关系:&&,如command1 && command2

一、分号

命令之间没有逻辑关系。分号连接的命令会按照顺序从前向后依次执行,但分号两端的命令之间没有任何逻辑关系,所有写出来的命令最终都会被执行,即使分号前面的命令出错也不影响后面的命令。

二、&&

逻辑与。&&连接的命令会按照顺序从前向后执行,但只有当command1正确执行才执行command2,如果command1不正确执行,则不执行command2。在bash中,通过预定义变量“? ”来判断命令是否正确执行,如果 ” ?”来判断命令是否正确执行,如果”?”来判断命令是否正确执行,如果”?”的值为0则表示前一条命令正确执行,其他任意值都表示不正确执行。

三、||

逻辑或。||连接的命令会按照顺序从前向后执行,但只有当command1不正确执行才执行command2,command1正确执行则不会执行command2。||和&&都是短路符号,符号左右的命令之间具有逻辑关系。

案例剖析

# -*- coding: utf-8 -*-
# time: 2022/10/4 21:47
# file: cmd_1_success.py
# 公众号: 玩转测试开发


def run_success_1():
    print("cmd_1 start run")
    print("cmd_1 do something")
    print("cmd_1 end")


if __name__ == '__main__':
    run_success_1()

cmd1_fail = python cmd_1_fail.py

# -*- coding: utf-8 -*-
# time: 2022/10/4 21:47
# file: cmd_1_fail.py
# 公众号: 玩转测试开发


def run_fail_1():
    print("cmd_1 start run")
    raise BaseException
    print("cmd_1 end")


if __name__ == '__main__':
    run_fail_1()

cmd2_success = python cmd_2_success.py

# -*- coding: utf-8 -*-
# time: 2022/10/4 21:49
# file: cmd_2_success.py


def run_success_2():
    print("cmd_2 start run")
    print("cmd_2 do something")
    print("cmd_2 end")


if __name__ == '__main__':
    run_success_2()

cmd2_fail = python cmd_2_fail.py

# -*- coding: utf-8 -*-
# time: 2022/10/4 21:51
# file: cmd_2_fail.py
# 公众号: 玩转测试开发


def run_fail_2():
    print("cmd_2 start run")
    raise BaseException
    print("cmd_2 end")


if __name__ == '__main__':
    run_fail_2()

即:存在4种基本情况

cmd_1_success、cmd_2_success
cmd_1_fail、cmd_2_success
cmd_1_success、cmd_2_fail
cmd_1_fail、cmd_2_fail

test.sh

#bin/bash
python cmd_1_success; python cmd_2_success
python cmd_1_success && python cmd_2_success
python cmd_1_success || python cmd_2_success

演示分号的四类基础情况:

# 第一种情况:cmd_1_success; cmd_2_success
cmd_1 start run
cmd_1 do something
cmd_1 end
cmd_2 start run
cmd_2 do something
cmd_2 end
# 第二种情况:cmd_1_fail; cmd_2_success
cmd_1 start run
Traceback (most recent call last):
  File "cmd_1_fail.py", line 14, in <module>
    run_fail_1()
  File "cmd_1_fail.py", line 9, in run_fail_1
    raise BaseException
BaseException
cmd_2 start run
cmd_2 do something
cmd_2 end

# 第三种情况:cmd_1_success; cmd_2_fail
cmd_1 start run
cmd_1 do something
cmd_1 end
cmd_2 start run
Traceback (most recent call last):
  File "cmd_2_fail.py", line 14, in <module>
    run_fail_2()
  File "cmd_2_fail.py", line 9, in run_fail_2
    raise BaseException
BaseException

# 第四种情况:cmd_1_fail; cmd_2_fail
cmd_1 start run
Traceback (most recent call last):
  File "cmd_1_fail.py", line 14, in <module>
    run_fail_1()
  File "cmd_1_fail.py", line 9, in run_fail_1
    raise BaseException
BaseException
cmd_2 start run
Traceback (most recent call last):
  File "cmd_2_fail.py", line 14, in <module>
    run_fail_2()
  File "cmd_2_fail.py", line 9, in run_fail_2
    raise BaseException
BaseException

演示 && 的四类基本情况:

# 第一种情况:cmd_1_success && cmd_2_success
cmd_1 start run
cmd_1 do something
cmd_1 end
cmd_2 start run
cmd_2 do something
cmd_2 end

# 第二种情况:cmd_1_fail && cmd_2_success
cmd_1 start run
Traceback (most recent call last):
  File "cmd_1_fail.py", line 14, in <module>
    run_fail_1()
  File "cmd_1_fail.py", line 9, in run_fail_1
    raise BaseException
BaseException

# 第三种情况:cmd_1_success && cmd_2_fail
cmd_1 start run
cmd_1 do something
cmd_1 end
cmd_2 start run
Traceback (most recent call last):
  File "cmd_2_fail.py", line 14, in <module>
    run_fail_2()
  File "cmd_2_fail.py", line 9, in run_fail_2
    raise BaseException
BaseException

# 第四种情况:cmd_1_fail && cmd_2_fail
cmd_1 start run
Traceback (most recent call last):
  File "cmd_1_fail.py", line 14, in <module>
    run_fail_1()
  File "cmd_1_fail.py", line 9, in run_fail_1
    raise BaseException
BaseException

演示 || 的四类基本情况:

# 第一种情况:cmd_1_success || cmd_2_success
cmd_1 start run
cmd_1 do something
cmd_1 end

# 第二种情况:cmd_1_fail || cmd_2_success
cmd_1 start run
Traceback (most recent call last):
  File "cmd_1_fail.py", line 14, in <module>
    run_fail_1()
  File "cmd_1_fail.py", line 9, in run_fail_1
    raise BaseException
BaseException
cmd_2 start run
cmd_2 do something
cmd_2 end

# 第三种情况:cmd_1_success || cmd_2_fail
cmd_1 start run
cmd_1 do something
cmd_1 end

# 第四种情况:cmd_1_fail || cmd_2_fail
cmd_1 start run
Traceback (most recent call last):
  File "cmd_1_fail.py", line 14, in <module>
    run_fail_1()
  File "cmd_1_fail.py", line 9, in run_fail_1
    raise BaseException
BaseException
cmd_2 start run
Traceback (most recent call last):
  File "cmd_2_fail.py", line 14, in <module>
    run_fail_2()
  File "cmd_2_fail.py", line 9, in run_fail_2
    raise BaseException
BaseException

即:分号前后运行无逻辑关系;&&后面的命令需要前面的命令正常执行;

||后面的命令需要前面的命令运行失败才执行。

到此这篇关于Linux Shell脚本多命令执行逻辑的示例详解的文章就介绍到这了,更多相关Shell脚本多命令执行逻辑内容请搜索aitechtogether.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持aitechtogether.com!

退出移动版