首頁 > 軟體

Linux Shell指令碼多命令執行邏輯的範例詳解

2022-10-18 14:01:25

簡介

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指令碼多命令執行邏輯內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com