Get the App
SLTechnology News&Howtos  ›  Servers  › 

Use Python to generate Shell commands and batch execute program code parsing

Shulou Source: shulou.com Published: 2022-06-02 03:52:58 09月14日 Update

This paper mainly studies the related contents of the batch execution program that uses Python to generate Shell commands in Linux system, as follows.

These days it is common to encounter batch execution of similar commands in Linux shell. For example, execute the following command:

Gifsicle-- delay=100 gif/App_1_hour_*_down.gif > combine_gif/App_1_hour_down.gif gifsicle-- delay=100 gif/App_1_hour_*_up.gif > combine_gif/App_1_hour_up.gif gifsicle-- delay=100 gif/App_2_hour_*_down.gif > combine_gif/App_2_hour_down.gif gifsicle-- delay=100 gif/App_2_hour_*_up.gif > combine_gif/App_2_hour _ up.gif gifsicle-- delay=100 gif/App_3_hour_*_down.gif > combine_gif/App_3_hour_down.gif gifsicle-- delay=100 gif/App_3_hour_*_up.gif > combine_gif/App_3_hour_up.gif gifsicle-- delay=100 gif/App_4_hour_*_down.gif > combine_gif/App_4_hour_down.gif gifsicle-- delay=100 gif/App_4_hour_*_up.gif > combine_gif/App _ 4_hour_up.gif gifsicle-- delay=100 gif/App_5_hour_*_down.gif > combine_gif/App_5_hour_down.gif

If you enter manually, the error rate is high, the time cost is high, and the efficiency is very low. It is easy to batch process the above programs using Shell programming, but it can also be easily implemented if you are not familiar with Shell programming. The key is the change of thinking. We can use Python to write the above command to a file, and then copy the contents of the file into the .sh file at the beginning of #! / bin/bash to build the required Shell script file. The Python code that generates the above command is as follows:

Output = open ("C:\\ Python34\\ shell_commands.txt", "w") for i in range (1 21): wr_line_1 = "gifsicle-- delay=100 gif/App_" + str (I) + "_ hour_*_down.gif > combine_gif/App_" + str (I) + "_ hour_down.gif" + "\ n" wr_line_2 = "gifsicle-- delay=100 gif/App_" + str (I) + "_ hour_*_up.gif > combine_gif/App_" + str (I) + "_ hour_up.gif" + "\ n "output.writelines (wr_line_1) output.writelines (wr_line_2) output.close ()

Summary

This is the whole content of this article about using Python to generate Shell commands and batch execute program code parsing. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site, if there are any deficiencies, please leave a message to point out. Thank you for your support to this site!

Tags: Commands files programs generation content code friends programming low similar topics key interests that is beginning thinking situation cost manual copy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Shulou Technology Docker Shulou Information