Python 执行 Shell 命令

2024 年 8 月 29 日 | 阅读 17 分钟

自动化重复性工作是一个很棒的主意。开发人员和系统管理员经常使用 shell 脚本来自动化重复性过程,例如健康检查和文件备份。然而,随着这些活动变得越来越复杂,shell 脚本的维护可能会变得更加困难。幸运的是,Python 可以用于自动化,而不是 shell 脚本。Python 通过运行 shell 命令的方法提供了与那些 shell 脚本相同的功能。Python 执行 shell 命令的能力为我们系统化、可扩展的计算机过程自动化打开了大门。

以下是使用 Python 运行 shell 命令的一些优势示例

  • 在 Python 中导入必要的模块简单且交互式。
  • 这些技术可用于启动任何外部文件,例如 .exe,或任何程序。
  • Python 为程序员提供了更大的灵活性和强大的功能。它有助于流程自动化。Python shell 脚本可用于完成许多单调乏味、缺乏原创性和生产力的任务,例如磁盘扫描、删除不必要的缓存文件、备份、恢复以及更多任务。

了解 Shell

一个经常被误用的短语是 shell。在计算中,shell 是一种软件,它提供用户界面来使用操作系统功能。

根据设备的功能和基本功能,操作系统的 shell 可以是 GUI(图形用户界面)或 CLI(命令行界面)。Windows 中的 shell 是一个程序,它允许您通过“命令行”与操作系统进行通信,在 Linux 和 Mac 中有时也称为终端。您可以使用 Shell 访问 Unix 系统。您提供输入,它使用该输入来运行程序。程序完成运行后,将显示其输出。我们可以在 shell 环境中执行我们的命令、程序和 shell 脚本。与有多种操作系统一样,有多种 shell 变体。每种类型的 shell 都有独特的常用命令和功能集。命令行解释器,有时也称为 shell,允许 Linux 和 Unix 用户使用命令行界面管理他们的操作系统。Shell使用户能够有效地直接与他们的操作系统进行交互。

尽管 shell 脚本不是一种单一的语言,但即使没有编程背景的人也很容易学习,因为它使用了一些日常英语中的命令。为了规划更复杂的任务,shell 需要大量的技巧,因为每种 shell 脚本方言都被视为一种语言。建立自动化的最简单方法之一是 shell 脚本。shell 脚本允许数据科学家、DevOps 和 TechOps 专业人员使用 Linux 或 Unix 命令通过条件和循环控制结构重复操作。shell 脚本有多种名称。Bash(最流行的)、csh 和 tesh 是 shell 脚本。在 IBM 的 VM 操作系统中,shell 脚本被称为 EXEC,而在 DOS 中,shell 脚本被称为批处理文件。命令行 shell 和图形 shell 是两种类型的 shell。命令行界面用于访问命令行 shell,命令行 shell 用于在计算机以人类可读指令形式接收输入时显示输出。

图形 shell 使用图形用户界面 (GUI) 来执行交互和关键任务,包括打开、关闭和保存文件。由于其有效性和易于更新,Shell 在几乎所有操作系统上都被广泛使用。它会为您的计算机执行例行备份和系统监控,而无需您担心。由于 shell 的语法和命令与放置在命令行中的命令相同,因此无需在它们之间移动。Shell 脚本也可以快速轻松地创建,因为它们运行速度快且易于进行故障排除。

使用 os.system 运行命令

使用 Python 中的 os.system() 方法,我们可以即时运行保存在字符串中的 shell 命令。该模块提供了一种可移植的方法来使用依赖于操作系统的功能。Open() 可用于简单地读取或写入文件,os.path 模块可用于更改路径,文件输入模块可用于读取命令行文件中的每一行。应使用 tempfile 模块创建临时文件和目录,而 shutil 模块应处理高级文件和目录操作。

无论它是 Windows 平台、Macintosh 还是 Linux,我们都可以利用 OS 模块提供的功能来处理底层操作系统任务。在本课程中,我们将介绍这些功能以及我们可以用它们完成什么。

代码

输出

Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
0
The name of the OS is nt
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
1
Enter the command that you want to execute::
dir
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
2
Command executed successfully.
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
3
The output of the dir command is  

07/10/2022  11:12 PM              .
07/10/2022  11:12 PM              ..
07/10/2022  08:22 PM                 0 batch.bat
07/10/2022  08:22 PM                 0 clean.sh
07/10/2022  08:21 PM             2,304 code.py
07/10/2022  08:22 PM                 0 file_src.cpp
07/10/2022  11:12 PM                 0 output.txt
07/10/2022  08:22 PM                 0 program_c1.c
07/10/2022  08:22 PM                 0 program_c2.c
07/10/2022  08:22 PM                 0 program_c3.c
07/10/2022  08:22 PM                 0 program_c4.c
07/10/2022  11:12 PM                 0 sou.srt
07/10/2022  08:22 PM                 0 src.pdf
              11 File(s)          2,304 bytes
               2 Dir(s)  396,554,895,360 bytes free

For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
1
Enter the command that you want to execute::
date
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
2

Command executed successfully
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
3
The output of the date command is The current date is: Sun 07/10/2022
Enter the new date: (mm-dd-yy)
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
4

说明

因此,在上面编写的代码中,我们编写了主函数,用户会看到显示不同选项的菜单;用户可以从该菜单中选择的各种选项包括:打印程序正在执行的操作系统的名称、输入用户想要执行的 shell 命令、实际执行用户输入的命令,然后打印从执行特定命令获得的结果。所有这些选项都列出以表示上面编写的类中编写的不同函数,创建了上面编写的类的对象,并使用该对象调用了各种函数。

使用 subprocess 模块

您可以使用 subprocess 模块启动新进程,连接到它们的输入/输出/错误管道,并获取它们的返回码。使用 OS 或 subprocess 模块中的库函数,如 os.fork()、subprocess.Popen() 等,程序可以启动新进程。这些进程称为子进程,它们独立于彼此运行,每个进程都有自己的私有系统状态和执行主线程。子进程作为其独立进程与主进程同时运行。换句话说,当子进程在后台处理其自己的任务时,主进程可以继续处理其他任务。subprocess 模块使开发人员可以直接从 Python 启动进程或应用程序。换句话说,您可以使用 subprocess 模块启动应用程序并向它们提供输入。为了替换 OS 模块的 os.popen、os.spawn 和 os.system 函数集合,以及 popen2 和以前的 commands 模块,subprocess 模块最初是在 Python 2.4 中引入的。

代码

输出

Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
0
The name of the OS is nt
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
1
Enter the command that you want to execute::
dir
Are there any parameters for dir command? [y/n]
y
Enter the args for dir command
/L
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
2
Command executed successfully
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
3
Output of the ['dir', '/L'] command is 

07/12/2022  07:06 PM    <DIR>          .
07/12/2022  07:06 PM    <DIR>          ..
07/10/2022  08:22 PM                 0 batch.bat
07/10/2022  08:22 PM                 0 clean.sh
07/12/2022  07:06 PM             2,635 code.py
07/10/2022  08:22 PM                 0 file_src.cpp
07/10/2022  11:13 PM                69 output.txt
07/10/2022  08:22 PM                 0 program_c1.c
07/10/2022  08:22 PM                 0 program_c2.c
07/10/2022  08:22 PM                 0 program_c3.c
07/10/2022  08:22 PM                 0 program_c4.c
07/10/2022  11:12 PM                 0 sou.srt
07/10/2022  08:22 PM                 0 src.pdf
              11 File(s)          2,704 bytes
               2 Dir(s)  396,486,488,064 bytes free

For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
1
Enter the command that you want to execute::
dir
Are there any parameters for dir command? [y/n]
n
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
2
Command executed successfully
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
3
Output of the ['dir', '/L', 'dir'] command is 

07/10/2022  11:12 PM    <DIR>          .
07/10/2022  11:12 PM    <DIR>          ..
07/10/2022  08:22 PM                 0 batch.bat
07/10/2022  08:22 PM                 0 clean.sh
07/10/2022  08:21 PM             2,304 code.py
07/10/2022  08:22 PM                 0 file_src.cpp
07/10/2022  11:12 PM                 0 output.txt
07/10/2022  08:22 PM                 0 program_c1.c
07/10/2022  08:22 PM                 0 program_c2.c
07/10/2022  08:22 PM                 0 program_c3.c
07/10/2022  08:22 PM                 0 program_c4.c
07/10/2022  11:12 PM                 0 sou.srt
07/10/2022  08:22 PM                 0 src.pdf
              11 File(s)          2,304 bytes
               2 Dir(s)  396,554,895,360 bytes free

For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To print the name of the Operating System on which the program is getting executed.
1. To enter the shell command that you want to execute.
2. To execute the command entered in the previous step.
3. To print the output of the previously executed command.
4. To exit from the code execution.
4

说明

因此,在上面编写的代码中,我们编写了主函数,用户会看到显示不同选项的菜单;用户可以从该菜单中选择的各种选项包括:打印程序正在执行的操作系统的名称、输入用户想要执行的 shell 命令、实际执行用户输入的命令,然后打印从执行特定命令获得的结果。所有这些选项都列出以表示上面编写的类中编写的不同函数,创建了上面编写的类的对象,并使用该对象调用了各种函数。

结论

因此,在本文中,我们了解了如何使用 Python 提供的各种模块执行 shell 命令,并且我们还实现了两个实现这些功能的模块的代码。