Shell Programming in Python Snake Shell
MakerSpace|MakerSpace #03
Create lightweight Raspberry Pi scripts with Xonsh, a Python shell that lets you write scripts in Python with Bash commands mixed in
Brooke Metcalfe and Pete Metcalfe
Shell Programming in Python Snake Shell

For Raspberry Pi users, Xonsh [1] offers many opportunities to write some extremely lightweight scripts, with Python connecting to physical devices and Bash utilities accessing system and file resources.

In this article, we look at two lean Xonsh projects for the Raspberry Pi. The first program connects to a DHT11 temperature and humidity sensor and shows the results in a Bash dialog in just five lines. The second project calls the Bash top utility in a lean eight lines to show the Raspberry Pi idle time and user time on a 16x2 LCD screen.

Getting Started

Xonsh has two requirements: Python 3.8 or greater and a Bash shell. To install and run Xonsh on a Raspberry Pi, Ubuntu, or Debian system, enter:

sudo apt install xonsh
xonsh

When the Xonsh shell opens, the terminal label changes so you can see that you are working in Xonsh rather than a standard Bash shell (Figure 1). When the shell first starts up, two options are presented: xonfig tutorial, which opens a browser help window, and xonfig web, which allows users to tweek the xonsh shell configuration.

Within the Xonsh shell, you can enter both Bash and Python statements, such as:

$ # Mix Bash and Python Lines
$ echo "Time is:" $(date +%T) ; # Bash
Time is: 12:59:51
$ print("%d + %d = %d" % U
            (2,3,2+3)) ; # Python
2 + 3 = 5

Xonsh in the Terminal

Denne historien er fra MakerSpace #03-utgaven av MakerSpace.

Start din 7-dagers gratis prøveperiode på Magzter GOLD for å få tilgang til tusenvis av utvalgte premiumhistorier og 9000+ magasiner og aviser.

Denne historien er fra MakerSpace #03-utgaven av MakerSpace.

Start din 7-dagers gratis prøveperiode på Magzter GOLD for å få tilgang til tusenvis av utvalgte premiumhistorier og 9000+ magasiner og aviser.

FLERE HISTORIER FRA MAKERSPACESe alt
Access the Raspberry Pi's GPIO Pig Pen
MakerSpace

Access the Raspberry Pi's GPIO Pig Pen

The wiringPi library, which many Raspberry Pi fans have grown attached to over the years, is no longer under maintenance by its developer. An alternative, in the form of Pigpio, has arrived just in time

time-read
5 mins  |
MakerSpace #03
Shell Programming in Python Snake Shell
MakerSpace

Shell Programming in Python Snake Shell

Create lightweight Raspberry Pi scripts with Xonsh, a Python shell that lets you write scripts in Python with Bash commands mixed in

time-read
4 mins  |
MakerSpace #03
Playing old DOS games on the Raspberry Pi Retro Gamer
MakerSpace

Playing old DOS games on the Raspberry Pi Retro Gamer

Play old DOS games on the Dosbian operating system, which turns the Raspberry Pi into an 80486 PC

time-read
6 mins  |
MakerSpace #03
Home Assistant makes the Raspberry Pi fit for the Z-Wave world Home Automaton
MakerSpace

Home Assistant makes the Raspberry Pi fit for the Z-Wave world Home Automaton

Z-Wave components, a RaZberry module, and the free Home Assistant software make the Raspberry Pi a powerful smart home control center

time-read
7 mins  |
MakerSpace #03
Water your plants with a Raspberry Pi Watering Pi
MakerSpace

Water your plants with a Raspberry Pi Watering Pi

With a Pi Zero and a few components, you can build an inexpensive and reliable automatic watering system for your plants in next to no time

time-read
7 mins  |
MakerSpace #03
Raspberry Pi automated fish feeder Fish Food
MakerSpace

Raspberry Pi automated fish feeder Fish Food

Whether at work or on vacation, every pet lover worries about how to take care of their little roommates in their absence. What aquarium owners need is an automatic feeder

time-read
7 mins  |
MakerSpace #03
Integrate hardware components with pluggable systems Plugged In
MakerSpace

Integrate hardware components with pluggable systems Plugged In

Ecosystems with pluggable Raspberry Pi modules, sensors, and displays are a great choice if you don’t want to – or can’t – solder but still want to extend your hardware

time-read
6 mins  |
MakerSpace #03
Change internal logic from relays to an Arduino Redo
MakerSpace

Change internal logic from relays to an Arduino Redo

An electronic project at a local science center was showing its age, calling for a refresh: in this case, rebuilding it almost from scratch with an Arduino instead of relays

time-read
9 mins  |
MakerSpace #03
Get creative with the FLUX beamo laser and open source software Beam Me Up, Fluxy!
MakerSpace

Get creative with the FLUX beamo laser and open source software Beam Me Up, Fluxy!

With the FLUX beamo laser and a Raspberry Pi Board B10001, you can execute your own laser cutting projects on a wide range of materials

time-read
10+ mins  |
MakerSpace #03
Power your Pi Pico with solar Self-Sufficient
MakerSpace

Power your Pi Pico with solar Self-Sufficient

Powering your Pi Pico with solar may not solve the current energy crisis, but it will save you from changing or recharging batteries

time-read
4 mins  |
MakerSpace #03