Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to combine Python and Bash

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to combine Python and Bash. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

As the Linux community's reliance on the command line has grown, UNIX shells such as bash and zsh have evolved into extremely powerful tools that complement UNIX shell experience. With bash and other similar shells, many powerful features are available, such as pipes, filename wildcards, and the ability to read commands from files called scripts.

Let's look at a real-world example to demonstrate the functionality of the command line. Each time a user logs in to the service, their username is recorded in a text file. For this example, let's find out how many unique users use the service.

The series of commands in the following example shows the power of a more complex utility by linking smaller building blocks together:

$ cat names.log | sort | uniq | wc -l

pipe symbol (|Used to pass the standard output of one command to the standard input of the next. In the example here, the output of catname.txt is passed into the sort command. The output of the sort command is to rearrange each line of the file alphabetically. This is then passed to the uniq command, which removes all duplicate names. Finally, the output of uniq is passed to the wc command. wc is a count command with the-l flag set, which returns the number of rows. This allows you to chain many commands together.

However, sometimes what is required can become very complex, and chaining commands together can become awkward. In this case, shell scripts are the answer. A Shell script is a list of commands read by the Shell and executed sequentially. Shell scripts also support certain programming language basics, such as variables, flow control, and data structures. Shell scripts are useful for batch jobs that will run repeatedly. Unfortunately, shell scripts have some drawbacks:

Shell scripts can easily become overly complex and unreadable to developers who want to improve or maintain them.

The syntax and interpreters of these shell scripts are often awkward and unintuitive. The more clumsy the syntax, the less readable it is for developers who must use these scripts.

This code is usually not available in other scripts. Code reuse between scripts is often difficult, and scripts tend to be very specific to a particular problem.

Libraries for advanced functionality, such as HTML parsing or HTTP requests, are not as readily available as modern programming and scripting languages.

These problems can make shell scripts difficult to handle and often cause a lot of wasted developer time. Instead, the Python programming language can be used as a very powerful alternative. Using Python instead of Shell scripts has many benefits:

By default, all major Linux distributions have Python installed. Opening the command line and typing python immediately will take you to the Python interpreter. This universality makes it a smart choice for most scripted tasks.

Python has a syntax that is very easy to read and understand. Its style emphasizes simplicity and clean code, while allowing developers to write in a quasi-system style suitable for shell scripting.

Python is an interpreted language, which means there is no compilation phase. This makes Python an ideal language for scripting, allowing you to quickly try new code in an interpretive way. This allows developers to modify quickly without having to write the entire program to a file.

Python is a fully functional programming language. Code reuse is simple because Python modules can be easily imported and used in any Python script. Scripts can be easily extended or constructed.

Python can use excellent standard libraries and thousands of third-party libraries to handle various advanced utilities, such as resolvers and request libraries. Python's standard library, for example, includes a date-time library that allows you to parse dates into any format you specify and easily compare them to other dates.

Python should not replace all bash commands. Writing Python programs that run UNIX (that is, reading in standard input and writing standard output) is just as powerful as writing Python alternatives to existing shell commands such as cat and sort.

Let's build on the problems solved earlier in this article. In addition to what has already been done, let's find out how many times a certain user has logged into the system. The uniq command removes duplicates only, but does not provide information about how many duplicates there are. Instead of uniq, Python scripts can be used as another command in the chain. Here's a Python program that does this (in my example, I'll call this file namescount.py):

#!/ usr/bin/env python import sys if __name__ == "__main__": # Initialize a names dictionary as empty to start with. # Each key in this dictionary will be a name and the value # will be the number of times that name appears. names = {} # sys.stdin is a file object. All the same functions that # can be applied to a file object can be applied to sys.stdin. for name in sys.stdin.readlines(): # Each line will have a newline on the end # that should be removed. namename = name.strip() if name in names: names[name] += 1 else: names[name] = 1 # Iterating over the dictionary, # print name followed by a space followed by the # number of times it appeared. for name, count in names.iteritems(): sys.stdout.write("%d\t%s\n" % (count, name))

Let's see how this Python script fits into the command chain. First, it reads input from the standard input exposed through the sys.stdin object. Any output is written to the sys.stdout object, which is how standard output is implemented in Python. Python dictionaries (often called hash maps in other languages) are used to obtain mappings from user names to duplicate counts. To get the number of all users:

$ cat names.log | python namescount.py

Displays the number of user appearances and the count of user names. The next thing to do is to display, in order, the users who use the system the most. This can be done at Python level, but let's implement it using utilities already provided by core UNIX utilities. Previously, I used the sort command to sort letters. If the command provides the-rn flag, it sorts rows numerically in descending order. With Python scripts outputting as standard, simply pipe the command to sort and retrieve the desired output:

$ cat names.log | python namescount.py | sort -rn

This is a powerful example of using Python as part of a command chain. The advantages of using Python in this case are as follows:

Ability to link with tools like cat and sort. Simple utilities (reading files line by line and sorting them numerically) are handled by tried and tested UNIX commands. These commands are also read line-by-line, which means that these features can scale to large files and are fast.

When there's some heavy lifting that needs to be done in the chain, it's possible to write a very clear, concise Python script that will perform the work it needs and then transfer responsibility to the next link in the chain.

It is a reusable module, and although this example is specific to names, if you enter any input to this that contains duplicate lines, it will print out the number of duplicates per line. By modularizing Python code, you can apply it to a variety of scenarios.

To demonstrate the power of combining Python scripts in modular and pipelined ways, let's zoom in further. Let's find the top five users of the service. head is a command that allows you to specify a certain number of lines to display a given standard input. Adding it to the command chain results in the following:

$ cat names.log | python namescount.py | sort -rn | head -n 5

This shows only the first five users and ignores the rest. Similarly, to get a minimum of five users to use the service, you can use the tail command, which takes the same parameters. Printing Python commands to standard output makes it possible to build and extend their functionality.

Thank you for reading! About "how to combine Python and Bash together" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report