{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Shell-Befehle in IPython\n", "\n", "IPython Notebooks ermöglichen die Ausführung einfacher UNIX/Linux-Befehle in einer Eingabezelle. Es gibt keine Einschränkungen, aber denkt bitte daran, dass ihr im Gegensatz zu einer normalen UNIX/Linux-Shell jeden Shell-Befehl `!` voranstellen müsst, zum Beispiel `!ls`für den Befehl `ls` (weitere Erläuterungen zum Befehl folgen weiter unten). Außerdem wird jeder Shell-Befehl in einer eigenen Subshell ausgeführt. Aus diesem Grund stehen Euch die Ergebnisse früherer Shell-Befehle nicht zur Verfügung." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Zunächst listet der Befehl `ls` die Dateien im aktuellen Arbeitsverzeichnis auf. Die Ausgabe wird unter der Eingabezelle angezeigt und listet eine einzelne Datei` shell.ipynb` auf:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "debugging.ipynb \u001b[34mmypackage\u001b[m\u001b[m\n", "debugging.ipynb.license myscript.py\n", "display.ipynb shell.ipynb\n", "display.ipynb.license shell.ipynb.license\n", "examples.ipynb start.rst\n", "examples.ipynb.license \u001b[31mtab-completion-for-anything.png\u001b[m\u001b[m\n", "extensions.rst tab-completion-for-anything.png.license\n", "importing.ipynb \u001b[31mtab-completion-for-modules.png\u001b[m\u001b[m\n", "importing.ipynb.license tab-completion-for-modules.png.license\n", "index.rst \u001b[31mtab-completion-for-objects.png\u001b[m\u001b[m\n", "magics.ipynb tab-completion-for-objects.png.license\n", "magics.ipynb.license \u001b[34munix-shell\u001b[m\u001b[m\n" ] } ], "source": [ "!ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Der Befehl `!pwd` zeigt den Pfad zum Arbeitsverzeichnis (engl: **p**ath to the **w**orking **d**irectory) an:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience-de/docs/workspace/ipython\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Der Befehl `!echo` gibt Text aus, der dem `echo`-Befehl als Parameter übergeben wurde. Das folgende Beispiel zeigt, wie ihr *Hello world!* ausgeben könnt:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "!echo \"Hello world!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Werte an und von der Shell übergeben\n", "\n", "Es gibt eine clevere Möglichkeit, auf die Ausgabe eines UNIX/Linux-Befehls als Variable in Python zuzugreifen, z.B. mit:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "contents = !ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hier wurde der Python-Variable `contents` die Ausgabe des Befehls `ls` zugewiesen. Als Ergebnis von `contents` entsteht eine Liste, wobei jedes Listenelement einer Zeile in der Ausgabe entspricht. Mit dem `print`-Befehl gebt ihr den Listeninhalt aus:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['debugging.ipynb', 'debugging.ipynb.license', 'display.ipynb', 'display.ipynb.license', 'examples.ipynb', 'examples.ipynb.license', 'extensions.rst', 'importing.ipynb', 'importing.ipynb.license', 'index.rst', 'magics.ipynb', 'magics.ipynb.license', '\\x1b[34mmypackage\\x1b[m\\x1b[m', 'myscript.py', 'shell.ipynb', 'shell.ipynb.license', 'start.rst', '\\x1b[31mtab-completion-for-anything.png\\x1b[m\\x1b[m', 'tab-completion-for-anything.png.license', '\\x1b[31mtab-completion-for-modules.png\\x1b[m\\x1b[m', 'tab-completion-for-modules.png.license', '\\x1b[31mtab-completion-for-objects.png\\x1b[m\\x1b[m', 'tab-completion-for-objects.png.license', '\\x1b[34munix-shell\\x1b[m\\x1b[m']\n" ] } ], "source": [ "print(contents)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Das gleiche Ergebnis seht ihr unten, wenn ihr den Befehl `pwd` ausführt:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "directory = !pwd" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['/Users/veit/cusy/trn/Python4DataScience-de/docs/workspace/ipython']\n" ] } ], "source": [ "print(directory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Shell-bezogene Magic-Befehle" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience-de/docs/workspace/ipython\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "!cd .." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience-de/docs/workspace/ipython\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience-de/docs/workspace\n" ] } ], "source": [ "%cd .." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Neben `%cd` gibt es noch folgende andere Shell-bezogene Magic-Befehle:\n", "`%cat`, `%cp`, `%env`, `%ls`, `%man`, `%mkdir`, `%more`, `%mv`, `%pwd`, `%rm` und `%rmdir`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `%automagic`-Funktion\n", "\n", "Mit der `%automagic`-Funktion lassen sich diese auch ohne vorangestelltes `%`-Zeichen verwenden:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Automagic is ON, % prefix IS NOT needed for line magics.\n" ] } ], "source": [ "%automagic" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience-de/docs\n" ] } ], "source": [ "cd .." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }