To add a Python script as a shortcut in the Bash

To add a Python script as a shortcut in the Bash

Linux Bash 5.2.32(1): I have created a Python (version 3.13.7) script in order to shut down my pc. I don’t want to click on the graphic menu in the Gnome (47.9 my version).

This is the script in order to shut down the pc:

import subprocess
import time
import os

t = time.localtime()
Zeit = time.strftime("%H:%M:%S", t)
print("Es ist jetzt", Zeit)

cache_pfad = "/home/sven/.cache/pip"
paket_dateien_gefunden = False

for dirpath, dirnames, filenames in os.walk(cache_pfad):
    for filename in filenames:
        if filename.endswith((".whl",".tar.gz",".zip")):
            paket_dateien_gefunden = True
            break
        if paket_dateien_gefunden:
            break
    if paket_dateien_gefunden:
        print("Paketdateien im Cache gefunden. Lösche Cache")
        subprocess.run(["pip","cache","purge"])
        print("Cache erfolgreich gelöscht.")
else:
    print("Keine Paketdateien gefunden.")

subprocess.run(["shutdown","now"])

The script checked first the current time and second whether pip has something in the cache, when I had updated some packages.

I have saved into many subfolders in my file system

with the command

I had to start it. But if you have a busy day on the command line, then this command is far away. You can search with the history command with:

then you can execute it with the current number on the left side. But it could be much easier when you have only command for this. On Linux, you can short long commands with a few words. This is the alias command.

I now created a shortcut for this long command

with

Now it works for one use.

But if you want to use it forever then you have to add this into the last row of you

file. The file .bashrc is in the home directory in my case. /home/sven

Open your .bashrc file with an editor what you like. I have opened with the vi editor

type i in order to add something into the file into the last row

type esc in order to leave the edit mode in vi

type :wq! in order to save it and quit the vi editor.

The last thing is to update you .bashrc file with the command:

It should save it now and when you type now hs then the long command

will be executed.

Die Kommentare sind geschlossen.