����λ�ã���ҳ > �����̳� > �̳� > pythonִ��shell����ȡ���

pythonִ��shell����ȡ���

��Դ������������|��ʱ�䣺2024-07-10 16:15:15 |���Ķ���91��|�� ��ǩ�� T Hell Shell El S Python �� |����������

������ϸ������pythonִ��shell����ȡ����ķ��������������ʹ��Shell��̣���������ϸ�Ĵ���ʾ����

��Python��ִ��Shell�����ȡ������ͨ������ʹ�� subprocess ģ�顣���ģ���������������µĽ��̣����ӵ����ǵ�����/���/����ܵ�������ȡ���ǵķ����롣������һ����ϸ��ʾ����չʾ�����ʹ�� subprocess.run() ������ִ��Shell�����ȡ�������

1. ʾ��һ��ʹ�� subprocess.run() ִ�� ls �����ȡ���

���ʾ����ִ�� ls �����Unix/Linux/macOSϵͳ���г���ǰĿ¼�µ��ļ����ļ��У������������������ͷ����롣

import subprocess  
  
# ����Ҫִ�е�����  
command = ['ls', '-l']  # ʹ���б���ʽ������ȫ�����Ա���shellע�빥��  
  
# ִ������  
# capture_output=True ������ʾ��������������stdout��stderr��  
# text=True ������ʾ�������Ϊ�ı�������Python 3.7+����֮ǰ�汾ʹ��universal_newlines=True  
result = subprocess.run(command, capture_output=True, text=True)  
  
# ��ȡ����ı�׼���  
stdout = result.stdout  
  
# ��ȡ����Ĵ������������еĻ���  
stderr = result.stderr  
  
# ��ȡ����ķ�����  
returncode = result.returncode  
  
# ��ӡ���  
print(f"��׼���:\n{stdout}")  
if stderr:  
    print(f"�������:\n{stderr}")  
print(f"������: {returncode}")  
  
# ע�⣺�������ɹ�ִ�У�returncodeͨ��Ϊ0����0ֵ��ʾ�д�����

ע������ ��

��1�� ��ȫ�� ��������ʹ�������б������ַ���������shellע�빥����������Ͳ������б���ʽ�ṩʱ��Python��ֱ�ӽ����Ǵ��ݸ�ϵͳ������ͨ��shell���ͣ��Ӷ������˰�ȫ���ա�

��2�� �ı����ֽ� �� capture_output=True �� text=True ���� universal_newlines=True ���ھɰ汾�У������ʹ��������ı����ַ�������ʽ���أ��������ֽڡ�����ڴ����ı����ݺܷ��㣬�����������Ҫ�������������ݣ���ͼ�����Ƶ�ļ������������Ҫ���ֽ���ʽ���������

��3�� ������ ��ͨ����� returncode �����ж������Ƿ�ɹ�ִ�С���� returncode ��Ϊ0���������Ҫ���� stderr �е���Ϣ��������⡣

��4�� ��ƽ̨������ ����ʾ���е� ls -l ������Unix/Linux/macOSϵͳ���еġ���Windowsϵͳ�ϣ����ǿ�����Ҫִ�в�ͬ������� dir ������������Ҫ��������ĵ��÷�ʽ�����磬ʹ�� shell=True ������ע��������Ӱ�ȫ���գ���

��5�� ���ܿ��� ��Ƶ���������ⲿ���̿��ܻή�ͳ�������ܡ�������ܣ�������Python�ڲ�������⣬���߿���ʹ�ö��߳�/����������д����ⲿ����ĵ��á�

2. ʾ������ʹ�� subprocess.run() ������ִ��Shell����

������һ������ϸ�Ĵ���ʾ������չʾ�������Python��ʹ�� subprocess.run() ������ִ��Shell���������������� ls -l �������������ܳ��ֵĸ�������������ɹ�ִ�С�������ڡ��Լ������׼����ʹ��������

��ע�⣬���ʾ������������һ��Unix/Linux/macOSϵͳ�����У���Ϊ ls -l ����Щϵͳ��������������Windows�ϣ����ǿ�����Ҫ�滻Ϊ dir �����������Ҫ���� shell ������ʹ�ã�����ͨ���������ʹ�� shell=True �Ա��ⰲȫ���գ���

import subprocess  
  
def run_command(command):  
    """  
    ִ�и������������������ͷ����롣  
  
    ����:  
    - command: Ҫִ�е������Ϊ�б����ݣ����� ['ls', '-l']�����Ա���shellע�롣  
  
    ����:  
    - output: ����ı�׼���������еĻ�����  
    - error: ����Ĵ������������еĻ�����  
    - returncode: ����ķ����롣  
    """  
    try:  
        # ʹ��subprocess.run()ִ������  
        # capture_output=True��ʾ����stdout��stderr  
        # text=True��ʾ�������Ϊ�ı�������Python 3.7+��  
        result = subprocess.run(command, capture_output=True, text=True, check=True)  
        # �������ɹ�ִ�У�û���쳣�����򷵻�������ͷ�����  
        return result.stdout, None, result.returncode  
    except subprocess.CalledProcessError as e:  
        # �������ִ��ʧ�ܣ��������0�����򲶻�CalledProcessError�쳣  
        # �����ش����������׼���������еĻ����ͷ�����  
        return None, e.stderr, e.returncode  
    except Exception as e:  
        # �����������ܵ��쳣����Ȼ������򵥵������п��ܲ�̫������  
        return None, f"An unexpected error occurred: {e}", None  
  
# ����Ҫִ�е�����  
command = ['ls', '-l']  
  
# ִ�������ȡ���  
output, error, returncode = run_command(command)  
  
# ���ݷ��صĽ����ӡ��Ӧ����Ϣ  
if output:  
    print("��׼���:")  
    print(output)  
if error:  
    print("�������:")  
    print(error)  
if returncode is not None:  
    print(f"������: {returncode}")  
    if returncode == 0:  
        print("����ɹ�ִ�С�")  
    else:  
        print("����ִ��ʧ�ܡ�")

�����ʾ���У� run_command ������װ�� subprocess.run() �ĵ��ã��������˼��ֿ��ܵ������

��1������ɹ�ִ�У�������Ϊ0�������ر�׼����� None ��Ϊ����������Լ������롣

��2������ִ��ʧ�ܣ��������0�������� subprocess.CalledProcessError �쳣�������� None ��Ϊ��׼���������������Լ������롣

��3�������쳣��������񲢷���һ��������Ϣ�� None ��Ϊ�����루��Ȼ������ض��������У����� subprocess.run() ͨ��ֻ�׳� CalledProcessError �������ⲿ�ֿ��ܲ��ᱻִ�У���

��ע�⣬ subprocess.run() �� check=True ������������ط����˳���ʱ�Զ��׳� CalledProcessError �쳣����ʹ�����ǿ����� try-except ���в�������Ȼ���������ʾ���У���ѡ������ʽ�ز����쳣���Ա��ܹ������ش�������ͷ����롣�������ֻ��������ʧ��ʱ�׳��쳣�����Ҳ����Ĵ������ľ���ϸ�ڣ���ô�����ڵ��� subprocess.run() ʱ���� check=True ������Python��Ĭ���쳣������������������

3. shell��̼�shell����

3.1 Shell���

Shell�����ָʹ��Shell��Ҳ��Ϊ�����н������������н��棩��Ϊ�����������д�ű��Ĺ��̡�Shell��Unix/Linux/macOS����Unix����ϵͳ�е�һ�����������Ϊ�û��ṩ��һ�������ϵͳ�����Ļ�����Shell�ű���һϵ��Shell����ļ��ϣ���Щ�����д���ı��ļ��У���ͨ��Shell������ִ�У���ʵ���Զ��������������ļ�������ϵͳ��Դ��Ŀ�ġ�

Shell�ű����п�ƽ̨�ԣ���Ϊ������Ҫ������Shell�Ĺ��ܺ��������Щ�ڴ������Unixϵͳ�ж������Ƶġ�Ȼ������ͬ��Shell����Bash��Zsh��Fish�ȣ��������Լ������Ժ���չ����˱�д�Ľű�������Ҫ����ض���Shell�������䡣

Shell���ͨ�������������塢�����жϡ�ѭ�����ơ��������õȱ��Ԫ�أ����봫ͳ���������ȣ�Shell�ű����﷨��Լ�����

3.2 Shell����

Shell�������û���Shell�����������ָ�����ִ�и��ֲ��������ļ�����������ִ�С�ϵͳ�����ȡ�Shell���������Shell���õģ�Ҳ������ϵͳ�ϵ��ⲿ����

��1�� �������� ����Shell�����ṩ�������Щ������Shell����ʱ���Ѿ����ص��ڴ��У����ִ���ٶȽϿ졣�������������ϵͳ�ϵ������������������ϵͳ����ʱ���Ѿ����á������������������ cd ���ı�Ŀ¼���� echo ����ʾ��Ϣ���� exit ���˳�Shell���ȡ�

��2�� �ⲿ���� ��Ҳ��Ϊ�ļ�ϵͳ�����Щ������ϵͳ�϶����ij���ͨ��λ�� /bin �� /usr/bin �� /sbin �� /usr/sbin ��Ŀ¼�¡���Shell��Ҫִ����Щ����ʱ�����������ЩĿ¼���ҵ���Ӧ�ij���ִ�С��������ⲿ������� ls ���г�Ŀ¼���ݣ��� cp �������ļ���Ŀ¼���� mv ���ƶ����������ļ���Ŀ¼���ȡ�

Shell�������ͨ���ܵ��� | �����ض��� > �� < �� >> ���������滻�� command �� $(command) ���Ȼ��ƽ�����ϣ���ʵ�ָ����ӵIJ��������磬 ls -l | grep '^d' ������г���ǰĿ¼������Ŀ¼����ϸ��Ϣ�� ls -l �г���ϸ��Ϣ�� grep '^d' ɸѡ���� d ��ͷ���У���Ŀ¼����

Shell��̺�Shell������Unix/Linux/macOS��ϵͳ�û��ճ������в��ɻ�ȱ�Ĺ��ߣ������ܹ����������û��Ĺ���Ч�ʣ��������û��Զ�������ɸ�������

3.3 ���ʹ��Shell���

ʹ��Shell�����Ҫ�漰����дShell�ű�����Щ�ű�������һϵ�е�Shell���ͨ��Shell������ִ����ʵ���ض��Ĺ��ܡ�������ʹ��Shell��̵Ļ������裺

3.3.1 ѡ��Shell

���ȣ�������Ҫȷ��ʹ������Shell��������Shell��Bash��Bourne Again SHell�������Linux���а��Ĭ��Shell����Zsh��Z Shell������������ǿ���Ժ͸��õ��û����飩��Fish��Friendly Interactive SHell�����û��Ѻú�����ѧϰ�����ƣ��ȡ����ڳ�ѧ����˵��Bash��һ���ܺõ���㣬��Ϊ���㷺�������ĵ��ḻ��

3.3.2 ��дShell�ű�

Shell�ű�ͨ���������� .sh Ϊ��չ�����ļ��С����ǿ���ʹ���κ��ı��༭������дShell�ű������� nano �� vim �� emacs ��򵥵� echo ���ض���

������һ���򵥵�Shell�ű�ʾ��������ӡ����Hello, World!����

#!/bin/bash  
# ����һ���򵥵�Shell�ű�ʾ��  
echo "Hello, World!"

�ڽű��ĵ�һ�У� #!/bin/bash ����Ϊshebang��������ϵͳ����ű�Ӧ��ʹ���ĸ���������ִ�С�����������У���ָ����Bash��

3.3.3 ����ű�

�����ǵĽű����浽�ļ��У����� hello.sh ��

3.3.4 ����ִ��Ȩ��

��Linux��macOS�ϣ�������Ҫ���ű��ļ�����ִ��Ȩ�ޣ��Ա��ܹ�ֱ�������������ǿ���ʹ�� chmod ������������һ�㣺

bash���ƴ���

chmod +x hello.sh

��������� hello.sh �ļ�����ִ��Ȩ�ޡ�

3.3.5 ���нű�

���ڣ����ǿ���ͨ���������ַ�ʽ֮һ���������ǵĽű���

ֱ��ͨ���ű���·�������ƣ�����ű�����ִ��Ȩ�ޣ���

bash���ƴ���

./hello.sh

ע�⣬������Ҫʹ�� ./ ��ָ���ű�λ�ڵ�ǰĿ¼�¡�

ʹ��Shell��������ִ�нű������۽ű��Ƿ����ִ��Ȩ�ޣ���

bash���ƴ���

bash hello.sh

�����������Bash��������ִ�� hello.sh �ű��е����

С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

Shell 1.0
Shell 1.0
���ͣ��������������Ӫ״̬����ʽ��Ӫ�������ԣ� Ӣ�� ����

��Ϸ����

��Ϸ���

��Ϸ��Ƶ

��Ϸ����

��Ϸ�

��Shell������Ϸ��ShamBdour������һ��Ȥζ������ؽ�����Ϸ����Ϸ�У���ҽ������ڹ꣬����Ҫ�����Լ���

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������