<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zInformatik &#187; Konsole</title>
	<atom:link href="http://zinformatik.de/tag/konsole/feed/" rel="self" type="application/rss+xml" />
	<link>http://zinformatik.de</link>
	<description>Computer, Informatik und Mikrocontroller Blog</description>
	<lastBuildDate>Fri, 23 Jul 2010 21:22:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Die Z-Shell (zsh) &#8211; Eine mächtige Alternative zur Bash</title>
		<link>http://zinformatik.de/tipps-tricks/interessante-programme/die-z-shell-zsh-eine-machtige-alternative-zur-bash/</link>
		<comments>http://zinformatik.de/tipps-tricks/interessante-programme/die-z-shell-zsh-eine-machtige-alternative-zur-bash/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 15:44:52 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Interessante Programme]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Konsole]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[zsh]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=1668</guid>
		<description><![CDATA[Die Standardshell auf den meisten Linux-Systemen ist die Bash. Sie ist schon sehr mächtig und erlaubt es viele Aufgaben zu automatisieren. Dieser Artikel soll sich einer sehr interessanten weil noch mächtigeren Shell widmen, der Z-Shell kurz zsh.
]]></description>
			<content:encoded><![CDATA[<p>Die Standardshell auf den meisten Linux-Systemen ist die Bash. Sie ist schon sehr mächtig und erlaubt es viele Aufgaben zu automatisieren. Dieser Artikel soll sich einer sehr interessanten weil noch mächtigeren Shell widmen, der Z-Shell kurz zsh.</p>
<p>Die zsh lässt sich auf quasi allen Linuxsystemen über die Paketverwaltung installieren. Unter Ubuntu z.B. mit dem Befehl</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #c20cb9; font-weight: bold;">zsh</span></pre></div></div>

<p>Danach kann man sie mit dem Aufruf von <code>zsh</code> ausführen. Um sie zur Standardshell zu machen genügt der Befehl</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chsh</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">zsh</span> username</pre></div></div>

<p>Wobei <code>username</code> durch den entsprechenden Usernamen ersetzt werden muss und der Pfad zu <code>zsh</code> stimmen muss. Auf manchen Systemen liegt <code>zsh</code> in <code>/bin</code>. Wo <code>zsh</code> liegt kann man mit dem folgenden Befehl heraus finden:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">zsh</span></pre></div></div>

<p>Die zsh bedient sich Elementen der Bash, der Korn-Shell (ksh) und der TENEX-C-Shell (tcsh &#8211; eine erweiterte C-Shell). Sie ist sehr gut konfigurierbar und fast jedes Verhalten der Bash kann nachgeahmt werden so dass der Umstieg sehr leicht fällt. Bestehende Bash-Scripte können natürlich weiterhin genutzt werden, wenn der Shebang entsprechend gesetzt ist. Also in der ersten Zeile folgendes steht:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span></pre></div></div>

<p>Im Folgenden werde ich einige interessante Vorteile der zsh kurz beschreiben. Zu einzelnen Themen werde wahrscheinlich in anderen Artikel ausführlicher eingehen. Dies sollen also nur Beispiele sein, die die Mächtigkeit demonstrieren sollen. Da die zsh so viele Möglichkeiten bietet ist es nicht möglich hier alles im Detail zu erläutern. Optionen werden mit dem Befehl <code>setopt</code> gesetzt. Dies kann man auch so in die Konfigurationsdatei <code>~/.zshrc</code> schreiben.</p>
<p><strong>Autokorrektur</strong><br />
Die zsh beherrscht einen Mechanismus, der kleine Tippfehler automatisch korrigiert. Hat man in einem Ordner eine Datei <code>testfile.txt</code> und möchte diese kopieren nach <code>file2.txt</code>, so führt man in der Regel folgenden Befehl aus:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> testfile.txt file2.txt</pre></div></div>

<p>wie von der Bash gewohnt nutzt man dafür die Autovervollständigung mittels TAB. Bei einem Verschreiber wird dieser in der zsh automatisch korrigiert. Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> tsetf<span style="color: #000000; font-weight: bold;">&lt;</span>TAB<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>wird automatisch korrigiert und zu</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> testfile.txt</pre></div></div>

<p>vervollständigt.</p>
<p>Die Autokorrektur aktiviert man mit der Option <code>correct</code></p>
<p><strong>Globale Aliase</strong><br />
Neben den normalen Aliasen für Kommandos, wie man sie aus der Bash kennt, gibt es in der zsh noch globale Aliase, die überall im Befehl genutzt werden können, nicht nur am Anfang. Zwei Beispiele verdeutlichen dies:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #660033;">-g</span> <span style="color: #007800;">G</span>=<span style="color: #ff0000;">'| grep'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #660033;">-g</span> <span style="color: #007800;">L</span>=<span style="color: #ff0000;">'| less'</span></pre></div></div>

<p>erzeugt die globalen Aliase G und L die man hinter alle möglichen Befehle schreiben kann um die Ausgabe des entsprechenden Befehls in grep oder less zu pipen. Z.B.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> G txt</pre></div></div>

<p>entspricht dem Befehl</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> txt</pre></div></div>

<p>oder</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-e</span> L</pre></div></div>

<p>entspricht</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-e</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">less</span></pre></div></div>

<p>Natürlich ist auch folgendes Möglich:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> G txt L</pre></div></div>

<p>was folgendem Befehl entspricht:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> txt <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">less</span></pre></div></div>

<p><strong>Suffix Aliase</strong><br />
Mit Suffixaliasen kann man Programme festlegen, mit denen bestimmte Dateitypen (anhand ihrer Endung) geöffnet werden sollen. Man braucht dann nur noch den Namen der Datei eingeben und sie wird mit dem entsprechenden Programm geöffnet. Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #660033;">-s</span> <span style="color: #007800;">pdf</span>=evince</pre></div></div>

<p>legt einen Alias für pdf Dateien an. Sie sollen mit evince geöffnet werden. Danach reicht die Eingabe von</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">dokument.pdf</pre></div></div>

<p>und die Datei <code>dokument.pdf</code> wird mit evince geöffnet.</p>
<p><strong>Aliase für Verzeichnisse: Hashes</strong><br />
Mit sogenannten Hashes lassen sich Aliase für beliebige Verzeichnisse anlegen. Auf diese kann dann sehr einfach zugegriffen werden. Mit dem Befehl</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">hash</span> <span style="color: #660033;">-d</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">perl</span></span>=~<span style="color: #000000; font-weight: bold;">/</span>developement<span style="color: #000000; font-weight: bold;">/</span>scripting<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">perl</span></pre></div></div>

<p>legt man einen Hash namens <code>perl</code> für das Verzeichnis <code>~/developement/scripting/perl</code> an. Auf dieses Verzeichnis kann man jetzt mit <code>~perl</code> zugreifen. Auch innerhalb eines Befehls wie folgendes Beispiel zeigt:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> testscript.pl ~<span style="color: #c20cb9; font-weight: bold;">perl</span></pre></div></div>

<p>Dieser Befehl kopiert die Datei <code>testscript.pl</code> in das Verzeichnis <code>~/developement/scripting/perl</code>.</p>
<p><strong>Auto-CD</strong><br />
Mit der Option <code>autocd</code> kann man zum Wechseln in ein Verzeichnis einfach den Namen eines Verzeichnisses eingeben und das Kommando <code>cd</code> davor einfach weglassen. Wenn es keinen Befehl gibt, der so heißt wie das Verzeichnis, so wird in das Verzeichnis gewechselt.</p>
<p><strong>Kurze for-Schleifen</strong><br />
Statt einem</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*</span>.eps; <span style="color: #000000; font-weight: bold;">do</span> epstopdf <span style="color: #007800;">$i</span>; <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>um alle eps-Dateien in einem Verzeichnis in pdf-Dateien umzuwandeln genügt unter zsh die kürzere Form</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>.eps<span style="color: #7a0874; font-weight: bold;">&#41;</span> epstopdf <span style="color: #007800;">$i</span></pre></div></div>

<p><strong>Globale History</strong><br />
Die History, die man mit der Pfeil-auf Taste durchgehen kann, kann man in der zsh so einstellen, dass sie in jeder zsh-Instanz gleich ist. Das heißt man hat nicht mehr für jedes Fenster seine eigene History sondern kann mit der Pfeil-auf Taste auch Befehle raussuchen, die in anderen Fenstern eingegeben wurden. Die History wird jedoch nur nach jedem Befehl geupdated, das heißt man muss ggf. einmal Return drücken, damit die Befehle aus einem anderen Fenster verfügbar sind.</p>
<p>Dieses Verhalten kann mit der Option <code>share_history</code> aktiviert werden.</p>
<p><strong>Directory Stack</strong><br />
Wie Bash, hat die zsh auch einen Directory stack. Mit dem Befehl <code>pushd</code> kann man das aktuelle Verzeichnis auf den Stack legen und mit <code>popd</code> das jeweils letzte Verzeichnis vom Stack nehmen und dorthin springen. Mit der Option <code>auto_pushd</code> legt die zsh automatisch jedes Verzeichnis auf den Directorystack, aus welchem man in ein anderes Verzeichnis wechselt. So hat man immer eine History der Verzeichnisse in denen man war und kann mit <code>popd</code> sehr einfach wieder in Verzeichnisse welchseln, die man vorher besucht hatte.</p>
<p><strong>Globbing</strong><br />
Die Nutzung von Wildcards &#8211; unter zsh globbing genannt &#8211; ist bei der Z-Shell wesentlich mächtiger als in der Bash. Zunächst gibt es rekursives Globbing (gibts in der Bash in neueren Versionen wohl auch). So kann man mit dem Befehl</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">**/*</span>.html</pre></div></div>

<p>alle HTML-Dateien im aktuellen Verzeichnis und allen Unterverzeichnissen auflisten lassen.</p>
<p>Es gibt jedoch auch noch Qualifier um Dateien mit bestimmten Eigenschaften auszuwählen. Diese schreibt man in runde Klammern. So kann man mit <code>.</code> normale Dateien, mit <code>/</code> Verzeichnisse und mit <code>@</code> Symbolische Links matchen. Praktisch ist das z.B. wenn man allen Verzeichnissen ab einem bestimmten Verzeichnis die Rechte 755 und allen Dateien 644 vergeben möchte. Dies kann man mit den folgenden zwei Befehlen bewerkstelligen statt mit <code>find</code> und <code>exec</code> zu arbeiten.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">755</span> <span style="color: #000000; font-weight: bold;">**/*</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">644</span> <span style="color: #000000; font-weight: bold;">**/*</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>.<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Zusätzlich gibt es auch Qualifier für verschiedenste Dateirechte. Folgender Befehl listet alle Dateien auf, die von allen beschrieben werden können:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">**/*</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>.W<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Auch nach der Dateigröße lässt sich suchen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">print <span style="color: #000000; font-weight: bold;">**/*</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>L0<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>listet alle leeren Dateien auf.</p>
<p>Auch die Sortierung lässt sich beliebig anpassen. Mit dem Befehl:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">print <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>oL<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Listet alle Dateien der Größe nach von klein nach groß auf. Mit großem O wird die Sortierung umgekehrt.</p>
<p>Somit lässt sich mittels globbing in der zsh das Kommando <code>find</code> quasi komplett ersetzen. Die hier gezeigten Beispiele sehen auf den ersten Blick recht kompliziert und kryptisch aus. Wenn man das Prinzip aber einmal verstanden hat und die wichtigsten Qualifier kennt ist es aber ganz einfach und sehr praktisch.</p>
<p>Es gibt noch jede Menge weiterer Qualifier und Möglichkeiten für das Globbing (z.B. nur das erste oder eine bestimmte Anzahl von Elementen auszuwählen), die ich hier gar nicht alle erwähnen kann. Im zsh-Manual gibt es jedoch eine Liste der <a href="http://zsh.sourceforge.net/Doc/Release/Expansion.html#SEC87" title="zsh-Manual - Glob-Qualifier" target="_blank" class="liexternal">Glob-Qualifier</a>.</p>
<p>Um alle Möglichkeiten nutzen zu können sollte die Option <code>extended_glob</code> gesetzt werden.</p>
<p><strong>Vervollständigung</strong><br />
Die zsh hat wie die Bash einen Vervollständigungsmechanismus, so dass man mit der TAB-Taste Kommandos, Dateinamen und vieles mehr vervollständigen kann. In der zsh ist dieser Mechanismus jedoch extrem gut konfigurierbar und programierbar. So kann man z.B. beim <code>scp</code> Befehl (der Dateien über ssh kopiert) die <a href="http://blog.pimpmyshell.de/2007/01/21/ssh-completion-mit-zsh/" title="ssh completion mit zsh" target="_blank" class="liexternal">Verzeichnisse auf dem entfernten Rechner vervollständigen</a> und genau definieren welche Programme mit welchen Dateitypen zusammen arbeiten können. Für sehr viele Programme ist dies schon vorgefertigt, so dass ein</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">latex doku<span style="color: #000000; font-weight: bold;">&lt;</span>TAB<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>automatisch zu</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> latex dokument.tex</pre></div></div>

<p>vervollständigt wird. Dies beherrscht die Bash zum Teil auch. Jedoch ist es in der zsh wesentlich besser konfigurierbar und anpassbar.</p>
<p>Sehr praktisch finde ich auch die Menüfunktion. So wird beim ersten Druck auf die TAB-Taste wie in der Bash soweit vervollständigt, bis nicht mehr entschieden werden kann welche Datei (bzw. welcher Befehl) gemeint ist. Ein weiterer Druck auf die TAB-Taste listet alle weiteren Möglichkeiten auf und jeder weitere Druck geht diese Möglichkeiten durch. Wenn ich also drei Dateien habe: <code>test.txt</code>, <code>testfile1.txt</code> und <code>testfile2.txt</code> und gebe folgendes ein:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> te<span style="color: #000000; font-weight: bold;">&lt;</span>TAB<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>so wird zuerst auf <code>test</code> vervollständigt. Ein weiterer Druck gibt mir alle Möglichkeiten aus:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">testfile1.txt  testfile2.txt  test.txt</pre></div></div>

<p>und ein dritter Druck auf die Tab-Taste vervollständigt den Befehl zu</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> testfile1.txt</pre></div></div>

<p>Das ist viel schneller als erst ein <code>f</code> einzugeben um erneut TAB zu drücken und dann die <code>1</code> einzugeben um wiederum TAB zu drücken bis der Dateiname endgültig vervollständigt wird.</p>
<p>Ein weiterer Druck auf TAB nimmt dann die nächste Möglichkeit. In diesem Beispiel also</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> testfile2.txt</pre></div></div>

<p>und so weiter.</p>
<p><strong>Expansion</strong><br />
Mit der TAB-Taste kann man in der zsh nicht nur Kommandos, Dateinamen, &#8230; vervollständigen, sondern auch Variablen expandieren. Viele kennen ja die spezielle Variable <code>!!</code>, die den zuletzt ausgeführten Befehl enthält. In der zsh kann man nun folgendes eingeben:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">!!&lt;</span>TAB<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>und die Variable <code>!!</code> wird automatisch zum zuletzt ausgeführten Befehl expandiert. Genauso kann man auch jede andere Variable expandieren:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$PWD</span><span style="color: #000000; font-weight: bold;">&lt;</span>TAB<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>wird zu <code>/home/zimon/developement</code> (vorrausgesetzt ich befinde mich zum Zeitpunkt der expansion in diesem Verzeichnis).</p>
<p><strong>History Kontrolle</strong><br />
Wie bei der Bash auch, kann man in der zsh die History durchsuchen. Die zsh geht jedoch so weit, dass man auch nach bestimmten Parametern suchen kann. Ein Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> ..<span style="color: #000000; font-weight: bold;">/</span>perl<span style="color: #000000; font-weight: bold;">/</span>foo.pl ~<span style="color: #000000; font-weight: bold;">/</span>developement<span style="color: #000000; font-weight: bold;">/</span>projektordner</pre></div></div>

<p>Danach kommen ein paar andere Kommandos. Möchte nun eine andere Datei in den gleichen Projektordner kopieren reicht folgende Eingabe:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> ..<span style="color: #000000; font-weight: bold;">/</span>perl<span style="color: #000000; font-weight: bold;">/</span>bar.pl <span style="color: #000000; font-weight: bold;">!</span>?proj?:<span style="color: #000000;">3</span></pre></div></div>

<p>Mit !? wird nach einem vorherigen Befehl gesucht. Das <code>proj</code> ist ein String der in diesem Befehl vorkam. Mit dem <code>?:3</code> wird das dritte Argument genutzt. Durch die Expansion kann manmit durch einen Druck auf TAB diesen Ausdruck auch noch expandieren um sicher zu gehen, dass das richtige Argument ausgewählt wurde. Man kann zwar mit Alt+. wie in der Bash die jeweils letzten Argumente durchgehen, auf diese Weise kann man aber auch das zweite von drei Argumenten suchen.</p>
<p>Es gibt noch wesentlich mehr Möglichkeiten die History zu durchsuchen und zu verwenden.</p>
<p><strong>Hochkonfigurierbarer Prompt</strong><br />
In der Z-Shell kann man nicht nur die linke Seite des Prompts sondern auch die rechte Seite konfigurieren. Als Beispiel hier mal mein aktueller Prompt:</p>
<div id="attachment_1679" class="wp-caption aligncenter" style="width: 643px"><a href="http://zinformatik.de/wp-content/uploads/2010/01/zsh_prompt.png" ><img class="size-full wp-image-1679" title="zsh Prompt" src="http://zinformatik.de/wp-content/uploads/2010/01/zsh_prompt.png" alt="zsh Prompt" width="633" height="37" /></a><p class="wp-caption-text">Mein aktueller zsh-Prompt</p></div>
<p>Ich finde die Anzeige der Uhrzeit inklusive Sekunden recht praktisch, da man daran sieht wie lange ein Befehl gedauert hat oder wann man ihn ausgeführt hat. Es gibt jedoch auch die Möglichkeit automatisch die Ausgabe von <code>time</code> bei der Beendigung eines Befehls ausgeben zu lassen, der länger als eine vorher definierte Zeit benötigt hat.</p>
<p>Der Prompt ist eine abgeänderte Version von <a href="http://aperiodic.net/phil/prompt/" title="Phil!s ZSH Prompt" target="_blank" class="liexternal">Phil!s ZSH Prompt</a>.</p>
<p><strong>[UPDATE]</strong> Auf Anfrage hin ist hier noch der Code für die von mir abgeänderte Version des Prompts:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">setprompt <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #666666; font-style: italic;">###</span>
    <span style="color: #666666; font-style: italic;"># Need this so the prompt will work.</span>
&nbsp;
    setopt prompt_subst
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">###</span>
    <span style="color: #666666; font-style: italic;"># See if we can use colors.</span>
&nbsp;
    autoload colors zsh<span style="color: #000000; font-weight: bold;">/</span>terminfo
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$terminfo</span>[colors]&quot;</span> <span style="color: #660033;">-ge</span> <span style="color: #000000;">8</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	colors
    <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">for</span> color <span style="color: #000000; font-weight: bold;">in</span> RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; <span style="color: #000000; font-weight: bold;">do</span>
	<span style="color: #7a0874; font-weight: bold;">eval</span> PR_<span style="color: #007800;">$color</span>=<span style="color: #ff0000;">'%{$terminfo[bold]$fg[${(L)color}]%}'</span>
	<span style="color: #7a0874; font-weight: bold;">eval</span> PR_LIGHT_<span style="color: #007800;">$color</span>=<span style="color: #ff0000;">'%{$fg[${(L)color}]%}'</span>
	<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span> count = <span style="color: #007800;">$count</span> + <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">done</span>
    <span style="color: #007800;">PR_NO_COLOUR</span>=<span style="color: #ff0000;">&quot;%{<span style="color: #007800;">$terminfo</span>[sgr0]%}&quot;</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">###</span>
    <span style="color: #666666; font-style: italic;"># See if we can use extended characters to look nicer.</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">typeset</span> <span style="color: #660033;">-A</span> altchar
    <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-A</span> altchar <span style="color: #800000;">${(s..)terminfo[acsc]}</span>
    <span style="color: #007800;">PR_SET_CHARSET</span>=<span style="color: #ff0000;">&quot;%{<span style="color: #007800;">$terminfo</span>[enacs]%}&quot;</span>
    <span style="color: #007800;">PR_SHIFT_IN</span>=<span style="color: #ff0000;">&quot;%{<span style="color: #007800;">$terminfo</span>[smacs]%}&quot;</span>
    <span style="color: #007800;">PR_SHIFT_OUT</span>=<span style="color: #ff0000;">&quot;%{<span style="color: #007800;">$terminfo</span>[rmacs]%}&quot;</span>
    <span style="color: #007800;">PR_HBAR</span>=<span style="color: #800000;">${altchar[q]:--}</span>
    <span style="color: #007800;">PR_ULCORNER</span>=<span style="color: #800000;">${altchar[l]:--}</span>
    <span style="color: #007800;">PR_LLCORNER</span>=<span style="color: #800000;">${altchar[m]:--}</span>
    <span style="color: #007800;">PR_LRCORNER</span>=<span style="color: #800000;">${altchar[j]:--}</span>
    <span style="color: #007800;">PR_URCORNER</span>=<span style="color: #800000;">${altchar[k]:--}</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$TERM</span>&quot;</span> == <span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #007800;">PR_HBAR</span>=-
     	<span style="color: #007800;">PR_ULCORNER</span>=--
    	<span style="color: #007800;">PR_LLCORNER</span>=--
    	<span style="color: #007800;">PR_LRCORNER</span>=--
    	<span style="color: #007800;">PR_URCORNER</span>=-
   <span style="color: #000000; font-weight: bold;">fi</span> 
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">###</span>
    <span style="color: #666666; font-style: italic;"># Decide if we need to set titlebar text.</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$TERM</span> <span style="color: #000000; font-weight: bold;">in</span>
	xterm<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
	    <span style="color: #007800;">PR_TITLEBAR</span>=$<span style="color: #ff0000;">'%{\e]0;%(!.-=*[ROOT]*=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\a%}'</span>
	    <span style="color: #000000; font-weight: bold;">;;</span>
	<span style="color: #c20cb9; font-weight: bold;">screen</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
	    <span style="color: #007800;">PR_TITLEBAR</span>=$<span style="color: #ff0000;">'%{\e_screen \005 (\005t) | %(!.-=[ROOT]=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\e\\%}'</span>
	    <span style="color: #000000; font-weight: bold;">;;</span>
	<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
	    <span style="color: #007800;">PR_TITLEBAR</span>=<span style="color: #ff0000;">''</span>
	    <span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000; font-weight: bold;">esac</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">###</span>
    <span style="color: #666666; font-style: italic;"># Decide whether to set a screen title</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$TERM</span>&quot;</span> == <span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #007800;">PR_STITLE</span>=$<span style="color: #ff0000;">'%{\ekzsh\e\\%}'</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #007800;">PR_STITLE</span>=<span style="color: #ff0000;">''</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">###</span>
    <span style="color: #666666; font-style: italic;"># Finally, the prompt.</span>
&nbsp;
    <span style="color: #007800;">PROMPT</span>=<span style="color: #ff0000;">'$PR_SET_CHARSET$PR_STITLE${(e)PR_TITLEBAR}\
$PR_RED$PR_SHIFT_IN$PR_ULCORNER$PR_BLUE$PR_HBAR$PR_SHIFT_OUT(\
$PR_GREEN%(!.%SROOT%s.%n)$PR_GREEN@%m:%l\
$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_RED$PR_HBAR${(e)PR_FILLBAR}$PR_BLUE$PR_HBAR$PR_SHIFT_OUT(\
$PR_MAGENTA%$PR_PWDLEN&lt;...&lt;%~%&lt;&lt;\
$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_RED$PR_URCORNER$PR_SHIFT_OUT\
&nbsp;
$PR_RED$PR_SHIFT_IN$PR_LLCORNER$PR_BLUE$PR_HBAR$PR_SHIFT_OUT(\
%(?..$PR_LIGHT_RED%?$PR_BLUE:)\
${(e)PR_APM}$PR_YELLOW%D{%H:%M:%S}\
$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_RED$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_NO_COLOUR '</span>
&nbsp;
    <span style="color: #007800;">RPROMPT</span>=<span style="color: #ff0000;">' $PR_RED$PR_SHIFT_IN$PR_HBAR$PR_BLUE$PR_HBAR$PR_SHIFT_OUT\
($PR_YELLOW%D{%a,%b%d}$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_RED$PR_LRCORNER$PR_SHIFT_OUT$PR_NO_COLOUR'</span>
&nbsp;
    <span style="color: #007800;">PS2</span>=<span style="color: #ff0000;">'$PR_RED$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_BLUE$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT(\
$PR_LIGHT_GREEN%_$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_RED$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT$PR_NO_COLOUR '</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
setprompt</pre></div></div>

<p><strong>[/UPDATE]</strong></p>
<p><strong>ZLE-Widgets</strong><br />
Der Z-Line-Editor (ZLE) ist quasi die Eingabezeile in der zsh. Dafür kann man sich kleine Programme schreiben, die einem das Leben vereinfachen. Ein Beispiel wäre ein Programm, dass bei der Eingabe von <code>...</code> diese zu <code>../..</code> umwandelt. Jeder weitere Punkt wird wieder umgewandelt. So kann man recht einfach folgendes eingeben:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ....<span style="color: #000000; font-weight: bold;">/</span>directory</pre></div></div>

<p>Auf dem Bildschirm erscheint jedoch folgendes:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>directory</pre></div></div>

<p>Dieses Widget ist kein Standard bei der Z-Shell. Man kann es aber durch folgenden Code in der Konfigurationsdatei <code>~/.zshrc</code> erzeugen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rationalise-dot<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$LBUFFER</span> = <span style="color: #000000; font-weight: bold;">*</span>.. <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        LBUFFER+=<span style="color: #000000; font-weight: bold;">/</span>..
    <span style="color: #000000; font-weight: bold;">else</span>
        LBUFFER+=.
            <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
zle <span style="color: #660033;">-N</span> rationalise-dot
bindkey . rationalise-dot</pre></div></div>

<p><strong>Automatisches tee-ing</strong><br />
In der Z-Shell kann man die Ausgabe eines Kommandos recht einfach in mehrere verschiedene Dateien umleiten. Statt wie in der Bash</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #660033;">-a</span> all <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> actual</pre></div></div>

<p>zu schreiben, kann man in der zsh einfach</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span>all <span style="color: #000000; font-weight: bold;">&gt;</span>actual</pre></div></div>

<p>eingeben. Beide Befehle führen ls aus und hängen die Ausgabe an die Datei <code>all</code> an und schreiben sie in die Datei <code>actual</code> wobei der inhalt jeweils überschrieben wird.<br />
Möchte man die Ausgabe wie beim original <code>tee</code> Befehl auch auf dem Bildschirm sehen, so leitet man sie auch noch auf STDOUT um:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span>all <span style="color: #000000; font-weight: bold;">&gt;</span>actual <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span></pre></div></div>

<p><strong>Verzeichnisse durch Ersetzung wechseln</strong><br />
Ein Beispiel sollte dies am besten Erklären. Man nehme an man hat zwei Verzeichnisse: <code>~/developement/scripting/perl/modules</code> und <code>~/developement/scripting/python/modules</code> und man befindet sich in <code>~/developement/scripting/perl/modules</code>, so kann man durch den Befehl</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> python</pre></div></div>

<p>in das Verzeichnis <code>~/developement/scripting/python/modules</code> wechseln. Bei einem <code>cd</code> Kommando mit zwei Parametern wird im aktuellen Verzeichnis-String das erste Wort durch das zweite ersetzt. Hier also <code>perl</code> durch <code>python</code>.</p>
<p><strong>Fazit</strong><br />
Ich bin jetzt nicht 100% sicher, dass alles hier beschriebene nicht auch in der Bash möglich ist. Es zeigt aber auf jeden Fall, dass die Z-Shell jede Menge interessante Möglichkeiten bietet und man viele Dinge einfacher und schneller erledigen kann als in der Bash. Die oben gezeigten Features sind meist nur angerissen und nur eine kleine Auswahl von dem was die Z-Shell bietet. Darüber hinaus besitzt die Z-Shell eine ganze Reihe Module z.B. für Matheoperationen oder einen eigenen FTP-Client, die man bei Bedarf laden kann.</p>
<p>Sehr schön ist auch die Möglichkeit den <a href="http://zinformatik.de/linux/titel-von-screen-fenstern-automatisch-von-vim-bash-und-zsh-setzen/" title="Titel von Screen Fenstern automatisch von vim, Bash und zsh setzen" target="_self" class="liinternal">Screen-Title von der zsh aus zu setzen</a>.</p>
<p>Es lohnt sich auf jeden Fall, diese Shell etwas genauer zu betrachten. Bei mir hat sie mittlerweile die Bash fast vollständig ersetzt (vorhandene Scripte habe ich nicht umgeschrieben).</p>
<p><strong>Links</strong></p>
<ul>
<li><a href="http://zsh.sourceforge.net/" title="Offizielle zsh Projektseite" target="_blank" class="liexternal">Offizielle zsh Projektseite</a> (englisch)</li>
<li><a href="http://zsh.sourceforge.net/Doc/Release/zsh_toc.html" title="zsh Manual" target="_blank" class="liexternal">zsh Manual</a> (englisch)</li>
<li><a href="http://zsh.sourceforge.net/Guide/zshguide.html" title="zsh Users Guide" target="_blank" class="liexternal">zsh User&#8217;s Guide</a> (englisch)</li>
<li><a href="http://michael-prokop.at/computer/tools_zsh.html" title="zsh generell" target="_blank" class="liexternal">Michael Prokops zsh Seite</a> &#8211; Seite mit Features und Beispielen wo zsh mit Bash verglichen wird</li>
<li><a href="http://michael-prokop.at/computer/tools_zsh_liebhaber.html" title="zsh liebhaber" target="_blank" class="liexternal">Michael Prokops zsh Liebhaberseite</a> &#8211; Viele gute Beispiele und zsh Tricks angelehnt an das zsh-lovers Projekt.</li>
<li><a href="http://zshwiki.org" title="zsh wiki" target="_blank" class="liexternal">zsh wiki</a> &#8211; Wiki über zsh (englisch)</li>
<li><a href="http://grml.org/zsh/zsh-lovers.html" title="zsh-lovers" target="_blank" class="liexternal">zsh-lovers</a> &#8211; Sammlung von Tipps, Tricks und Beispielen (englisch)</li>
<li><a href="http://grml.org/zsh/" target="_blank" class="liexternal">grml zsh Seite</a> &#8211; Seite mit sehr großer zsh-Konfigurationsdatei, Referenzkarte und anderen zsh Infos. Die grml-Linuxdistribution hat zsh als Standardshell. (englisch)</li>
<li><a href="http://www.strcat.de/zsh/" title="stcats zsh Page" target="_blank" class="liexternal">strcat.de/zsh/</a> &#8211; Seite mit Auflistung vieler Features und Tipps (englisch)</li>
<li><a href="http://rayninfo.co.uk/tips/zshtips.html" title="Best of zsh Tips" target="_blank" class="liexternal">Zzappers Best of ZSH Tips</a> &#8211; Viele zsh Tipps (englisch)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/tipps-tricks/interessante-programme/die-z-shell-zsh-eine-machtige-alternative-zur-bash/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Erinnerungen mit at und zenity</title>
		<link>http://zinformatik.de/linux/erinnerungen-mit-at-und-zenity/</link>
		<comments>http://zinformatik.de/linux/erinnerungen-mit-at-und-zenity/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 17:59:53 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tipps und Tricks]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[at]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Erinnerung]]></category>
		<category><![CDATA[Konsole]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[zenity]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=1619</guid>
		<description><![CDATA[<p>Wenn man am PC sitzt vergisst man schnell die Zeit. Wenn man noch etwas erledigen muss wäre es daher vorteilhaft, wenn man daran erinnert werden würde. </p>
<p>Ein solches &#8220;Erinnerungsprogramm&#8221; kann man sich recht leicht selber basteln. Ich habe dafür  das Linuxtool <code>at</code> mit dem Programm <a href="http://wiki.ubuntuusers.de/Zenity" target="_blank" title="zenity - Darstellung von Dialogen" class="liuu"><code>zenity</code></a>, welches Dialoge darstellen kann, kombiniert.&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Wenn man am PC sitzt vergisst man schnell die Zeit. Wenn man noch etwas erledigen muss wäre es daher vorteilhaft, wenn man daran erinnert werden würde. </p>
<p>Ein solches &#8220;Erinnerungsprogramm&#8221; kann man sich recht leicht selber basteln. Ich habe dafür  das Linuxtool <code>at</code> mit dem Programm <a href="http://wiki.ubuntuusers.de/Zenity" target="_blank" title="zenity - Darstellung von Dialogen" class="liuu"><code>zenity</code></a>, welches Dialoge darstellen kann, kombiniert.</p>
<p><code>at</code> (bzw. der at-Daemon <code>atd</code>) ist auf den meisten Linuxdistributionen bereits vorhanden, kann sonst aber auch über die Paketverwaltung installiert werden. Zenity sollte bei den meisten Distributionen ebenfalls über die Paketverwaltung installierbar sein.<br />
Unter Ubuntu installiert man <code>zenity</code> mit dem Befehl:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> zenity</pre></div></div>

<p>Mit <code>at</code> können Befehle zu einem festgelegten Zeitpunkt ausgeführt werden. Eine sehr schöne Einführung gibt es in der <a href="http://www.freiesmagazin.de/mobil/freiesMagazin-2008-10-bilder.html#08_10_at" target="_blank" title="freiesMagazin 10/2008 - at" class="liexternal">Ausgabe 10/2008 von freiesMagazin</a> (eine Fortsetzung ist in der <a href="http://www.freiesmagazin.de/mobil/freiesMagazin-2008-11-bilder.html#08_11_at-beispiele" target="_blank" class="liexternal">nachfolgenden Ausgabe</a> enthalten, welche mich auch zu diesem &#8220;Programm&#8221; inspirierte, weil ich mit den dort vorgestellten Erinnerungs-Lösungen nicht zufrieden war).</p>
<p>Um mit <code>zenity</code> einen Dialog anzuzeigen kann man folgenden Befehl nutzen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">zenity <span style="color: #660033;">--info</span> <span style="color: #660033;">--title</span>=<span style="color: #ff0000;">&quot;Titel des Dialogs&quot;</span> <span style="color: #660033;">--text</span> <span style="color: #ff0000;">&quot;Text, der angezeigt werden soll&quot;</span></pre></div></div>

<p>Damit dies von <code>at</code> angezeigt werden kann muss noch das Display angegeben werden:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">zenity <span style="color: #660033;">--info</span> <span style="color: #660033;">--title</span>=<span style="color: #ff0000;">&quot;Titel des Dialogs&quot;</span> <span style="color: #660033;">--text</span> <span style="color: #ff0000;">&quot;Text, der angezeigt werden soll&quot;</span> <span style="color: #660033;">--display</span>=:<span style="color: #000000;">0</span></pre></div></div>

<p>Diesen Befehl kann man nun in ein Bash- oder Perl-Script einfügen, wobei der Titel fest sein kann und der Text als Argument übergeben wird. Ich habe mich für Perl entschieden:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000066;">system</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;zenity --info --title=<span style="color: #000099; font-weight: bold;">\&quot;</span>Erinnerung<span style="color: #000099; font-weight: bold;">\&quot;</span> --text <span style="color: #000099; font-weight: bold;">\&quot;</span>@ARGV<span style="color: #000099; font-weight: bold;">\&quot;</span> --display=:0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Dieses Script habe ich unter dem Namen <code>r</code> (für &#8220;remember&#8221;) im bin-Ordner meines Homeverzeichnisses gespeichert. Dieses muss natürlich im Pfad liegen, was durch folgende Zeile in der Datei <code>~/.bashrc</code> erreicht werden kann:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #007800;">$PATH</span>:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>bin</pre></div></div>

<p>&#8220;user&#8221; muss natürlich durch den entsprechenden Usernamen ersetzt werden. Nachdem man die <code>~/.bashrc</code> mit dem Befehl</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. ~<span style="color: #000000; font-weight: bold;">/</span>.bashrc</pre></div></div>

<p>neu &#8220;geladen&#8221; hat kann man sich nun an Termine oder andere Dinge erinnern lassen.</p>
<p><strong>Beispiele:</strong><br />
Um 14:30 Termin bei FooBar. Also um 14:15 erinnern:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" style="font-family:monospace;">at 14:15
r Termin bei FooBar in 15 Minuten
Strg+d</pre></div></div>

<p>In einer halben Stunde ist eine Besprechung (5 min vorher erinnern):</p>

<div class="wp_syntax"><div class="code"><pre class="plain" style="font-family:monospace;">at now +25min
r Besprechung
Strg+d</pre></div></div>

<p>Es öffnet sich zur angegebenen Zeit ein Dialogfenster (egal, was man gerade macht &#8211; solange man sich in X befindet. Bei Spielen im Fullscreen habe ich es jedoch nicht getestet.), welches den Titel &#8220;Erinnerung&#8221; enthält und den angegebenen Text ausgibt. Das Fenster kann durch einen Klick auf &#8220;OK&#8221; wieder geschlossen werden.</p>
<p><code>at</code> bietet noch viele weitere Möglichkeiten eine Zeit anzugeben. Diese werden im (ersten) oben verlinkten freiesMagazin-Artikel beschrieben.</p>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/erinnerungen-mit-at-und-zenity/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Titel von Screen-Fenstern automatisch von Vim, Bash und zsh setzen</title>
		<link>http://zinformatik.de/linux/titel-von-screen-fenstern-automatisch-von-vim-bash-und-zsh-setzen/</link>
		<comments>http://zinformatik.de/linux/titel-von-screen-fenstern-automatisch-von-vim-bash-und-zsh-setzen/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 09:34:51 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[vim Tipps]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Konsole]]></category>
		<category><![CDATA[Screen]]></category>
		<category><![CDATA[Titel]]></category>
		<category><![CDATA[Vim]]></category>
		<category><![CDATA[zsh]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=1605</guid>
		<description><![CDATA[<p>Wenn man unter <a href="http://zinformatik.de/tipps-tricks/interessante-programme/einfuhrung-in-screen/" title="Einführung in Screen" target="_blank" class="liinternal">Screen</a> die Statusleiste nutzt, ist es oft mühsam jedes mal die Titel zu setzen. Auch wenn man diese fest in der <code>~/.screenrc</code> definiert, kann man manchmal durcheinander kommen.</p>
<p>Unter Vim gibt es die Möglichkeit, den Namen der aktuellen Datei als Titel für das Screenfenster zu benutzen.<br />
Dafür braucht man nur&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Wenn man unter <a href="http://zinformatik.de/tipps-tricks/interessante-programme/einfuhrung-in-screen/" title="Einführung in Screen" target="_blank" class="liinternal">Screen</a> die Statusleiste nutzt, ist es oft mühsam jedes mal die Titel zu setzen. Auch wenn man diese fest in der <code>~/.screenrc</code> definiert, kann man manchmal durcheinander kommen.</p>
<p>Unter Vim gibt es die Möglichkeit, den Namen der aktuellen Datei als Titel für das Screenfenster zu benutzen.<br />
Dafür braucht man nur die folgenden Zeilen zur <code>~/.vimrc</code> hinzufügen:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">if</span> <span style="color: #25BB4D;">expand</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'$TERM'</span><span style="color: #000000;">&#41;</span> == <span style="color: #C5A22D;">'screen'</span>
augroup vim_screen
  <span style="color: #668080;">autocmd</span><span style="color: #000000;">!</span>
  <span style="color: #668080;">autocmd</span> VimEnter,BufWinEnter,WinEnter <span style="color: #000000;">*</span>
    \ <span style="color: #804040;">exec</span> <span style="color: #C5A22D;">&quot;silent! !echo -ne '<span style="">\\</span>ek&quot;</span> <span style="color: #000000;">.</span> <span style="color: #25BB4D;">expand</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'%:t'</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot;<span style="">\\</span>e<span style="">\\</span><span style="">\\</span>'&quot;</span>
augroup END
<span style="color: #804040;">endif</span></pre></div></div>

<p><strong>[UPDATE]</strong>Auf dem <a href="http://blog.256bit.org/archives/451-Vim-in-Screen.html" title="Vim in Screen" target="_blank" class="liexternal">256bit Blog</a> beschreibt Christian Brabandt, wie man den Screen-Titel von Vim aus ohne autocommands und mit sehr vielen Einstellungen setzen kann.<strong>[/UPDATE]</strong></p>
<p>Ansonsten finde ich es sehr praktisch, wenn in der Statusleiste das aktuelle Verzeichnis steht. Da der komplette Pfad meist sehr lang ist, können mit einem regulären Ausdruck alle Verzeichnisse bis auf das letzte jeweils mit ihren Anfangsbuchstaben abgekürzt werden:</p>
<p><code>s/(\w).*?\//$1\//g</code></p>
<p>So wird <code>/home/zimon/.vim/plugin</code> zu <code>/h/z/.v/plugin</code> abgekürzt.</p>
<p>Wem das immer noch zu lang ist, der kann aus dem Namen des letzten Verzeichnisses auch noch die Vokale heraus nehmen (ähnlich wie es dillo in seinen Tabs macht):</p>
<p><code>s/(\w).*?\//$1\//g; my $end; $x=~s/\/(.)(?:[aeiou]|([^aeiou\/])(?{$end.=$^N}))*\s*$/\/$1$end/</code></p>
<p>Damit wird <code>/home/zimon/.vim/plugin</code> zu <code>/h/z/.v/plgn</code></p>
<p>Um in der Bash den Screen-Titel automatisch zu setzen, kann man folgenden Code zu seiner <code>~/.bashrc</code> hinzufügen. Hier wurde der erste reguläre Ausdruck genutzt (man kann ihn natürlich auch durch den anderen Ausdruck ersetzen, wenn man möchte):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> precmd <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$TERM</span>&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
        <span style="color: #c20cb9; font-weight: bold;">screen</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">wd</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'$x=shift; $x=~ s/(\w).*?\//$1\//g; print $x;'</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PWD</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;\033k<span style="color: #007800;">$wd</span>\033\<span style="color: #000099; font-weight: bold;">\&quot;</span>
        ;;
    esac
}
PROMPT_COMMAND=precmd</span></pre></div></div>

<p>Für zsh User gibt es hier den Code für die <code>~/.zshrc</code>. In diesem Beispiel wurde der zweite reguläre Ausdruck genutzt:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> precmd <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$TERM</span>&quot;</span> == <span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">wd</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'$x=shift; $ x=~ s/(\w).*?\//$1\//g; my $end; $x=~s/\/(.)(?:[aeiou]|([^aeiou\/])(?{$end.=$^N}))*\s*$/\/$1$end/; print $x;'</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PWD</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
	<span style="color: #007800;">PR_STITLE</span>=$<span style="color: #ff0000;">'%{\ek'</span><span style="color: #007800;">$wd</span>$<span style="color: #ff0000;">'\e\\%}'</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Vielen Dank an <a href="http://zinformatik.de/author/ploppor/" target="_self" class="liinternal">Ploppor</a> für den zsh Code und sonstige zahlreiche Unterstützung in letzter Zeit.</p>
<p>Es tut mir leid, dass ich in letzter Zeit nichts geschrieben habe, aber meine Diplomarbeit lässt mir einfach keine Zeit.</p>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/titel-von-screen-fenstern-automatisch-von-vim-bash-und-zsh-setzen/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Perl Einzeiler auf der Konsole</title>
		<link>http://zinformatik.de/linux/perl-einzeiler-auf-der-konsole/</link>
		<comments>http://zinformatik.de/linux/perl-einzeiler-auf-der-konsole/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 18:59:11 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programmieren]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[Einzeiler]]></category>
		<category><![CDATA[Konsole]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=1373</guid>
		<description><![CDATA[<p>Ähnlich wie bei <a href="Keine Angst vor awk - ein Schnelleinstieg" title="http://zinformatik.de/linux/keine-angst-vor-awk-ein-schnelleinstieg/" class="liinternal">awk</a> kann man perl das Programm auf der Konsole direkt übergeben. Dazu ruft man perl mit der Option <code>-e</code> auf. Das Programm muss dann in Hochkommata eingeschlossen werden.<br />
Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print &#34;Hello World!\n&#34;'</span></pre></div></div>

<p>Um jede print-Anweisung mit einem &#8220;newline&#8221; abzuschließen, kann&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Ähnlich wie bei <a href="Keine Angst vor awk - ein Schnelleinstieg" title="http://zinformatik.de/linux/keine-angst-vor-awk-ein-schnelleinstieg/" class="liinternal">awk</a> kann man perl das Programm auf der Konsole direkt übergeben. Dazu ruft man perl mit der Option <code>-e</code> auf. Das Programm muss dann in Hochkommata eingeschlossen werden.<br />
Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print &quot;Hello World!\n&quot;'</span></pre></div></div>

<p>Um jede print-Anweisung mit einem &#8220;newline&#8221; abzuschließen, kann man zusätzlich die Option <code>-l</code> benutzen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-l</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print &quot;Hello World!&quot;; print &quot;Hello Ubuntuusers!&quot;'</span></pre></div></div>

<p>Um eine Datei Zeile für Zeile durchzugehen, gibt es die Option <code>-n</code>. Das Program beschreibt dann ähnlich wie bei awk was mit jeder Zeile geschehen soll:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-l</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print $_ if $_ =~ /foo/&quot;'</span> datei.txt</pre></div></div>

<p>gibt alle Zeilen aus, die &#8220;foo&#8221; enthalten.<br />
Die Option -n entspricht folgendem Code:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># Der Code, der übergeben wird</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die Option -a bringt perl noch einen Schritt näher zu awk. Damit werden die Wörter einer Zeile (die durch Leerzeichen getrennt sind) in das Array <code>@F</code> geladen. Den Worttrenner kann man mit der Option <code>-F</code> setzen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-a</span> -F; <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print $F[$#F]'</span> datei.csv</pre></div></div>

<p>gibt von jeder Zeile das letzte Element einer csv (Comma Separated Value) Datei aus.<br />
Dies entspricht folgendem Code:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">@F</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$pattern</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># Der übergebene Code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>wobei <code>$pattern</code> das mit -F übergebene Pattern (im obigen Beispiel <code>";"</code>) ist. Dadurch sind auch reguläre Ausdrücke wie <code>-F/[0-9]+/</code> möglich.</p>
<p>Es können auch die Schlüsselwörter BEGIN und END gesetzt werden um einen Block vor oder nach der Hauptschleife auszuführen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-l</span> <span style="color: #660033;">-a</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'BEGIN{$x=0} {$x+=$F[$#F]} END{print $x}'</span> preisliste.txt</pre></div></div>

<p>errechnet den Gesammtpreis einer Preisliste, in der am Ende jeder Zeile der Preis steht. Es wird also von jeder Zeile das letzte Wort addiert. Man könnte das Programm jetzt auch noch etwas kürzer schreiben:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-nlae</span> <span style="color: #ff0000;">'$x+=$F[$#F]; END{print $x}'</span> preisliste.txt</pre></div></div>

<p>Dies entspricht dem awk-Programm:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{x+=$NF} END{print x}'</span> preisliste.txt</pre></div></div>

<p>Für das Suchen und Ersetzen mit Regulären Ausdrücken eignet sich die Option <code>-p</code>. Diese Funktioniert genau wie <code>-n</code> mit dem Unterschied, dass jede Zeile automatisch ausgegeben wird. So kann man wie mit sed Dateien ändern:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-pe</span> <span style="color: #ff0000;">'s/foo/bar/g'</span> datei.txt <span style="color: #000000; font-weight: bold;">&gt;</span> datei_new.txt</pre></div></div>

<p>ersetzt jedes Vorkommen von &#8220;foo&#8221; durch &#8220;bar&#8221; und schreibt das Ergebnis in die Datei &#8220;datei_new.txt&#8221;.</p>
<p>Um die Datei direkt zu ändern, kann man die Option <code>-i</code> verwenden. Dieser kann optional ein String mitgegeben, der an den verwendeten Dateinamen angehängt den Dateinamen einer anzulegenden Sicherheitskopie ergibt:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-pi</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/foo/bar/g'</span> <span style="color: #000000; font-weight: bold;">*</span>.txt
<span style="color: #c20cb9; font-weight: bold;">perl</span> -pi.orig <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/foo/bar/g'</span> datei.txt</pre></div></div>

<p>Beim ersten Befehl jede Text-Datei im aktuellen Verzeichnis direkt geändert. Beim zweiten wird die &#8220;datei.txt&#8221; nach &#8220;datei.txt.orig&#8221; kopiert bevor jedes Vorkommen von &#8220;foo&#8221; durch &#8220;bar&#8221; ersetzt wird. (Man kann natürlich auch mit dem 2. Befehl beliebig viele Dateien bearbeiten, wenn man * benutzt.)</p>
<p><strong>Module</strong><br />
Die Option <code>-M</code> erlaubt es einem beliebige Module einzubinden, die dann im Programm benutzt werden können. So kann man z.B. mit dem folgenden Befehl mit dem Modul LWP::Simple eine Webite laden und den HTML-Code ausgeben:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> -MLWP::Simple <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'getprint(&quot;http://zinformatik.de&quot;)'</span></pre></div></div>

<p>Mit dem folgenden Befehl werden von allen iso Dateien im Verzeichnis MD5-Summen erstellt:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">*</span>.iso <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> -MDigest::MD5 <span style="color: #660033;">-nle</span> <span style="color: #ff0000;">'open(FILE,$_); $d=Digest::MD5-&gt;new; $d-&gt;addfile(FILE); print $d-&gt;hexdigest'</span></pre></div></div>

<p><strong>Fazit</strong><br />
Für kleinere kosmetische Änderungen eines Textes oder einer Ausgabe sowie zum extrahieren von Informationen daraus ist awk meist besser geeignet. Bei komplexeren Aufgabenstellungen ist die Mächtigkeit von perl jedoch vorteilhafter.<br />
Vor allem durch die Möglichkeit der Dateioperationen sollen manche Aktionen beschleunigt werden können:<br />
Statt des Aufrufs:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*.bak&quot;</span> <span style="color: #660033;">-exec</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">&quot;{}&quot;</span> \;</pre></div></div>

<p>der für jede gefundene Datei einen neuen Prozess startet um sie zu löschen kann man folgenden Befehl nutzen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*.bak&quot;</span> <span style="color: #660033;">-print</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-nle</span> <span style="color: #ff0000;">'unlink'</span></pre></div></div>

<p>Es ist jedenfalls nicht verkehrt beides zu kennen, ganz nach dem Motto: Für jeden Zweck die richtige Programmiersprache.</p>
<p><strong>[UPDATE]</strong>Mehr informationen liefert der Befehl <code>perldoc perlrun</code> (perldoc muss installiert sein) oder die Seite <a href="http://p3rl.org/perlrun" title="Dokumentation von perlrun" target="_blank" class="liexternal">http://p3rl.org/perlrun</a><strong>[/UPDATE]</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/perl-einzeiler-auf-der-konsole/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Praktische alias-Kommandos für die Bash</title>
		<link>http://zinformatik.de/linux/praktische-alias-kommandos-fur-die-bash/</link>
		<comments>http://zinformatik.de/linux/praktische-alias-kommandos-fur-die-bash/#comments</comments>
		<pubDate>Sun, 17 May 2009 17:14:12 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[alias]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Konsole]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=1235</guid>
		<description><![CDATA[<p>Sehr praktisch bei der Bash ist die Möglichkeit sich alias-Befehle zu konfigurieren. Damit kann man bestehende Befehle überschreiben oder Abkürzungen hinzufügen. Ich habe mal einige praktische alias-Kommandos zusammen gestellt. Es wären natürlich noch viel mehr denkbar, aber vielleicht regen die hier genannten ja beim einen oder anderen die Phantasie an.</p>
<p>Übrigens kann man die &#8220;Ursprungsversion&#8221; eines Kommandos, welches durch einen&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Sehr praktisch bei der Bash ist die Möglichkeit sich alias-Befehle zu konfigurieren. Damit kann man bestehende Befehle überschreiben oder Abkürzungen hinzufügen. Ich habe mal einige praktische alias-Kommandos zusammen gestellt. Es wären natürlich noch viel mehr denkbar, aber vielleicht regen die hier genannten ja beim einen oder anderen die Phantasie an.</p>
<p>Übrigens kann man die &#8220;Ursprungsversion&#8221; eines Kommandos, welches durch einen Alias überschrieben wurde durch ein Vorrangestelltes <em>\</em> aufrufen. Also wenn es z.B. folgenden alias gibt:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">ls</span></span>=<span style="color: #ff0000;">&quot;ls --color=auto&quot;</span></pre></div></div>

<p>so kann man durch die Eingabe von</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">\<span style="color: #c20cb9; font-weight: bold;">ls</span></pre></div></div>

<p>den Befehl <em>ls</em> ohne <em>&#8211;color=auto</em> aufrufen.</p>
<p><strong>Grundlegende Kommandos im Dateisystem</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Macht die Ausgabe von ls farbig:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">ls</span></span>=<span style="color: #ff0000;">'ls --color=auto'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Gibt zu jeder Datei weitere Informationen aus. Größenangaben immer &quot;menschenlich lesbar&quot;:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">ll</span>=<span style="color: #ff0000;">'ls -lh'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Wie ll, aber es werden auch versteckte Dateien aufgelistet:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">la</span>=<span style="color: #ff0000;">'ls -lha'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Für Vertipper recht praktisch:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> cd..=<span style="color: #ff0000;">'cd ..'</span> 
<span style="color: #7a0874; font-weight: bold;">alias</span> cd-=<span style="color: #ff0000;">'cd -'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Abkürzung für cd .. :</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> ..=<span style="color: #ff0000;">'cd ..'</span> 
<span style="color: #7a0874; font-weight: bold;">alias</span> ...=<span style="color: #ff0000;">'cd ../..'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Alle nicht existierenden Verzeichnisse automatisch erstellen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span></span>=<span style="color: #ff0000;">'mkdir -p'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Für Anfänger zu empfehlen (es wird bei jeder Datei nachgefragt)</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">rm</span></span>=<span style="color: #ff0000;">'rm -i'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">cp</span></span>=<span style="color: #ff0000;">'cp -i'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">mv</span></span>=<span style="color: #ff0000;">'mv -i'</span></pre></div></div>

<p><strong>Befehle die immer mit bestimmten Optionen ausgeführt werden sollen sowie Abkürzungen</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Bei df die Größen immer &quot;menschlich lesbar&quot; anzeigen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">df</span></span>=<span style="color: #ff0000;">'df -h'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Bei du die Größen immer &quot;menschlich lesbar&quot; anzeigen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">du</span></span>=<span style="color: #ff0000;">'du -h'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Suche in less case-insensitive:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">l</span>=<span style="color: #ff0000;">'less -i'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Zeigt Änderungen einer Datei an (wie tail -f):</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">lf</span>=<span style="color: #ff0000;">'less -i +F'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Beispielhafte Abkürzung für ssh</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">meinserver</span>=<span style="color: #ff0000;">'ssh user@meinserver.tld'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># lässt grep case-insensitive arbeiten und markiert die Treffer farbig:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">grep</span></span>=<span style="color: #ff0000;">'grep -i --color=auto'</span></pre></div></div>

<p>Die Follow-Option von <em>less</em> (<em>less +F</em>) ist recht praktisch, da sie einfach mit Strg+c unterbrochen und mit F weitergeführt werden kann. So kann man leicht nochmal zurück scrollen.</p>
<p><strong>Befehle zum Suchen, Installieren und Updaten</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Da apt-get ja meist mit sudo aufgerufen wird:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span>=<span style="color: #ff0000;">'sudo apt-get'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">#Statt dessen kann man auch die folgenden Befehle nutzen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">agi</span>=<span style="color: #ff0000;">'sudo apt-get install'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">acs</span>=<span style="color: #ff0000;">'apt-cache search'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Das ganze System updaten:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">update</span>=<span style="color: #ff0000;">'sudo apt-get update &amp;&amp; sudo apt-get upgrade'</span></pre></div></div>

<p><strong>Komplexere Befehle</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Starte Kommando in neuem Screen-Fenster:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">s</span>=<span style="color: #ff0000;">'screen -X screen'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Dateinamen im aktuellen Verzeichnis nach Ausdruck durchsuchen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">lg</span>=<span style="color: #ff0000;">'ls --color=always | grep --color=always -i'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Timestamp generieren:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">timestamp</span>=<span style="color: #ff0000;">'date &quot;+%Y%m%d%H%M%S&quot;'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Verzeichnis mit aktuellem Timestamp erstellen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">tsdir</span>=<span style="color: #ff0000;">'timestamp | xargs mkdir'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Änderungen bei allen angegebenen Logdateien live anzeigen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mylogs</span>=<span style="color: #ff0000;">'sudo tail -f /var/log/{syslog,messages}'</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Einen Prozess (nach Namen) suchen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">psg</span>=<span style="color: #ff0000;">'ps ax | grep -v grep | grep '</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Die öffentliche IP ausgeben:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">myip</span>=<span style="color: #ff0000;">&quot;wget -qO - http://checkip.dyndns.org | sed 's/[a-zA-Z&lt;&gt;/ :]//g'&quot;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Zuletzt bearbeitete Datei in vim öffnen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">lvim</span>=<span style="color: #ff0000;">&quot;vim -c <span style="color: #000099; font-weight: bold;">\&quot;</span>normal '0<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Die 10 Prozesse anzeigen, die den meisten RAM verbrauchen:</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">psram</span>=<span style="color: #ff0000;">'ps aux | sort -rnk 4 | head'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Zeige alle Prozesse bei ps (außer ps selbst) mit langer Ausgabe</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">ps</span></span>=<span style="color: #ff0000;">'ps ax | grep -v &quot;ps ax&quot;'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/praktische-alias-kommandos-fur-die-bash/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Tipps und Tricks für die Bash</title>
		<link>http://zinformatik.de/linux/tipps-und-tricks-fur-die-bash/</link>
		<comments>http://zinformatik.de/linux/tipps-und-tricks-fur-die-bash/#comments</comments>
		<pubDate>Sun, 10 May 2009 17:12:03 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tipps und Tricks]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[History]]></category>
		<category><![CDATA[Konsole]]></category>
		<category><![CDATA[Prozesse]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=1194</guid>
		<description><![CDATA[<p>Hier ein paar kleine Tipps und Tricks für die Bash.</p>
<p><strong>Livesuche in der History</strong><br />
Sucht man ein Kommando, welches man schon einmal eingegeben hat, so kann man die History (welche in der Datei ~/.bash_history gespeichert wird) durchsuchen. Die Bash stellt dafür sogar eine Livesuche bereit. Diese wird mit der Tastenkombination Strg+r eingeleitet. Danach kann man eine beliebige Zeichenkette eingeben, die&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Hier ein paar kleine Tipps und Tricks für die Bash.</p>
<p><strong>Livesuche in der History</strong><br />
Sucht man ein Kommando, welches man schon einmal eingegeben hat, so kann man die History (welche in der Datei ~/.bash_history gespeichert wird) durchsuchen. Die Bash stellt dafür sogar eine Livesuche bereit. Diese wird mit der Tastenkombination Strg+r eingeleitet. Danach kann man eine beliebige Zeichenkette eingeben, die in irgendeinem Befehl schonmal vorgekommen ist. Mit jedem eingegebenen Buchstaben werden Vorschläge gemacht, wobei die Suche immer Rückwärts läuft.<br />
Durch erneutes Drücken von Strg+r kann man weiter suchen. So kann man dann z.b. mit <em>Strg+r sudo Strg+r</em> zum vorletzten <em>sudo</em>-Befehl gelangen.</p>
<p><strong>Weitere History-Funktionen</strong><br />
Mit dem Befehl <em>history</em> werden alle eingegebenen Kommandos, die sich in der History befinden mit Zeilennummer ausgegeben. Gibt man dem Befehl noch eine Zahl x als Parameter mit, so werden nur die letzten x Einträge angezeigt. Mit der Option -c (clear) kann man die History löschen.<br />
Normalerweise wird die Datei ~/.bash_history erst beim beenden der Shell geschrieben. Man kann sie aber auch sofort schreiben, indem man den Befehl <em>history -w</em> ausführt. Umgekehrt kann man die aktuelle History auch mittels <em>history -r</em> aus der Datei ~/.bash_history einlesen.</p>
<p><strong>Praktische Tastenkombinationen</strong></p>
<ul>
<li>Strg+l &#8211; (kleines L) löscht den Bildschirm</li>
<li>Strg+u &#8211; löscht alles vor dem Cursor</li>
<li>Strg+t &#8211; tauscht die letzten beiden Zeichen</li>
<li>Alt+. &#8211; fügt das letzte Argument ein</li>
<li>Strg+d &#8211; beendet die Sitzung bzw. sendet ein EOF (z.B. für at).</li>
</ul>
<p><strong>Praktische Variablen</strong></p>
<ul>
<li>!! &#8211; kompletter letzter eingegebener Befehl</li>
<li>!$ &#8211; letztes Argument</li>
<li>$$ &#8211; PID der aktuellen Bash</li>
<li>!String &#8211; letztes Kommando, das mit <em>String</em> angefangen hat</li>
<li>!?String &#8211; letztes Kommando, welches <em>String</em> enthielt.</li>
</ul>
<p>So kann man mit <em>sudo !!</em> den letzten Befehl mit root-Rechten erneut ausführen oder ihn mit <em>echo &#8220;!!&#8221; > script.sh</em> in eine Datei schreiben.<br />
Nach einem Kopierbefehl wechselt man mit <em>cd !$</em> in das Verzeichnis, wohin die Datei(en) kopiert wurde(n).<br />
Mit <em>kill -9 $$</em> wird die Bash geschlossen ohne dass die Historydatei geschrieben werden kann.</p>
<p><strong>vi-Modus</strong><br />
Die Bash beinhaltet einen vi-Modus, wodurch man vi-Befehle in der Bash nutzen kann. Den Modus startet man mittels</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-o</span> <span style="color: #c20cb9; font-weight: bold;">vi</span></pre></div></div>

<p>Danach kann man mit ESC in den Kommandomodus gelangen und mit hjkl statt Pfeiltasten navigieren. auch dd, D , p , A, 0 und $,&#8230; funktionieren.<br />
Die obige Zeile kann man sich auch in die ~/.bashrc schreiben, damit der vi-Modus standardmäßig aktiviert ist.<br />
Leider funktioniert danach die Tastenkombination Strg+l nicht mehr. Diese kann man jedoch mit einem Binding wieder aktivieren. Dafür schreibt man folgendes in die ~/.bashrc:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Strg+l - clear screen</span>
<span style="color: #7a0874; font-weight: bold;">bind</span> <span style="color: #660033;">-m</span> vi-insert <span style="color: #ff0000;">&quot;\C-l&quot;</span>:clear-screen</pre></div></div>

<p><strong>Geänderte Konfigurationsdatei einlesen ohne neues Terminal aufzumachen</strong><br />
kann man mit folgendem Befehl:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">source</span> ~<span style="color: #000000; font-weight: bold;">/</span>.bashrc</pre></div></div>

<p><strong>Prozesskontrolle</strong><br />
Um ein Programm im Hintergrund zu starten hängt man dem Befehl ein <em>&#038;</em> an.<br />
Hat man dies vergessen, so kann man das Programm mit Strg+z anhalten, wodurch man wieder einen Prompt bekommt. Dann kann man den Prozess mittels <em>bg</em> im Hintergrund weiterlaufen lassen. Um ihn wieder in den Vordergrund zu holen genügt ein <em>fg</em>.<br />
Wenn mehrere Prozesse im Hintergrund laufen, so holt <em>fg</em> den letzten Prozess zurück, der in den Hintergrund geschickt wurde. Möchte man einen anderen Prozess in den Vordergrund holen, so kann man <em>fg</em> die entsprechende Job-ID (diese steht in Eckigen klammern, wenn ein Prozess gestoppt oder in den Hintergrund verschoben wird) als Argument mitgeben. Alternativ kann man als Paramter für <em>fg</em> ein <em>%</em> gefolgt von einigen Anfangsbuchstaben des Befehls mitgeben.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">fg</span> <span style="color: #000000; font-weight: bold;">%</span>v</pre></div></div>

<p>holt zum Beispiel einen vim-Prozess aus dem Hintergrund (wenn keine anderen Prozesse mit v im Hintergrund sind).</p>
<p>Um zwei Befehle nacheinander auszuführen, kann man sie mit einem Semikolon trennen. Möchte man, dass der 2. Befehl nur ausgeführt wird, wenn der erste Befehl erfolgreich war, so trennt man sie mit <em>&#038;&#038;</em>. dies ist praktisch beim compilieren von Programmen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>So wird <em>make</em> nur ausgeführt, wenn das configurescript ohne Fehler durchgelaufen ist. <em>make install</em> wird nur ausgeführt, wenn <em>make</em> auch wirklich compiliert hat.</p>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/tipps-und-tricks-fur-die-bash/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Keine Angst vor awk &#8211; ein Schnelleinstieg</title>
		<link>http://zinformatik.de/linux/keine-angst-vor-awk-ein-schnelleinstieg/</link>
		<comments>http://zinformatik.de/linux/keine-angst-vor-awk-ein-schnelleinstieg/#comments</comments>
		<pubDate>Sat, 02 May 2009 08:36:44 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programmieren]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[Konsole]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=1081</guid>
		<description><![CDATA[<p>Um meine <a href="http://zinformatik.de/linux/commandlinefu-die-besten-einzeiler-auf-der-konsole/" title="Commandlinefu - die besten Einzeiler auf der Linux Konsole" target="_self" class="liinternal">Commandline-Fu</a> Skills zu trainieren hab ich mir mal <a href="http://de.wikipedia.org/wiki/Awk" title="Wikipedia - awk" target ="_blank" target="_blank" rel="nofollow" class="liwikipedia">awk</a> vorgenommen. awk gilt bei vielen Linux-Anwendern als eines der kompliziertesten Kommandos. Um mit awk zurecht zu kommen ist es jedoch einfacher es als sehr leichte Programmiersprache zu sehen&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Um meine <a href="http://zinformatik.de/linux/commandlinefu-die-besten-einzeiler-auf-der-konsole/" title="Commandlinefu - die besten Einzeiler auf der Linux Konsole" target="_self" class="liinternal">Commandline-Fu</a> Skills zu trainieren hab ich mir mal <a href="http://de.wikipedia.org/wiki/Awk" title="Wikipedia - awk" target ="_blank" target="_blank" rel="nofollow" class="liwikipedia">awk</a> vorgenommen. awk gilt bei vielen Linux-Anwendern als eines der kompliziertesten Kommandos. Um mit awk zurecht zu kommen ist es jedoch einfacher es als sehr leichte Programmiersprache zu sehen und nicht als kompliziertes Kommando (zumindest ging es mir so). awk ist auch eigentlich eine Programmierspache primär um Textdateien zu verarbeiten. Sie eignet sich auch hervorragend für die Standardeingabe STDIN. Aber auch wer nicht programmieren kann, wird vielleicht an den einfachen Konstrukten gefallen finden. Dies hier wird jedoch keine Programmieranleitung.</p>
<p>Ich gehe hier auf nawk (new awk) ein, dass auf den  meisten Linux-Distributionen der Standard ist. Es gibt noch eine erweiterte Version namens gawk (GNU awk), auf der die hier genannten Beispiele aber genauso funktionieren.</p>
<p>Wer irgendeine Programmiersprache (am besten C, Java oder Perl) beherrscht, wird mit awk recht schnell zurecht kommen. awk liest meist eine Textdatei (oder andere Eingabe) ein und zerstückelt sie direkt in Zeilen und diese wiederrum in Felder (Wörter). Dadurch hat man sich schon etwas Programmieraufwand gespart. Nun geht awk den Text Zeile für Zeile durch und führt das Programm auf jeder einzelnen Zeile aus. Es gibt einige Standardvariablen, die man verwenden kann:</p>
<ul>
<li> $0 ist die aktuelle Zeile</li>
<li>$1 .. $n sind die Wörter der Zeile</li>
<li>NF (Number of Fields) ist die Anzahl der Felder der aktuellen Zeile</li>
<li>NR (Number of Records) ist die Nummer der aktuellen Zeile</li>
</ul>
<p>Mit diesen Variablen kann man schon recht praktische Programme schreiben. Dabei wird für jede Zeile nach folgendem Schema vorgegangen</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">Muster <span style="color: #009900;">&#123;</span> Aktion <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Wobei das Muster meist ein <a href="http://de.wikipedia.org/wiki/Regexp" title="Wikipedia - regulärer Ausdruck" target="_blank" rel="nofollow" class="liwikipedia">regulärer Ausdruck</a> ist, bei dessen Gültigkeit (für die Zeile) die Aktion auf die Zeile ausgeführt wird. Es kann jedoch eine beliebige Bedingung sein. Oft wird das Muster weggelassen, was dazu führt, dass die Aktion auf jede Zeile ausgeführt wird.</p>
<p>Ein paar einfache Beispiele:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span> datei.txt</pre></div></div>

<p>Gibt von jeder Zeile das 2. Wort aus</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print NF}'</span> datei.txt</pre></div></div>

<p>Gibt von jeder Zeile die Anzahl der Wörter aus</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $NF}'</span> datei.txt</pre></div></div>

<p>Gibt von jeder Zeile das letzte Wort aus</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print NR &quot;: &quot; $0}'</span> datei.txt</pre></div></div>

<p>Gibt jede Zeile mit vorangestellter Zeilennummer aus. </p>
<p>Wie man sieht können Strings einfach verkettet werden indem sie aneinander geschrieben werden (ein Leerzeichen dazwischen erhöht die Lesbarkeit). Variablen werden hingegen mit <em>,</em> konkateniert. Ein Semikolon trennt (wie bei vielen Programmiersprachen) 2 Anweisungen.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'END{print NR}'</span> datei.txt</pre></div></div>

<p>Gibt die Anzahl der Zeilen aus. Das <em>END</em> ist ein Muster, das besagt, dass die Aktion erst ausgeführt werden soll, wenn alle Zeilen bearbeitet wurden. Entsprechend gibt es ein Muster <em>BEGIN</em>, dessen Aktion vor dem Einlesen der Datei ausgeführt wird.</p>
<p>Die Hochkommata umschließen das eigentliche Programm und verhindern, dass die Shell Zeichen daraus interpretiert. Entsprechend müssen Strings innerhalb des Programms mit doppelten Anführungszeichen gekennzeichnet werden.</p>
<p><br clear="all" /><br />
<strong>Variablen</strong><br />
Es gibt nur 2 Typen von Variablen: Zahlenwerte und Strings. Variablen brauchen nicht deklariert zu werden und werden standardmäßig mit 0 bzw. &#8220;&#8221; initialisiert. Wie bei Perl wird der Typ automatisch bestimmt.</p>
<p>Ein einfaches Beispiel könnte so aussehen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{sum+=$NF} END{print sum}'</span> datei.txt</pre></div></div>

<p>Dieses Programm addiert das letzte Wort jeder Zeile zu der Variablen <em>sum</em> hinzu und gibt am Ende den Inhalt von <em>sum</em> aus. Dies ist recht praktisch, wenn man sich die einzelnen Posten einer Bestellung in eine Datei schreibt wobei als letztes Wort jeweils der Preis steht. So kann einfach die Gesamtsumme berechnet werden ohne jeden Betrag einzeln in einen Taschenrechner tippen zu müssen.</p>
<p>Ein weiteres schönes Beispiel ist den Durchschnitt zu errechnen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{sum+=$NF} END{print sum/NR}'</span> datei.txt</pre></div></div>

<p>Hier wird das Ergebnis noch durch die Anzahl der Zeilen geteilt. Dies ist praktisch z.B. für einen Notendurchschnitt.</p>
<p>Ich kenne kaum eine Programmiersprache, bei der man mit so wenig Kenntnissen (Variablen, Print-Anweisung und Arithmetik) schon so viele praktische Dinge erledigen kann. Nimmt man nun noch einfache reguläre Ausdrücke hinzu, kann hat man noch mehr praktische Möglichkeiten. </p>
<p><br clear="all" /><br />
<strong>Reguläre Ausdrücke als Muster</strong<br />
Die folgenden Beispiele ließen sich so oder ähnlich auch mit anderen Kommandos realisieren, doch hier geht es ja um awk:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/png$/{print}'</span></pre></div></div>

<p>Gibt alle png-Dateien in einem Verzeichnis aus. Statt <em>print $0</em> reicht auch ein einfaches <em>print</em>, da ohne ein Argument standardmäßig die aktuelle Zeile ausgegeben wird. Dies ist also die gleiche Funktionalität wie ein simples grep. Genauso kann man auch wie mit grep Dateien durchsuchen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/foo/{print}'</span> datei.txt</pre></div></div>

<p>Gibt (wie grep) alle Zeilen aus, die &#8220;foo&#8221; enthalten. Um auch die Zeilennummern mit auszugeben, braucht man nur zusätzlich noch NR:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/foo/{print NR&quot;: &quot;$0}'</span> datei.txt</pre></div></div>

<p>Hier noch einige Beispiele mit dem Kommando <em>ls</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/png$/{sum+=$5} END{print sum}'</span></pre></div></div>

<p>Gibt die Gesammtgröße aller png-Dateien in einem Verzeichnis aus.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/png$/{sum+=$5; print} END{ print sum/(1024*1024)&quot; MB&quot;}'</span></pre></div></div>

<p>Zeigt die Ausgabe von ls -l für alle png-Dateien an und gibt am Ende die Gesamtgröße in Megabytes aus.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/png$/{sum+=$5; anz++; print} END{print &quot;Anzahl der PNG-Dateien: &quot;anz; print &quot;Gesammtgröße der PNG-Dateien: &quot;sum/1024&quot; kB ( &quot;sum&quot; Bytes )&quot;; print &quot;Durchschnittliche Größe einer PNG-Datei: &quot;(sum/anz)/1024&quot; kB&quot;}'</span></pre></div></div>

<p>Gibt alle PNG-Dateien mit <em>ls -l</em> aus und darunter, wieviele Dateien es waren, wie viel Speicher von allen png-Dateien im Verzeichnis verbraucht werden und wie groß eine Datei im Durchschnitt ist.</p>
<p>awk kennt auch Arrays, die beliebig dimensional sein können (mehr- und gemischtdimensional) und deren Länge vorher nicht festgelegt werden braucht. Auch assoziative Arrays sind möglich. Beispiele:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">arr<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># setzt den 6. Wert des Arrays auf 7</span>
arr<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;hallo&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># setzt den 4. Wert des 6. Arrays auf &quot;hallo&quot;. (Genauer gesagt setzt es &quot;5 SUPSEP 3&quot; auf &quot;hallo&quot;)</span>
arr<span style="color: #009900;">&#91;</span><span style="color: #ff0000;">&quot;first&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># assoziatives Array</span></pre></div></div>

<p>Die kürzeren Programme überlegt man sich in der Regel jedes mal neu und verwirft sie nach Gebrauch wieder. Bei längeren Programmen lohnt es sich jedoch auch mal sie zu speichern. Dafür wird der Teil in den Hochkommata in eine Datei geschrieben, die nun mit <em>awk -f</em> aufgerufen werden kann. Statt dessen kann man auch in die erste Zeile</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/awk -f</span></pre></div></div>

<p>schreiben und die Datei ausführbar machen. Dann kann man sie jederzeit aufrufen.</p>
<p><br clear="all" /><br />
<strong>Vordefinierte Funktionen</strong><br />
Bevor ich nun weiter auf die Kontrollstrukturen eingehe, möchte ich kurz einige vordefinierte Funktionen vorstellen, mit denen sich wieder viele Probleme lösen lassen.<br />
Am praktischsten ist wohl <em>getline</em>, mit dem die nächste Zeile in <em>$0</em> geladen wird. So kann man sich mittels</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ifconfig</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/eth0/{getline; print $2}'</span></pre></div></div>

<p>die IP-Adresse von eth0 anzeigen lassen. Doch es steht noch ein &#8220;Adresse:&#8221; davor. Um dieses Wort noch weg zu bekommen (um die IP-Adresse z.B. in Scripten verwenden zu können) kann man die Funktionen <em>substr(s,i[,n])</em> und <em>index(s,t)</em> nutzen. <em>substr</em> gibt einen Teilstring von <em>s</em> ab der Position <em>i</em> aus. Das Optionale <em>n</em> gibt an wieviele Zeichen ausgegeben werden sollen. <em>index</em> gibt die Position des ersten vorkommens von <em>t</em> in <em>s</em> aus.<br />
Daraus kann man das Programm verfeinern, so dass wirklich nur die IP-Adresse ausgegeben wird:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ifconfig</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/eth0/{getline; print substr($2,index($2,&quot;:&quot;)+1)}'</span></pre></div></div>

<p>Ein <em>substr($2,9)</em> hätte es in diesem Fall zwar auch getan, das hätte bei einer englischen Ausgabe jedoch nicht mehr funktioniert (und ich habe so direkt 2 Funktionen erklärt <img src='http://zinformatik.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  )</p>
<p>Einige weitere vordefinierte Funktionen sind:</p>
<ul>
<li><em>sin(x)</em>,<em>cos(x)</em> &#8211; Sinus und Cosinus von x (mit x in Radiant)</li>
<li><em>int(x)</em> &#8211; Gibt Ganzzahl zurück, indem Nachkommastellen von x abgeschnitten werden</li>
<li><em>sqrt(x)</em> &#8211; Wurzel von x</li>
<li><em>rand()</em> &#8211; Zufallswert zwischen 0 und 1</li>
<li><em>and(a,b)</em>, <em>or(a,b)</em>, <em>xor(a,b)</em>, <em>compl(a)</em> &#8211; Logische Operationen</li>
<li><em>gsub(r,s[,t])</em> &#8211; ersetzt jedes vorkommen von von r durch s in der Variablen t (bzw. $0, falls t nicht gesetzt)</li>
<li><em>length(s)</em> &#8211; gibt Länge von s zurück (ohne Argument Länge von $0)</li>
</ul>
<p>Das folgende Beispiel gibt die längste Zeile eines Textes mit Zeilennummer aus:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'length($0) &gt; longest {line=$0; longest=length($0); num=NR} END{print num&quot;: &quot;line}'</span> datei.txt</pre></div></div>

<p>Hier wurde als Muster eine Bedingung gewählt, deren Aktion ausgeführt wird, wenn sie wahr ist.</p>
<p><br clear="all" /><br />
<strong>Kontrollstrukturen</strong><br />
Die Kontrollstrukturen von awk sind denen von C, Perl und Java sehr ähnlich. Die if, while und for Konstrukte sind syntaktisch identisch. Daher werde ich hier nur ein Beispiel angeben. Wegen der Lesbarkeit habe ich das Programm in mehrere Zeilen geschrieben. Man kann jedoch auch einfach alles in eine Zeile schreiben.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{
	for(i=1;i&lt;=NF;i++){
		if(length($i)&gt;length(biggest)){
			biggest=$i;
		}
	}
}
END{print biggest}'</span> datei.txt</pre></div></div>

<p>gibt das längste Wort einer Datei aus. Für jede Zeile wird eine Variable i von 1 bis &#8220;Anzahl der Felder&#8221; hochgezählt und die Länge des entsprechenden Feldes mit dem bisher längsten Feld (biggest) verglichen. Ist das aktuell verglichene Feld größer als das bisher größte, so wird das größte Feld auf das aktuelle gesetzt. Am Ende wird das längste Feld ausgegeben. Als Einzeiler sieht das Programm so aus:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{for(i=1;i&lt;=NF;i++){if(length($i)&gt;length(biggest)){biggest=$i;}}}END{print biggest}'</span> datei.txt</pre></div></div>

<p><br clear="all" /><br />
<strong>Operatoren</strong><br />
Die Operatoren sind im Prinzip auch die gleichen wie in anderen Programmiersprachen und einige davon wurden ja auch schon benutzt. Interessant sind hier die Operatoren <em>~</em> und <em>!~</em>, die prüfen, ob ein regulärer Ausdruck matcht (bzw. nicht matcht).<br />
Das folgende Beispiel bestimmt die IP-Adresse von eth0. Hier wird zusätzlich zu den oben angegebenen Programmen noch geprüft, ob das entsprechende Wort eine Folge von Ziffern mit abschließendem Punkt beinhaltet. Wenn nicht, wird ein leerer String ausgegeben:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ifconfig</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/eth0/{getline; if($2 ~ /[0-9]+\./){print substr($2,index($2,&quot;:&quot;)+1)} else {print &quot;&quot;}}'</span></pre></div></div>

<p><br clear="all" /><br />
<strong>Funktionen</strong><br />
Natürlich lassen sich in awk auch eigene Funktionen schreiben. Diese sind einfach nach der Syntax</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> name<span style="color: #7a0874; font-weight: bold;">&#40;</span>arg1, arg2<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
...
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>zu schreiben und können wie gewohnt aufgerufen werden. Das folgende Beispiel definiert eine Funktion, die prüft, ob das übergebene Argument eine Primzahl ist. Das Programm nimmt nun jede Zeile der übergebenen Datei, die nur aus Ziffern besteht und gibt sie aus. dahinter wird geschrieben ob es eine Primzahl ist oder nicht.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/awk -f</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> isprim<span style="color: #7a0874; font-weight: bold;">&#40;</span>number<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">number</span>==<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
	<span style="color: #7a0874; font-weight: bold;">&#123;</span>
		<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>number<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000;">4</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
	<span style="color: #7a0874; font-weight: bold;">&#123;</span>
		<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>number<span style="color: #000000; font-weight: bold;">%</span>2==<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
	<span style="color: #7a0874; font-weight: bold;">&#123;</span>
		<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>number<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
	<span style="color: #7a0874; font-weight: bold;">&#123;</span>
		<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>number<span style="color: #000000; font-weight: bold;">%</span>3==<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
	<span style="color: #7a0874; font-weight: bold;">&#123;</span>
		<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span>
	<span style="color: #7a0874; font-weight: bold;">&#123;</span>
		<span style="color: #007800;">r</span>=int<span style="color: #7a0874; font-weight: bold;">&#40;</span>sqrt<span style="color: #7a0874; font-weight: bold;">&#40;</span>number<span style="color: #7a0874; font-weight: bold;">&#41;</span>+<span style="color: #000000;">0.5</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
		<span style="color: #007800;">f</span>=<span style="color: #000000;">5</span>;
		<span style="color: #000000; font-weight: bold;">while</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>f<span style="color: #000000; font-weight: bold;">&lt;</span>=r<span style="color: #7a0874; font-weight: bold;">&#41;</span>
		<span style="color: #7a0874; font-weight: bold;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>number<span style="color: #000000; font-weight: bold;">%</span><span style="color: #007800;">f</span>==<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
			<span style="color: #7a0874; font-weight: bold;">&#123;</span>
				<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>;
			<span style="color: #7a0874; font-weight: bold;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>number<span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>f+<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>==<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
			<span style="color: #7a0874; font-weight: bold;">&#123;</span>
				<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>;
			<span style="color: #7a0874; font-weight: bold;">&#125;</span>
			<span style="color: #007800;">f</span>=f+<span style="color: #000000;">6</span>;
		<span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>^<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>+$<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>isprim<span style="color: #7a0874; font-weight: bold;">&#40;</span>$<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
		<span style="color: #007800;">prim</span>=<span style="color: #ff0000;">&quot;: prim&quot;</span>;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
		<span style="color: #007800;">prim</span>=<span style="color: #ff0000;">&quot;&quot;</span>;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
	print $<span style="color: #000000;">0</span>,prim;
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p><br clear="all" /><br />
<strong>Ändern der Wort und Zeilentrenner</strong><br />
Welche Zeichen 2 Wörter bzw. Zeilen voneinander trennen, kann auch festgelegt werden. Dafür sind die beiden Variablen FS und RS zuständig. Setzt man z.B. <em>FS=&#8217;,&#8217;</em> so kann man recht gut csv-Dateien (csv=Comma Separated Values) verarbeiten. Dabei dürfen die Variablen mit regulären Ausdrücken belegt werden, so dass man mit <em>RS=&#8217;\. |\.\n|\! |\!\n|\? |\?\n&#8217;</em> mit Sätzen statt Zeilen arbeitet.<br />
Ein awk-Programm, welches jeden Satz in einer eigenen Zeile ausgibt wäre z.B.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'BEGIN{RS=&quot;\. |\.\n|\! |\!\n|\? |\?\n&quot;} {print}'</span> datei.txt</pre></div></div>

<p>Da print ohne Parameter automatisch $0 ausgibt.<br />
Die Variablen <em>FS</em> und <em>RS</em> können jederzeit und überall im Programm geändert werden.</p>
<p><br clear="all" /><br />
Dies war jetzt nur ein kurzer Anriß um awk ein wenig zu erklären. Es gibt natürlich noch Zahlreiche weitere Funktionen, vordefinierte (oder definierbare) Variablen, Aufrufoptionen, Kontrollstrukturen, Operatoren,&#8230;<br />
Aber mit diesem Wissen kann man schon recht schöne kleine awk-Programme schreiben um sich das Leben zu erleichtern. Auch für das Commandline-Fu sollten die hier vorgestellten Aspekte zumindest eine Zeit lang ausreichen.</p>
<p>Eine sehr schöne und ausführliche Anleitung zu awk gibt es unter <a href="http://www.ostc.de/awk.pdf" title="awk Anleitung" target="_blank" class="lipdf">http://www.ostc.de/awk.pdf</a>.<br />
Ein schönes cheat sheet bekommt man unter <a href="http://www.catonmat.net/download/awk.cheat.sheet.pdf" title="awk cheat sheet" target="_blank" class="lipdf">http://www.catonmat.net/download/awk.cheat.sheet.pdf</a> (englisch).<br />
Ansonsten hält wie immer die manpage weitere Informationen bereit.</p>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/keine-angst-vor-awk-ein-schnelleinstieg/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Einige Tipps und Tricks für Screen &#8211; Teil 2</title>
		<link>http://zinformatik.de/linux/einige-tipps-und-tricks-fur-screen-teil-2/</link>
		<comments>http://zinformatik.de/linux/einige-tipps-und-tricks-fur-screen-teil-2/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 11:44:30 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[Konsole]]></category>
		<category><![CDATA[Screen]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=1076</guid>
		<description><![CDATA[<p>Nachdem Ploppor eine <a href="http://zinformatik.de/tipps-tricks/interessante-programme/einfuhrung-in-screen/" title="Einführung in Screen" target="_self" class="liinternal">Einführung in Screen</a> gegeben hat, habe ich diese um ein paar <a href="http://zinformatik.de/linux/einige-tipps-und-tricks-fur-screen/" title="Einige Tipps und Tricks für Screen" target="_self" class="liinternal">prakische Tipps</a> erweitert. Hier jetzt noch einige weitere Tipps.<br />
Die Konfigurationsdatei .screenrc, die hier häufig verwendet wird liegt normalerweise im Home-Verzeichnis eines Users. Wenn sie noch nicht existiert, kann man&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Nachdem Ploppor eine <a href="http://zinformatik.de/tipps-tricks/interessante-programme/einfuhrung-in-screen/" title="Einführung in Screen" target="_self" class="liinternal">Einführung in Screen</a> gegeben hat, habe ich diese um ein paar <a href="http://zinformatik.de/linux/einige-tipps-und-tricks-fur-screen/" title="Einige Tipps und Tricks für Screen" target="_self" class="liinternal">prakische Tipps</a> erweitert. Hier jetzt noch einige weitere Tipps.<br />
Die Konfigurationsdatei .screenrc, die hier häufig verwendet wird liegt normalerweise im Home-Verzeichnis eines Users. Wenn sie noch nicht existiert, kann man sie einfach anlegen.</p>
<p><br clear="all" /><br />
<strong>Visual Bell abschalten</strong><br />
Wer ein wenig mit Screen gearbeitet hat, dem ist aufgefallen, dass hin und wieder der ganze Bildschirm neu gezeichnet wird, was sich durch ein kurzes Flackern äußert. Dies ist die Visual Bell (quasi als Ersatz für einen Piepton). Da diese häufig stört kann man mit der Tastenkombination Strg+a Strg+g zwischen visuell und audio hin und her schalten. Um sie dauerhaft abzuschalten kann man folgende Zeile in die ~/.screenrc schreiben:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">vbell off</pre></div></div>

<p><br clear="all" /><br />
<strong>Statusleiste von Screen einrichten</strong><br />
Ohne die Statusleiste macht die Arbeit mit Screen keinen Spass. Darum sollte man sie auf jeden Fall einrichten.<br />
In der ~/.screenrc werden 3 Zeilen angelegt. Die ersten beiden sind immer nötig und schalten die Hardline des Terminals ein und geben an, dass sie in der untersten Zeile stehen soll:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">hardstatus on
hardstatus alwayslastline</pre></div></div>

<p>Die Letzte Zeile bestimmt, die Farben und was alles wo zu sehen sein soll. Hier einige Beispiele, die man sich einfach mal anschauen kann. Das Einrichten einer eigenen Statusleiste ist relativ umständlich. Mehr Infos dazu gibt es in der Manpage. (Jeweils immer nur eine Zeile ausprobieren):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">hardstatus string <span style="color: #ff0000;">&quot;%{=b}%-w%{=bru}%n %t%{-}%+w&quot;</span></pre></div></div>

<p>Die Fenster werden nebeneinander in den eingestellten Systemfarben dargestellt. das aktive Fenster ist invertiert.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">hardstatus string <span style="color: #ff0000;">&quot;%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %d.%m.%y %c:%s &quot;</span></pre></div></div>

<p>Blaue Leiste, das aktuelle Fenster ist rot hinterlegt. Rechts steht der Name des Computers sowie Datum und Uhrzeit.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">hardstatus string <span style="color: #ff0000;">&quot;%{rw} * | %H * <span style="color: #007800;">$LOGNAME</span> | %{bw}%c %D | %{-}%-Lw%{rw}%50&gt;%{rW}%n%f* %t %{-}%+Lw%&lt;&quot;</span></pre></div></div>

<p>Graue Leiste, links steht das Hostname und Username sowie die Uhrzeit und der Wochentag. Danach kommen die Fenster wobei das aktive weis hinterlegt ist.</p>
<p><br clear="all" /><br />
<strong>Was tun bei Screen in Screen?</strong><br />
Wenn man überall Screen benutzt, bekommt man oft das Problem, dass man plötzlich Screen in einem Fenster aufruft, in dem Screen schon läuft. Dies ist mir häufig bei ssh passiert. Man hat Screen geöffnet und loggt sich auf einem entfernten Rechner ein, auf dem man Screen startet (oder attached). Wenn man jetzt Screen-Befehle eingibt, wirken sie nur auf das äußere Screen. Für solche Fälle ist das Escape-Kommando Strg+a a gedacht. Dies leitet Strg+a an das innere Screenfenster weiter. So kann man z.B. mit Strg+a a d das innere Screenfenster detachen.</p>
<p><br clear="all" /><br />
<strong>Shortcuts zum Wechseln der Fenster</strong><br />
Statt Strg+a p und Strg+a n zu drücken um ins vorherige bzw. nächste Fenster zu gelangen, kann man sich auch Shortcuts definieren. Schreibt man die folgenden 2 Zeilen in die ~/.screenrc, so kann man mit F7 und F8 je ein Fenster zurück bzw.  vorwärts wechseln:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">bindkey <span style="color: #660033;">-k</span> k7 prev
bindkey <span style="color: #660033;">-k</span> k8 next</pre></div></div>

<p><br clear="all" /><br />
<strong>Ein Kommando aus Screen heraus in einem neuen Fenster öffnen</strong><br />
Manchmal ist es Praktisch, wenn man einen Befehl direkt in einem neuen Screenfenster starten kann statt erst ein neues Fenster anzulegen und dort den Befehl einzugeben.Dies kann man mit dem Befehl</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">screen</span> <span style="color: #660033;">-X</span> <span style="color: #c20cb9; font-weight: bold;">screen</span> <span style="color: #000000; font-weight: bold;">&lt;</span>befehl<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>erreichen. Um das ganze abzukürzen kann man sich einen Alias definieren. Dazu schreibt man folgende Zeile in die ~/.bashrc</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">s</span>=<span style="color: #ff0000;">'screen -X screen'</span>;</pre></div></div>

<p>Im der nächsten geöffneten Konsole (oder nachdem man die ~/.bashrc mittels <em>source ~/.bashrc</em> neu geladen hat) kann man nun z.B. mittels</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">s top</pre></div></div>

<p><em>top</em> in einem neuen Screenfenster starten. Wenn das Programm geschlossen wird, schließt sich automatisch auch das geöffnete Screenfenster.</p>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/einige-tipps-und-tricks-fur-screen-teil-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>In der Konsole suchen mit ls, grep, find und locate</title>
		<link>http://zinformatik.de/linux/in-der-konsole-suchen-mit-ls-grep-find-und-locate/</link>
		<comments>http://zinformatik.de/linux/in-der-konsole-suchen-mit-ls-grep-find-und-locate/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 10:10:22 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[Konsole]]></category>
		<category><![CDATA[locate]]></category>
		<category><![CDATA[Suchen]]></category>
		<category><![CDATA[tracker]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=966</guid>
		<description><![CDATA[<p>Da ich ein Fan der Linux-Konsole bin, möchte ich einmal das Thema Suche ansprechen.<br />
Es gibt eine ganze Menge Möglichkeiten, wie man Dateien suchen kann.</p>
<p>Der Tracker ist eine praktische Suche, keine Frage. Aber in manchen Fällen können Shell-Kommandos auch recht hilfreich sein. So kann man mit ihnen z.B. nicht-indizierte Wechselmedien durchsuchen oder Dateien mit bestimmten Eigenschaften finden.<br />&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Da ich ein Fan der Linux-Konsole bin, möchte ich einmal das Thema Suche ansprechen.<br />
Es gibt eine ganze Menge Möglichkeiten, wie man Dateien suchen kann.</p>
<p>Der Tracker ist eine praktische Suche, keine Frage. Aber in manchen Fällen können Shell-Kommandos auch recht hilfreich sein. So kann man mit ihnen z.B. nicht-indizierte Wechselmedien durchsuchen oder Dateien mit bestimmten Eigenschaften finden.<br />
Die vorgestellten Kommandos, die ohne Indexierung funktionieren sollten nicht unüberlegt im Wurzelverzeichnis oder in anderen Verzeichnissen mit sehr vielen Datien und Ordnern genutzt werden, da sie teilweise sehr lange brauchen und viel Last erzeugen können.</p>
<p><strong>Suchkommandos ohne Indexierung</strong></p>
<p><strong>ls und grep</strong></p>
<p>Das Kommando ls ist wohl bekannt, daher hier nur ein paar interessante Optionen, die vielleicht nicht so häufig genutzt werden:</p>
<ul>
<li> -R	Listet rekursiv auch die Dateien der Unterverzeichnisse mit auf</li>
<li>-X	Sortiert die Dateien nach ihrem Dateityp (also z.B. erst css, dann alle html-Dateien und danach erst die png-Dateien)</li>
<li>-S	Sortiert die Dateien nach ihrer Dateigröße</li>
<li>-t	Sortiert die Dateien nach ihrer letzen Änderung</li>
<li>-lh	Zeigt die Dateigrößen in einem lesbaren Format an.</li>
</ul>
<p>Zusammen mit dem grep Kommando kann man nun schon recht gut einzelne Verzeichnisse und (mit der -R Option von ls) deren Unterverzeichnisse durchsuchen.<br />
So listet ein</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-R</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> png$</pre></div></div>

<p>alle PNG-Dateien auf. Um genauer zu sein: alle Dateien, die mit png enden also auch eine Datei wie z.B. test.apng<br />
Das Dollar-Zeichen zeigt an, dass der Dateiname dort aufhören soll. Das ^ hingegen zeigt an, dass der Dateiname so beginnen soll: ^test meint alle Dateien die mit &#8220;test&#8221; beginnen. Der Punkt steht für ein beliebiges Zeichen, der Asterisk (Stern *) für beliebig viele Wiederholungen des letzten Zeichens. Doch dies soll keine Einführung in reguläre Ausdrücke werden. Diese sind in der <a href="http://de.wikipedia.org/wiki/Regul%C3%A4rer_Ausdruck" title="Wikipedia - regulärer Ausdruck" target="_blank" rel="nofollow" class="liwikipedia">Wikipedia</a> recht gut beschrieben. (Vielleicht schreib ich auch irgendwann mal was darüber)<br />
Ohne $ werden Dateien angezeigt, die die Zeichenkette &#8220;png&#8221; im Dateinamen beinhalten.</p>
<p>Praktische Optionen für grep sind</p>
<ul>
<li> -i	schaltet die Unterscheidung zwischen Groß- und Kleinschreibung aus</li>
<li>-E	lässt reguläre Ausdrücke nach POSIX standard zu. Statt dessen kann auch egrep benutzt werden.</li>
<li>-r	durchsucht auch die Unterverzeichnisse rekursiv. Statt dessen kann auch rgrep benutzt werden.</li>
</ul>
<p>Mit grep kann man aber auch Dateien nach ihrem Inhalt (durch-) suchen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> hallo <span style="color: #000000; font-weight: bold;">*</span></pre></div></div>

<p>Listet alle Dateien im aktuellen Verzeichnis auf, in denen die Zeichenkette &#8220;hallo&#8221; vorkommt und gibt die entsprechenden Zeilen aus.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-r</span> hallo <span style="color: #000000; font-weight: bold;">*</span></pre></div></div>

<p>macht das gleiche rekursiv auch für alle Unterverzeichnisse. Auch hier kann die gesuchte Zeichenkette (in diesem Beispiel &#8220;hallo&#8221;) durch einen regulären Ausdruck beschrieben werden. Der Asterisk gibt an, dass in allen Dateien gesucht werden soll. Hier kann man praktische Einschränkungen machen, wenn z.B. nur in C-Quelltexten gesucht werden soll:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> hallo <span style="color: #000000; font-weight: bold;">*</span>.c</pre></div></div>

<p>Bei der Angabe der Datei gelten keine regulären Ausdrücke sondern die Regeln der Bash. Wie bei</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #000000; font-weight: bold;">*</span>.c</pre></div></div>

<p>*.c entspricht nebenbei bemerkt einer Liste aller Dateinamen, die mit .c enden und durch Leerzeichen getrennt sind.</p>
<p><strong>find</strong></p>
<p>Ein weiteres sehr mächtiges Werkzeug, dass ohne Indexierung auskommt ist find.<br />
Mit find kann man z.B. zuletzt geänderte oder geöffnete Dateien suchen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-amin</span> <span style="color: #660033;">-15</span></pre></div></div>

<p>listet alle Dateien im aktuellen Ordner und allen Unterordnern auf, die vor 15 Minuten oder kürzer geöffnet wurden. -15 bedeutet &#8220;kürzer als&#8221;, +10 bedeutet &#8220;länger als&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-mmin</span> <span style="color: #660033;">-20</span></pre></div></div>

<p>hingegen listet alle Dateien auf, vor weniger als 20 Minuten geändert wurden.<br />
Statt eine Zahl in Minuten anzugeben, kann man auch eine Datei angeben, deren Änderungszeitpunkt als Grenze genommen wird:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-newer</span> datei.txt</pre></div></div>

<p>listet alle Dateien auf, deren Änderung neuer sind als die der Datei datei.txt<br />
Statt amin und mmin kann man auch atime und mtime benutzen, um die Zeit in Tagen angeben zu können. Ein Tag ist dann als 24 Stunden definiert. Das bedeutet</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-mtime</span> <span style="color: #660033;">-2</span></pre></div></div>

<p>findet Dateien die jünger als 48 Stunden sind.<br />
Merkhilfe: atime = access time, mtime = modify time, amin = access (time in) minutes, mmin = modify (time in) minutes</p>
<p>Man kann auch große Dateien finden, die viel Speicher belegen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-size</span> +1G</pre></div></div>

<p>sucht alle Dateien, die Größer als 1 Gigabyte sind. Statt G für Gigabyte kann auch M für Megabyte oder k für Kilobyte verwendet werden.<br />
So sucht</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-size</span> <span style="color: #660033;">-1k</span></pre></div></div>

<p>alle Dateien kleiner 1 Kilobyte.</p>
<p>Praktisch ist auch die Suche nach leeren Dateien mittels</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-empty</span></pre></div></div>

<p>Schließlich kann man mit find auch nach Dateinamen suchen mittels:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*.png&quot;</span></pre></div></div>

<p>bzw. wenn Groß- und Kleinschreibung ignoriert werden soll:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-iname</span> <span style="color: #ff0000;">&quot;*.png&quot;</span></pre></div></div>

<p>Die Anführungszeichen werden benötigt, damit nicht eine Liste der png-Dateien an find übergeben wird sondern genau diese Zeichenkette.<br />
Die maximale Tiefe, die find in Unterverzeichnisse vordringen soll kann mittels -maxdepth begrenzt werden. So sucht</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-maxdepth</span> <span style="color: #000000;">1</span> <span style="color: #660033;">-emtpy</span></pre></div></div>

<p>nach Dateien im aktuellen Verzeichnis und allen Verzeichnissen darin. Jedoch nicht in Verzeichnissen, die in Unterverzeichnissen des aktuellen Ordners liegen.</p>
<p>Auch der Typ einer Datei kann bestimm werden. Um Verzeichnisse von den Suchergebnissen auszuschließen und nur Dateien zu suchen nutzt man die Option -type f. Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;test*&quot;</span> <span style="color: #660033;">-type</span> f</pre></div></div>

<p>Weitere Typen sind d für Verzeichnisse und l für symbolische Links</p>
<p>Man kann die gefundenen Dateien auch direkt Aktionen unterziehen.<br />
So löscht man mittels -delete die gefundenen Dateien.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-emtpy</span> <span style="color: #660033;">-delete</span></pre></div></div>

<p>löscht alle leeren Dateien<br />
Es können auch Befehle auf die gefundenen Dateien angewendet werden. So kann man dies auch gut mit grep verbinden um die gefundenen Dateien direkt zu durchsuchen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*.txt&quot;</span> <span style="color: #660033;">-exec</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-Hn</span> hallo <span style="color: #ff0000;">&quot;{}&quot;</span> \;</pre></div></div>

<p>durchsucht alle gefundenen Textdateien nach dem String &#8220;hallo&#8221;. Die Optionen <em>-H</em> und <em>-n</em> (kurz <em>-Hn</em>) bringen grep dazu die Dateinamen und Zeilennummern auszugeben, wie es bei einem normalen <em>grep hallo *</em> passiert (also wenn grep mehrere Dateien durchsuchen soll). Bei der Verwendung mit <em>find</em> muss dies explizit angegeben werden da sonst nur die gefundenen Zeilen ausgegeben werden. Das liegt daran, dass grep für jede Datei einzeln aufgerufen wird.</p>
<p><strong>Suchbefehle mit Index</strong></p>
<p>In der Konsole gibt es auch Suchkommandos, die einen Index besitzen.<br />
<strong>whereis</strong><br />
So kann man mit dem Kommando whereis nach Binarys und Manualpages suchen. Recht praktisch, wenn man wissen möchte, wo ein bestimmtes Programm liegt um z.B. darauf zu verlinken.<br />
Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">whereis</span> gvim</pre></div></div>

<p>Sucht die Ausführbare Datei von gvim</p>
<p><strong>locate</strong><br />
Mit locate lassen sich durch den verwendeten Index Dateien nach Dateinamen wesentlich schneller suchen als mit den bisher beschrieben Methoden. Dafür aber nur bereits indexierte Dateien, also keine Wechselspeichermedien oder gerade erstellte Dateien. So finde man mit</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">locate</span> <span style="color: #000000; font-weight: bold;">*</span>.tex</pre></div></div>

<p>alle LaTeX Dateien im gesammten System. (Auch hier kann man die Option -i verwenden um Groß- und Kleinschreibung zu ignorieren.)<br />
Bei Ubuntu wird die Datenbank täglich aktualisiert. Um sie manuell zu updaten kann man den folgenden Befehl benutzen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">updatedb</span></pre></div></div>

<p>Auch locate kann man mit grep verbinden um die gefundenen Dateien zu durchsuchen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">locate</span> <span style="color: #000000; font-weight: bold;">*</span>.tex <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> hallo</pre></div></div>

<p>durchucht alle LaTeX (.tex) Dateien im System nach dem String &#8220;hallo&#8221;.</p>
<p><strong>tracker</strong><br />
Natürlich kann man auch unter der Konsole den Tracker nutzen. Dafür installiert man unter Ubuntu die Tracker-Utils mittels</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> tracker-utils</pre></div></div>

<p>und kann dann wie gewohnt mit dem Tracker über den Befehl tracker-search suchen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">tracker-search hallo welt</pre></div></div>

<p>sucht nach Dateien, die die Wörter &#8220;hallo&#8221; und &#8220;welt&#8221; beinhalten.<br />
Wenn man den Tracker öfters benutzt, empfiehlt es sich dafür einen alias zu legen. Man kann z.B. in der Datei ~/.bashrc folgende Zeile anhängen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">ts</span>=tracker-search</pre></div></div>

<p>Dann kann man (in einem neuen Konsolenfenster, damit die ~/.bashrc ausgeführt wird) mit</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ts hallo</pre></div></div>

<p>nach &#8220;hallo&#8221; suchen</p>
<p>Der wesentliche Unterschied zwischen Tracker und locate ist, dass locate nur nach Dateinamen sucht während Tracker auch den Inhalt der Datei durchsucht. Locate arbeitet Systemweit und Tracker normalerweise nur in ausgewählten Verzeichnissen (die in der Konfigurationsdatei ~/.Tracker/tracker.cfg angegeben werden).</p>
<p>Weitere Informationen zu allen Kommandos findet man in den man-Pages (die Manualseiten) indem man z.B. für <em>find</em></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">man</span> <span style="color: #c20cb9; font-weight: bold;">find</span></pre></div></div>

<p>eingibt. Auch das <a href="http://wiki.ubuntuusers.de/Shell/Befehls%C3%BCbersicht" title="Ubuntuusers-Wiki" target="_blank" class="liuu">Ubuntuusers-Wiki</a> enthält zu den hier verwendeten Kommandos entsprechende Artikel.</p>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/in-der-konsole-suchen-mit-ls-grep-find-und-locate/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>commandlinefu &#8211; die besten Einzeiler auf der Konsole</title>
		<link>http://zinformatik.de/linux/commandlinefu-die-besten-einzeiler-auf-der-konsole/</link>
		<comments>http://zinformatik.de/linux/commandlinefu-die-besten-einzeiler-auf-der-konsole/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 21:05:14 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Webseiten]]></category>
		<category><![CDATA[commandlinefu]]></category>
		<category><![CDATA[Konsole]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=988</guid>
		<description><![CDATA[<p>Auf der Seite <a href="http://commandlinefu.com" title="commandlinefu" target="_blank" class="liexternal">http://commandlinefu.com</a> (englisch) werden einzeilige Kommandos (bzw. Kommandoketten) für die Shell und häufige Programme (vim, mysql, screen,&#8230;) gesammelt.</p>
<p>Dort gibt es so ziemlich Alles vom kurzen</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> -</pre></div></div>

<p>mit dem man zum letzten Verzeichnis wechselt bis zu recht langen und komplizierten Kommandos wie</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/sshd/ &#38;&#38; /Failed/ {gsub(/invalid user/,&#34;&#34;); printf</span></pre></div></div><p>&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Auf der Seite <a href="http://commandlinefu.com" title="commandlinefu" target="_blank" class="liexternal">http://commandlinefu.com</a> (englisch) werden einzeilige Kommandos (bzw. Kommandoketten) für die Shell und häufige Programme (vim, mysql, screen,&#8230;) gesammelt.</p>
<p>Dort gibt es so ziemlich Alles vom kurzen</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> -</pre></div></div>

<p>mit dem man zum letzten Verzeichnis wechselt bis zu recht langen und komplizierten Kommandos wie</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/sshd/ &amp;&amp; /Failed/ {gsub(/invalid user/,&quot;&quot;); printf &quot;%-12s %-16s %s-%s-%s\n&quot;, $9, $11, $1, $2, $3}'</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>auth.log</pre></div></div>

<p>mit dem man sich die Usernamen, IP-Adressen und Zeiten von fehlgeschlagenen SSH-Zugriffen anzeigen lassen kann.</p>
<p>Bei solch komplexen Befehlen ist es praktisch, sie sich als Script zu speichern:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;!!&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>my<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>script.sh</pre></div></div>

<p>speichert den letzten ausgeführten Befehl in die Datei script.sh im Verzeichnis /path/to/my/bin<br />
Mit &lt;Alt&gt;+.  lässt sich das letzte Argument in den nächsten Befehl einfügen, so dass</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">&lt;</span>Alt<span style="color: #000000; font-weight: bold;">&gt;</span>+.</pre></div></div>

<p>zu</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>my<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>script.sh</pre></div></div>

<p>wird und man so das erstellte Script auch direkt ausführbar machen kann. Ich habe mir ein eigenes bin Verzeichnis in meinem Homeverzeichnis erstellt und es in der .bashrc zu $PATH hinzugefügt. Also einfach folgende Zeile hinzufügen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #007800;">$PATH</span>:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>bin</pre></div></div>

<p>Weitere praktische Kommandos sind</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> filename<span style="color: #7a0874; font-weight: bold;">&#123;</span>,.bak<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>um ein Backup einer Datei anzulegen oder</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">!!</span></pre></div></div>

<p>was das letzte Kommando mit sudo ausführt, falls man dies beim ersten mal vergessen hat. (Wie man sieht steht !! für das letzte Kommando)</p>
<p>Mit</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-nrk</span> <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span></pre></div></div>

<p>kann man sich die laufenden Prozesse sortiert nach ihrem Speicherverbrauch anzeigen lassen.</p>
<p>Ziemlich praktisch ist auch</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-q</span> <span style="color: #660033;">-O</span> - <span style="color: #ff0000;">&quot;http://www.tarball.com/tarball.gz&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> zxvf -</pre></div></div>

<p>mit dem man eine .tar.gz Datei herunterladen und gleichzeitig entpacken kann ohne die Datei speichern zu müssen.</p>
<p>Auf der Seite gibt es noch haufenweise andere solch praktische Einzeiler. Natürlich sind auch einige dabei die nicht so sehr viel Sinn ergeben, aber dafür kann jeder ein Kommando bewerten und kommentieren. Man kann auch suchen sowie Tags nutzen. Zusätzlich werden automatisch alle verwendeten Funktionen in einer extra Tagcloud aufbereitet, so dass man auch Kommandos findet die nicht oder schlecht getagt sind.<br />
Somit ist die Seite wunderbar zum Stöbern, aber auch zum gezielten Suchen von Befehlen geeignet.</p>
<p>Also los, trainiere Dein Commandline-Fu <img src='http://zinformatik.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/commandlinefu-die-besten-einzeiler-auf-der-konsole/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
