Home > Backend Development > Python Tutorial > Share a powerful shell language and command prompt based on Python

Share a powerful shell language and command prompt based on Python

WBOY
Release: 2023-04-11 19:49:16
forward
1193 people have browsed it

Share a powerful shell language and command prompt based on Python

Xonsh is a Python-based, cross-platform, Unix-oriented shell language and command prompt. It is essentially a Python interpreter with good syntax for running Shell commands. . So you have all the power of Python in a familiar shell environment. Works on all major systems including Linux, OSX and Windows.

Bash scripts are fast and efficient for small or batch applications. One of the limitations of Bash is its handling of mathematical functions and floating point numbers.

If you are a Python programmer, you will like it very much. It has a huge library of available functions.

For Raspberry Pi users, Xonsh can provide many opportunities to write some extremely streamlined scripts. Python can be used to connect to third-party devices and sensors.

In this article, I will introduce Xonsh through some examples.

Getting Started

See the Xonsh documentation for system-specific installation instructions. To install on Raspberry Pi/Ubuntu/Debian, enter:

linuxmi@linuxmi:~/www.linuxmi.com$ sudo apt install xonsh
Copy after login

Share a powerful shell language and command prompt based on Python

To run Xonsh, just enter: xonsh

Share a powerful shell language and command prompt based on Python

Out-of-the-box Xonsh provides a configuration wizard and a tutorial.

Using Python

Python code can be entered directly on the command line. The version of Python will depend on what is loaded on the underlying system. To check your version:

linuxmi@linuxmi ~/www.linuxmi.com $ import sys
linuxmi@linuxmi ~/www.linuxmi.com $ sys.version
'3.9.7 (default, Sep 10 2021, 14:59:43) n[GCC 11.2.0]'
Copy after login

Share a powerful shell language and command prompt based on Python

As with the interactive Python interface, no print statements are required to view the output:

linuxmi@linuxmi ~/www.linuxmi.com $ 7+9
16
linuxmi@linuxmi ~/www.linuxmi.com $ a=5;b=6
linuxmi@linuxmi ~/www.linuxmi.com $ a+b
11
linuxmi@linuxmi ~/www.linuxmi.com $ i="Hello World!"
linuxmi@linuxmi ~/www.linuxmi.com $ i + " www.linuxmi.com"
'Hello World! www.linuxmi.com'
Copy after login

Share a powerful shell language and command prompt based on Python

Using Bash

Xonsh uses Python first, so take ls (Bash list command) as an example:

In the above example ***ls*** is used first as Bash list command, but if a variable with the same name is defined, that variable is referenced.

linuxmi@linuxmi ~/www.linuxmi.com $ ls
'VS Code.png' www.linuxmi.com.jpg www.linuxmi.com.py wwww.linuxmi.com
 linuxmi.comwww.linuxmi.com.mp4 www.linuxmi.png
linuxmi@linuxmi ~/www.linuxmi.com $ ls="这是一个变量www.linuxmi.com"
linuxmi@linuxmi ~/www.linuxmi.com $ ls
'这是一个变量www.linuxmi.com'
Copy after login

Share a powerful shell language and command prompt based on Python

The first ls is used as the Bash list command, and the second one is the Xonsh display variable ls.

Xonsh handles Bash statements as single lines. This means:

  • Bash for/while/if statements need to be all on one line
  • Remember to use spaces between characters
  • Line expansion (with "") is not supported
  • Bash functions are not supported.
  • You can write functions in Python instead of Bash

Using Python in Bash

Python statements are used in Bash: @( Python statements). Here are two examples of using Python with Bash:

linuxmi@linuxmi ~/www.linuxmi.com $ import sys
linuxmi@linuxmi ~/www.linuxmi.com $ echo @(sys.version)
3.9.7 (default, Sep 10 2021, 14:59:43)
[GCC 11.2.0]
linuxmi@linuxmi ~/www.linuxmi.com $ echo @("LinuxMi.com=" +str(8+9))
LinuxMi.com=17
Copy after login

Share a powerful shell language and command prompt based on Python

Examples

linuxmi@linuxmi ~/www.linuxmi.com $ from tkinter import *
linuxmi@linuxmi ~/www.linuxmi.com $ root = Tk()
linuxmi@linuxmi ~/www.linuxmi.com $ canvas = Canvas(root, width = 500, height =
500)
linuxmi@linuxmi ~/www.linuxmi.com $ canvas.pack()
linuxmi@linuxmi ~/www.linuxmi.com $ img = PhotoImage(file="www.linuxmi.com.png")
linuxmi@linuxmi ~/www.linuxmi.com $ canvas.create_image(0,0, anchor=NW, image=im
g)
1
linuxmi@linuxmi ~/www.linuxmi.com $ mainloop()
Copy after login

Share a powerful shell language and command prompt based on Python

Using Bash with Python

Bash variables can be used directly in Python, for example:

Use Bash date and pass it to Python

linuxmi@linuxmi ~/www.linuxmi.com $ now=$(date)
linuxmi@linuxmi ~/www.linuxmi.com $ print("现在的时间是: " + now)
现在的时间是: 2022年 04月 01日 星期五 19:17:13 CST
Copy after login

Share a powerful shell language and command prompt based on Python

Question

Working between different shells and subshells can be a bit confusing. I find that I sometimes get confused about which shell I'm working in. The ps command will tell me if xonsh* is running:

linuxmi@linuxmi ~/www.linuxmi.com $ ps
PID TTYTIME CMD
 7966 pts/100:00:00 bash
 8044 pts/100:00:04 xonsh
11342 pts/100:00:00 ps
Copy after login

Share a powerful shell language and command prompt based on Python

I am able to pass the Xonsh script to the Bash script without any problems, but I find that for some operation, I need to kill the Xonsh shell manually.

Summary

Xonsh is suitable for everyday use by experts and novices alike. Unlike other shells, xonsh is based on Python, adding additional syntax that makes it easy to invoke subprocess commands, operate the environment, and work with file systems. The xonsh command prompt provides users with interactive access to the xonsh language.

Xonsh has a lot of potential for users looking for a simple scripting solution.

For myself, I would probably stick with a Bash or Python solution, but I have other options.

The above is the detailed content of Share a powerful shell language and command prompt based on Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template