Twitter (was mache ich gerade?)

    Suche

     

    Credits

    Archiv

    April 2024
    Mo
    Di
    Mi
    Do
    Fr
    Sa
    So
     1 
     2 
     3 
     4 
     5 
     6 
     7 
     8 
     9 
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
     
     
     
     
     
     
     
     

    Status

    Online seit 6714 Tagen
    Zuletzt aktualisiert: 17. Apr, 22:03

    Counter & Co.

    Egoload - Verträumter Idealist
    Mein
Koordinaten auf der EgoMap:  93,2
    100% Heidnisch

    Locations of visitors to this page

    zsh

    Montag, 15. Dezember 2008

    permfix.zsh: Fix broken permissions

    Yesterday I messed up my NetBSD system while moving /usr and /var to a new, encrypted partition. I didn't reckon that NetBSDs tar needs "-p" to preserve the meta information attached to a directory or file, so I lost ownerships, groups and especially permissions. And as I discovered the mess I already had continue with setting up the system, installing ports, pkgsrc, /usr/src and so on...
    Luckily I didn't simply remove the original directories, but kept them as "/usr.old" and "/var.old". So I wrote a small script using zsh and the "zsh/stat" module to fix it. So in case something like this might happen to you, too, here's a simple solution:
     
    #!/usr/pkg/bin/zsh
    
    zmodload zsh/stat
    
    typeset srcdir dstdir 
    typeset -A srcobj dstobj
    typeset -i srcmode dstmode
    
    [[ $# != 2 ]] && {
        echo "Error: ${0:t} requires exactly two arguments: sourcedir and destdir"
        exit 1
    }  
    [[ ! -d $1 ]] && {
        echo "Error: $1 is not a directory or does not exist."
        exit 1
    }
    [[ ! -d $2 ]] && {
        echo "Error: $2 is not a directory or does not exist."
        exit 1
    }
    
    srcdir=$1
    dstdir=$2
    
    for obj in $( cd $srcdir ; find . )
    do
        [[ -e $dstdir/$obj ]] && {
            stat -oH srcobj $srcdir/$obj
            stat -oH dstobj $dstdir/$obj
            srcmode=${${srcobj[mode]}[4,7]}
            dstmode=${${dstobj[mode]}[4,7]}
            [[ $srcmode -ne $dstmode ]] && {
                echo "Changing $obj perm from $dstmode to $srcmode"
                chmod $srcmode $dstdir/$obj
            }
            [[ $srcobj[uid] != $dstobj[uid] ]] && {
                echo "Changing $obj owner from $dstobj[uid] to $srcobj[uid]"
                chown $srcobj[uid] $dstdir/$obj
            }
            [[ $srcobj[gid] != $dstobj[gid] ]] && {
                echo "Changing $obj group from $dstobj[gid] to $srcobj[gid]"
                chgrp $srcobj[gid] $dstdir/$obj
            }
        }
    done

     
    There might be a shorter solution, but I wanted to keep this simple and readable. ;-)

    Freitag, 3. August 2007

    I love zsh: Parameter Substitution

    I'm one of the guys who came across zsh, and use it since then. There has been an interesting thread about parameter substitution, with a summary of all the features that zsh provides.
    Check out "RE: Very useful !#^.
    It's great to be able to use parameters that have been supplied in the current line, or the line before. Just think about a long command. You press enter, and in the next line, you'll need a parameter again, such as a path name. With !:n, where n is a number that has to be substituted with the position number where the parameter is located.

    If you think "hey, what's this zsh stuff all about, there's bash!", check out the brilliant article The last shell you'll ever need.

    Zusammenfassung:
    Auf der zsh-Users Mailingliste kann man eine schicke Zusammenfassung der möglichen Parameter Substitutionen lesen. Für den Fall, dass man auf Copy+Paste keine Lust hat, aber mal aus der vorherigen, oder aktuellen Zeile, einen Parameter ("ein Wort") braucht.
    Sehr gut ist übrigens der Artikel The last shell you'll ever need, bei dem es Vorteile der zsh geht. Korrekturen stehen dann in den Kommentaren. ;-)

    Dienstag, 27. Februar 2007

    ZModules: stat

    Der Funktionsumfang der zsh kann durch Module erweitert werden. Aufgelistet sind diese in der manpage zu zshmodules (welche Überraschung).

    Eines davon ist das Module zsh/stat, das Zugriff auf den gleichnamigen Systemcall bietet:
    
    % zmodload zsh/stat
    % touch somefile
    % ls -l somefile
    -rw-r--r--  1 cptsalek  1001  0 Feb 27 22:16 somefile
    % stat somefile
    device  98
    inode   376964
    mode    33188
    nlink   1
    uid     1001
    gid     1001
    rdev    0
    size    0
    atime   1172610986
    mtime   1172610986
    ctime   1172610986
    blksize 4096
    blocks  0
    link
    
    Ok, das geht natürlich auch in einem menschenlesbaren Format:
    
    % stat -s somefile
    device  98
    inode   376964
    mode    -rw-r--r--
    nlink   1
    uid     cptsalek
    gid     1001
    rdev    0
    size    0
    atime   Tue Feb 27 22:16:26
    mtime   Tue Feb 27 22:16:26
    ctime   Tue Feb 27 22:16:26
    blksize 4096
    blocks  0
    link
    
    Yepp, mit stat kann man alle Daten, die zu einem File gehlören, in einem Rutsch auslesen, ohne mit ls, grep und Co. irgendwelche Orgien zu feiern. Und weils so praktisch ist, kann man das die Ausgabe auch in ein Hash eintüten:
    
    % stat -onH hash somefile
    % echo ${${hash[mode]}[4,7]}
    0644
    
    ...voila, womit man die Permissions erschlagen hätte.

    Muschelspaß

    Ich weiß garnicht mehr wie, aber vor einer Weile bin ich über die zsh gestolpert. Und seitdem ist diese höchst komfortable und genial programmierbare Shell zu meinem Standard geworden.
    Mäuschenschubser können an dieser Stelle übrigens aufhören zu lesen. ;-)

    Warum?
    Also da wäre z.B. die ZSH-Liebhaberseite.
    Eigentlich ist damit auch schon alles gesagt, meine Favoriten sind
    • Arrays
    • Assoziative Arrays ("Hashes")
    • Positionsparameter in Strings
    • Mächtige Parameter/Command/Argument-Expansion
    Hier mal einige Beispiele:
    
    typeset -a array
    array=( "${(f)$(ls -l)}" )
    
    Diese kleine Codezeile liest die Ausgabe des ls-Befehls in ein Array ein, und zwar Zeilenweise. Ein spezielles Konstrukt gibt es für Dateien, mit dem man auf cat verzichten kann:
    
    typeset -a array
    array=( "${(f)$(< dateiname)}" )
    
    Das funktioniert zwar hervorragend, entfernt aber alle Leerzeichen, die in der Datei vorkommen. So lange man das File nur lesen will ist das in Ordnung, wenn man es aber verarbeiten und die neue Fassung wieder ausgeben möchte, ist das ein K.O-Kriterium. Hier funktioniert dann sowas wie:
    
    typeset -a array
    < ldap_user_passwd while { read line } { content+=$line$'\n' }
    
    Die Konstruktion stammt von der zsh-Users Liste, und hat nach einem kurzen Test den Nachteil, dass hier an jede Zeile ein zusätzliches \n angehängt wird. Also:
    
    typeset -a array
    < ldap_user_passwd while read line
    do
      if [[ $array == "" ]] then
       array+=" "
      else
        array+=$line
      fi
    done
    
    Die if-then Geschichte kann man mit der zsh auch nett umgehen:
    
    [[ $array == "" ]] && { array+=" " } || { array+=$line }
    

    Wie gesagt kennt die zsh nicht nur Strings, sondern auch Arrays. Daher werden Strings auch anders verarbeitet. Die Zeichenkette
    string="Hallo Welt" wird z.B. in einer Schleife nicht in die Elemente "Hallo" und "Welt" zerlegt, wie das bei sh-kompatiblen Shells der Fall ist.
    Erst wenn man das mal verwendet hat um wie schon gesagt zeilenweise Dateien zu verarbeiten, merkt man, wie nützlich das ist.
    Apropos nützlich:
    
    for line in $array
    do
      [[ $line == *${suchwort}* ]] && { echo "Suchbegriff gefunden." }
    done
    
    Find ich genial... :-)
    Und wo wir gerade bei genial sind, hier ein Beispiel für die Positionsparameter:
    
    % typeset -a array
    % array=( Hallo Welt )
    % echo ${array[1]}
    Hallo
    % echo ${array[2]}
    Welt
    % echo ${${array[2]}[1]}
    W
    
    More to come. :-)

    Aktuelle Beiträge

    Im happy I finally registered
    Excellent write-up. I absolutely love this site. Continue...
    http://mittenmail3.isblog.net/pick-a-winner-via-hashtag-entry-for-twitter-and-instagram-woobox-bl... (Gast) - 17. Apr, 22:03
    Just want to say Hi!
    I all the time emailed this blog post page to all...
    how to get instagram likes (Gast) - 30. Mär, 23:20
    Im happy I now signed...
    Hello, There's no doubt that your website could possibly...
    buy instagram followers cheap (Gast) - 30. Mär, 09:27
    Hallo Miau!
    Ich will einen schönen Tag wünschen, mit unseren Schmusekatzen....
    SCHLAGLOCH - 4. Apr, 14:51
    Sehe hier nut Tag und...
    Wo ist das Jahr an dem der Text geschrieben wurde?
    Gast (Gast) - 3. Mär, 20:38

    User Status

    Du bist nicht angemeldet.

    ...wenn man trotzdem lacht
    Atari Mega ST
    Auf Arbeit
    Bloggen
    Bookmarks & Links
    BSD
    Bundeswehr
    CCC07
    Computing
    Contentmafia
    Datenschutz
    DRM
    Fahrrad
    Finanzkrise
    Fundsachen
    G8
    ... weitere
    Profil
    Abmelden
    Weblog abonnieren