background preloader

Bash

Facebook Twitter

Linux Commands - A practical reference. BashPitfalls. This page shows common errors that Bash programmers make. The following examples are all flawed in some way: 1. for i in $(ls *.mp3) One of the most common mistakes BASH programmers make is to write a loop like this: for i in $(ls *.mp3); do # Wrong! Never use a CommandSubstitution -- of EITHER kind! Why? Possibly worse, the strings that resulted from the previous word splitting step will then undergo pathname expansion. You can't double-quote the substitution either: for i in "$(ls *.mp3)"; do # Wrong! This causes the entire output of ls to be treated as a single word. In addition to this, the use of ls is just plain unnecessary. For i in *.mp3; do # Better! POSIX shells such as Bash have the globbing feature specifically for this purpose -- to allow the shell to expand patterns into a list of matching filenames.

Question: What happens if there are no *.mp3-files in the current directory? # POSIX for i in *.mp3; do [ -e "$i" ] || continue some command "$i" done 2. cp $file $target 3. Nützliche, einzeilige Scripts für SED (Unix Stream Editor) ADMIN-Tipp: Überraschendes cd - ADMIN | Das plattformübergreifende Magazin für alle IT-Administratoren. Wer hatte nicht schon mal den Finger auf der falschen Taste? Jcb@hercules:~$ cd /use/local bash: cd: /use/local: Datei oder Verzeichnis nicht gefunden Ups, müsste wohl "usr" heißen. Also neue Zeile, neues Glück.

Das muss aber gar nicht sein. Der Einzeiler shopt -s cdspell - den man beispielsweise günstig in .bashrc platzieren kann - bewirkt, dass die Shell den Vertipper stillschweigend korrigiert und so tut, als sei nichts gewesen: jcb@hercules:~$ cd /use/local /usr/local Manchmal muss man mehrfach zwischen verschiedenen Verzeichnissen wechseln. Jcb@hercules:/etc/init.d$ cd ~/Apps/Apache2/conf/ jcb@hercules:~/Apps/Apache2/conf$ pushd . ... jcb@hercules:~/Apps/Apache2/conf$ cd /etc/init.d ... jcb@hercules:/etc/init.d$ popd ~/Apps/Apache2/conf jcb@hercules:~/Apps/Apache2/conf$ Das Kommando pushd . hat das aktuelle Verzeichnis auf einen Stack geschoben (den man mit dirs betrachten kann) und wovon popd das oberste Element beim Rückweg als Sprungziel benutzt.

Alias .. und so weiter. Bash Cookbook | Main / HomePage browse. Greetings! Carl, JP, and Cameron are happy to bring you this resource for bash users of all backgrounds. Here you can find on-line resources for bash and related topics, as well as information about our book, "bash Cookbook", part of the O'Reilly cookbook series. Helpful Documents New to bash? You might want to try this introduction to bash. A Pro? What is bash? The bash shell is powerful software for Linux, Unix, and other systems. Great bash Video! Our Book Gets Reviews We've collected some book reviews on the bash Cookbook like this one Click on that image or here for more reviews. We Review Other Books Want to know more about Linux? Click on that image or here for the review. Learn Bash -- Take a Course On-Line There's a free online course that uses "bash Cookbook" as its textbook. Cygwin. Using the Bash IFS variable to make for loops split with non whitespace characters | mindspill.net.

A basic for loop The Bash for loop splits using a whitespace (space, tab or newline). This allows you to easily iterate over a glob of values, as follows (this particular example uses a glob of filenames, taken from a backup script that requires a list of files to exclude from the backup): Script: #! /bin/bash vals='/mnt /dev /proc /sys /tmp /usr/portage /var/tmp' for i in $vals; do echo $i; done Output: /mnt /dev /proc /sys /tmp /usr/portage /var/tmp The problem A problem arises when one of the values in the glob needs to contain a space.

. #! /mnt /var/lib/vmware/Virtual Machines /dev /proc /sys /tmp /usr/portage /var/tmp "/var/lib/vmware/Virtual Machines" is split because it contains a space, which would obviously be a major problem if you were attempting to do something with each file location rather than just echoing it. The IFS internal variable #! It's best to unset IFS after you've done your work, with unset IFS, so that it returns to its default value. References. How to escape white space in bash loop list.

Awk

Colors and Prompts in BASH. Mar 2007 Bourne Again Shell offers a lot of power, flexibility and fun. Many new Unix users do not realize the flexibility of the shell environment; indeed; many new Unix users regard the shell as primitive and too restricted: nothing could be further from the truth. With very little time investment a new Unix user can learn how not to just make their work environment in the shell more productive but even a little fun. This text only discusses the bash shell and the latest versions of the bash shell. In the old days (a few years ago...) shell prompts did not all act alike. The Prompt First and foremost to (most) users the shell prompt itself; there are 3 approaches to the shell prompt (in no particular order) for those who want to customize it: Put pwd in the prompt (do not care about the rest).A very sexy/l33t prompt (do not care about the rest).A nice prompt and pwd crammed in there. Addressing each point is easiest... The basic method of changing a prompt is by modifying the PS1 variable:

All commands | commandlinefu.com. Shells: Kommandos und Programmierung. In diesem Artikel behandeln wir an Hand vieler Beispiele die Ein- und Ausgabeumlenkung. Am Ende des Abschnitts betrachten wir noch ein häufig diskutiertes und vielfach nicht verstandenes Problem: Die Vertauschung der Standardausgabe (stdout) und der Standardfehlerausgabe (stderr) Wird ein Kommando in einer Shell ausgeführt, so erscheint die Ausgabe normalerweise im Terminal, in dem das Kommando aufgerufen wurde. Diese Ausgabe wird auch als Standardausgabe bezeichnet. Neben der Standardausgabe kennt Unix oder Linux noch zwei andere andere Standardkommunikationswege, auch Standard-Datenströme oder Kanäle genannt: In Linux können geöffnete Dateien sowohl über ihren Namen als auch über sogenannte Deskriptoren angesprochen werden.

Diagnose- und Fehlermeldungen bzw. Die Standardeingabe, die Standardausgabe und die Standardfehlerausgabe kann man umlenken, d.h. man kann z.B. aus einer Datei statt von der Tastatur lesen oder in eine Datei statt auf das Terminal schreiben. Aufrufen können.