欧美vvv,亚洲第一成人在线,亚洲成人欧美日韩在线观看,日本猛少妇猛色XXXXX猛叫

新聞資訊

    ash和Python是大多數自動化工程師最喜歡的編程語言。兩者都有利有弊,有時很難選擇,那么你應該使用哪一種。答案是:它取決于任務,范圍,背景和復雜性。

    讓我們比較這兩種語言,以便更好地了解每種語言的選擇。

    Bash

    • 是Linux/Unix shell命令語言
    • 非常適合編寫使用命令行界面(CLI)實用程序的shell腳本,利用從一個命令到另一個命令(管道)的輸出,以及執行簡單任務(最多100行代碼)
    • 可以按原樣使用命令行命令和實用程序
    • 具有比Python更好的啟動時間,但執行時間性能較差
    • 不預裝在Windows中;你的腳本可能與多個操作系統不兼容,但Bash是大多數Linux/Unix系統上的默認shell
    • 是不是與其他殼完全兼容(例如,CSH,zsh,fish)
    • 管道(“|”)CLI實用程序(如sed,awk,grep等)可能會降低其性能
    • 缺少許多函數,對象,數據結構和多線程,這限制了它在復雜腳本/編程中的使用
    • 缺乏良好的調試工具和實用程序

    Python

    • 是一種面向對象編程(OOP)語言,因此它比Bash更通用
    • 幾乎可以用于任何任務
    • 適用于大多數主要操作系統,默認情況下也安裝在大多數Unix/Linux系統上
    • 與編寫pseudo代碼非常相似
    • 具有簡單,清晰,易于學習和易于閱讀的語法
    • 擁有大量的庫,文檔和活躍的社區
    • 提供比Bash更好的錯誤處理功能
    • 擁有比Bash更好的調試工具和實用程序,這使它成為開發涉及多行代碼的復雜軟件應用程序的理想語言
    • 應用程序(或腳本)可以包含許多必須在執行之前安裝的第三方依賴項
    • 與Bash相比,需要為簡單任務編寫更多代碼行
    • 希望這些內容能讓你更好地了解使用哪種語言,以及何時使用它。

    1. os.system()

    help(os.system)

    1.1. demo

    #os.system(command):該方法在調用完shell腳本后,返回一個16位的二進制數,
    #低位為殺死所調用腳本的信號號碼,高位為腳本的退出狀態碼,
    #即腳本中exit 1的代碼執行后,os.system函數返回值的高位數則是1,如果低位數是0的情況下,
    #則函數的返回值是0x0100,換算為十進制得到256。
    #要獲得os.system的正確返回值,可以使用位移運算(將返回值右移8位)還原返回值:
    >>> import os
    >>> os.system("./test.sh")
    hello python!
    hello world!
    256
    >>> n>>8
    1

    2. os.popen()

    help(os.system)

    2.1 demo

    #os.popen(command):這種調用方式是通過管道的方式來實現,函數返回一個file對象,
    #里面的內容是腳本輸出的內容(可簡單理解為echo輸出的內容),使用os.popen調用test.sh的情況
    
    >> import os
    >>> os.popen("./test.sh")
    <open file './test.sh', mode 'r' at 0x7f6cbbbee4b0>
    >>> f=os.popen("./test.sh")
    >>> f
    <open file './test.sh', mode 'r' at 0x7f6cbbbee540>
    >>> f.readlines()
    ['hello python!\n', 'hello world!\n']

    3. commands模塊

    (1)commands.getstatusoutput(cmd),其以字符串的形式返回的是輸出結果和狀態碼,即(status,output)。

    (2)commands.getoutput(cmd),返回cmd的輸出結果。

    (3)commands.getstatus(file),返回ls -l file的執行結果字符串,調用了getoutput,不建議使用此方法

    4. subprocess

    subprocess模塊,允許創建很多子進程,創建的時候能指定子進程和子進程的輸入、輸出、錯誤輸出管道,執行后能獲取輸出結果和執行狀態。

    (1)subprocess.run():python3.5中新增的函數, 執行指定的命令, 等待命令執行完成后返回一個包含執行結果的CompletedProcess類的實例。

    (2)subprocess.call():執行指定的命令, 返回命令執行狀態, 功能類似os.system(cmd)。

    (3)subprocess.check_call():python2.5中新增的函數, 執行指定的命令, 如果執行成功則返回狀態碼, 否則拋出異常。

    說明:subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, universal_newlines=False)

    subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

    subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

    args:表示shell指令,若以字符串形式給出shell指令,如"ls -l “則需要使shell=Ture。否則默認已數組形式表示shell變量,如"ls”,"-l"。

    當使用比較復雜的shell語句時,可以先使用shlex模塊的shlex.split()方法來幫助格式化命令,然后在傳遞給run()方法或Popen。

    4.1 demo

    # Stubs for subprocess
    
    # Based on http://docs.python.org/2/library/subprocess.html and Python 3 stub
    
    from typing import Sequence, Any, Mapping, Callable, Tuple, IO, Union, Optional, List, Text
    
    _FILE=Union[None, int, IO[Any]]
    _TXT=Union[bytes, Text]
    _CMD=Union[_TXT, Sequence[_TXT]]
    _ENV=Union[Mapping[bytes, _TXT], Mapping[Text, _TXT]]
    
    # Same args as Popen.__init__
    def call(args: _CMD,
         bufsize: int=...,
         executable: _TXT=...,
         stdin: _FILE=...,
         stdout: _FILE=...,
         stderr: _FILE=...,
         preexec_fn: Callable[[], Any]=...,
         close_fds: bool=...,
         shell: bool=...,
         cwd: _TXT=...,
         env: _ENV=...,
         universal_newlines: bool=...,
         startupinfo: Any=...,
         creationflags: int=...) -> int: ...
    
    def check_call(args: _CMD,
            bufsize: int=...,
            executable: _TXT=...,
            stdin: _FILE=...,
            stdout: _FILE=...,
            stderr: _FILE=...,
            preexec_fn: Callable[[], Any]=...,
            close_fds: bool=...,
            shell: bool=...,
            cwd: _TXT=...,
            env: _ENV=...,
            universal_newlines: bool=...,
            startupinfo: Any=...,
            creationflags: int=...) -> int: ...
    
    # Same args as Popen.__init__ except for stdout
    def check_output(args: _CMD,
             bufsize: int=...,
             executable: _TXT=...,
             stdin: _FILE=...,
             stderr: _FILE=...,
             preexec_fn: Callable[[], Any]=...,
             close_fds: bool=...,
             shell: bool=...,
             cwd: _TXT=...,
             env: _ENV=...,
             universal_newlines: bool=...,
             startupinfo: Any=...,
             creationflags: int=...) -> bytes: ...
    
    PIPE=... # type: int
    STDOUT=... # type: int
    
    class CalledProcessError(Exception):
      returncode=0
      # morally: _CMD
      cmd=... # type: Any
      # morally: Optional[bytes]
      output=... # type: Any
    
      def __init__(self,
             returncode: int,
             cmd: _CMD,
             output: Optional[bytes]=...) -> None: ...
    
    class Popen:
      stdin=... # type: Optional[IO[Any]]
      stdout=... # type: Optional[IO[Any]]
      stderr=... # type: Optional[IO[Any]]
      pid=0
      returncode=0
    
      def __init__(self,
             args: _CMD,
             bufsize: int=...,
             executable: Optional[_TXT]=...,
             stdin: Optional[_FILE]=...,
             stdout: Optional[_FILE]=...,
             stderr: Optional[_FILE]=...,
             preexec_fn: Optional[Callable[[], Any]]=...,
             close_fds: bool=...,
             shell: bool=...,
             cwd: Optional[_TXT]=...,
             env: Optional[_ENV]=...,
             universal_newlines: bool=...,
             startupinfo: Optional[Any]=...,
             creationflags: int=...) -> None: ...
    
      def poll(self) -> int: ...
      def wait(self) -> int: ...
      # morally: -> Tuple[Optional[bytes], Optional[bytes]]
      def communicate(self, input: Optional[_TXT]=...) -> Tuple[Any, Any]: ...
      def send_signal(self, signal: int) -> None: ...
      def terminate(self) -> None: ...
      def kill(self) -> None: ...
      def __enter__(self) -> 'Popen': ...
      def __exit__(self, type, value, traceback) -> bool: ...
    
    # Windows-only: STARTUPINFO etc.
    
    STD_INPUT_HANDLE=... # type: Any
    STD_OUTPUT_HANDLE=... # type: Any
    STD_ERROR_HANDLE=... # type: Any
    SW_HIDE=... # type: Any
    STARTF_USESTDHANDLES=... # type: Any
    STARTF_USESHOWWINDOW=... # type: Any
    CREATE_NEW_CONSOLE=... # type: Any
    CREATE_NEW_PROCESS_GROUP=... # type: Any

    5. 參考文獻

    https://www.jb51.net/article/186301.htm

網站首頁   |    關于我們   |    公司新聞   |    產品方案   |    用戶案例   |    售后服務   |    合作伙伴   |    人才招聘   |   

友情鏈接: 餐飲加盟

地址:北京市海淀區    電話:010-     郵箱:@126.com

備案號:冀ICP備2024067069號-3 北京科技有限公司版權所有