Bash cdots: change directory functions .. ... .... etc mit tab-completion

F

fvu

Grünschnabel
Ich wollte Tastenanschläge speichern, indem ich zurück Abkürzungen für ändernde Verzeichnisse verwendete. Ich wollte auch ein zusätzliches Argument liefern: das Verzeichnis, zum von dort, mit Vorsprung-Beendigung weiter zu gehen:
(Mein deutsches entschuldigen… es wird übersetzt mit Hilfe von Google translate :-)

.. [dir] = cd ../[dir]
... [dir] = cd ../../[dir]
.... [dir] = cd ../../../[dir]
..... [dir] = cd ../../../../[dir]
...... [dir] = cd ../../../../../[dir]
....... [dir] = cd ../../../../../../[dir]
........ [dir] = cd ../../../../../../../[dir]

In der BNF-Darstellung:

..[.[.[.[.[.[.]]]]]] [dir]

Unter ist mein kurzes Programm - cdots.sh - ich möchte teilen.

See also: http://www.fvue.nl/cdots/

Freddy Vulto

Code:
#!/bin/bash
# --- cdots.sh -------------------------------------------------------
# Change directory back - 1-7 times - and forth with TAB-completion.
# Copyright (C) 2006  Freddy Vulto
# Version: 1.0.8
# Usage: ..[.[.[.[.[.[.]]]]]] [dir]
#
# Arguments: [dir]   Directory to go forth - down the directory tree
#
# Example:   $/usr/share> .. local/share/   # .. lo[TAB]/sh[TAB])
#            $/usr/local/share>  
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
# MA  02110-1301, USA
#
# The latest version of this software can be obtained here:
# [url]http://www.fvue.nl/cdots/[/url]


CDOTS_DEPTH=7


#--- _cdots() --------------------------------------------------------
# TAB completion for the .. ... .... etc commands
# @see cdots()
_cdots() {
        # ':2' = Ignore two dots at pos 0, $'\012' = newline (\n)
    local dots=${COMP_WORDS[COMP_CWORD-1]:2} IFS=$'\012' i j k=0
    local dir=${dots//./..\/}../  # Replace . with ../
    for j in $(compgen -d -- "$dir${COMP_WORDS[COMP_CWORD]}"); do
            #  If j not dir in current dir, append extra slash '/'
            #  NOTE: If j is also dir in current dir, 'complete -o 
            #+       filenames' automatically appends slash '/'
        [ ! -d ${j#$dir} ] && j="$j/"
        COMPREPLY[k++]="${j#$dir}"
    done
} # _cdots()


#--- cdots() ---------------------------------------------------------
# Change directory to specified directories back, and forth
# @param $1 string   Directory back
# @param $2 string   Directory forth
# @see _cdots() for TAB-completion
function cdots() {
    cd "$1$2"
} # cdots()


    # Define aliases .. ... .... etc
    # NOTE: Functions are not defined directly as .. ... .... etc, 
    #       because these are not valid identifiers under `POSIX'
cdotsAlias=.; cdotsAliases=; cdotsDir=
for ((i = 1; i <= $CDOTS_DEPTH; i++)); do
    cdotsAlias=$cdotsAlias.; cdotsDir=$cdotsDir../
    alias $cdotsAlias="cdots $cdotsDir"
    cdotsAliases="$cdotsAliases $cdotsAlias"
done
    # Set completion of aliases .. ... .... etc to _cdots()
    # -o filenames: Escapes whitespace
complete -o filenames -o nospace -F _cdots $cdotsAliases
unset -v CDOTS_DEPTH cdotsAlias cdotsAliases cdotsDir i
 
There are many english boards (and also for many other languages) for scripting and linux/unix problems available. Why you're using a german board if you need an online translator?

PS: Sorry for my english. It's not translated with 'Google translate' but it's although horrible. ;)
 

Ähnliche Themen

Zugriff Ubuntu 16.04. auf Freigabe 18.04. LTS nicht möglich

X startet nichtmehr

Samba 4 Gast Zugang unter Ubuntu funktioniert nicht

JBidWatcher: Problem bei loading Auctions in Verbindung mit mySQL

MacBook Pro hat Benutzer-Konten vergessen

Zurück
Oben