繁体   English   中英

使用命令输入的Bash别名

[英]Bash alias utilizing the command inputs

我想创建一个bash别名来执行以下操作:

假设我位于以下路径:

/dir1/dir2/dir3/...../dirN

我想不使用cd ..就直接上dir3。 我将只写cdd dir3 ,它应该直接转到/dir1/dir2/dir3 cdd是我的别名。

我写了以下别名,但是不起作用:

alias cdd='export newDir=$1; export myPath=`pwd | sed "s/\/$newDir\/.*/\/$newDir/"`; cd $myPath'

简单地说,它应该获取当前的完整路径,然后在新的目标目录之后删除所有内容,然后cd到该新路径

我的命令的问题是$1无法获得我对命令cdd输入

我认为这是一个稍微简单的功能,可以实现您要执行的操作:

cdd() { cd ${PWD/$1*}$1; }

说明:

${PWD/$1*}$1获取当前工作目录,并在将字符串传递给目标目录之后剥离所有内容,然后将该字符串添加回去。 然后将其用作cd的参数。 我没有理会添加任何错误处理,因为cd会自行处理。

例:

[atticus:pgl]:~/tmp/a/b/c/d/e/f $ cdd b
[atticus:pgl]:~/tmp/a/b $

这有点丑陋,但有效。

这是一个函数-您可以将其放置在shell配置文件中-它可以完成您想要的; 请注意,除了目录名称外 ,它还支持级别 (例如, cdd 2在层次结构中上升了2级); 仅使用cdd就会上移到父目录。

另请注意,匹配不区分大小写。

该代码取自“ 如何用制表符替换命令行参数? ”,您还将在其中找到一种方法来为原始目录名添加互补的制表符补全。

    cdd () 
    { 
        local dir='../';
        [[ "$1" == '-h' || "$1" == '--help' ]] && { 
            echo -e "usage:
        $FUNCNAME [n]
        $FUNCNAME dirname
      Moves up N levels in the path to the current working directory, 1 by default.
      If DIRNAME is given, it must be the full name of an ancestral directory (case does not matter).
      If there are multiple matches, the one *lowest* in the hierarchy is changed to." && return 0
        };
        if [[ -n "$1" ]]; then
            if [[ $1 =~ ^[0-9]+$ ]]; then
                local strpath=$( printf "%${1}s" );
                dir=${strpath// /$dir};
            else
                if [[ $1 =~ ^/ ]]; then
                    dir=$1;
                else
                    local wdLower=$(echo -n "$PWD" | tr '[:upper:]' '[:lower:]');
                    local tokenLower=$(echo -n "$1" | tr '[:upper:]' '[:lower:]');
                    local newParentDirLower=${wdLower%/$tokenLower/*};
                    [[ "$newParentDirLower" == "$wdLower" ]] && { 
                        echo "$FUNCNAME: No ancestral directory named '$1' found." 1>&2;
                        return 1
                    };
                    local targetDirPathLength=$(( ${#newParentDirLower} + 1 + ${#tokenLower} ));
                    dir=${PWD:0:$targetDirPathLength};
                fi;
            fi;
        fi;
        pushd "$dir" > /dev/null
    }

我同意mklement0,这应该是一个函数。 但是比较简单。

将此添加到您的.bashrc

cdd () {
  newDir="${PWD%%$1*}$1"
  if [ ! -d "$newDir" ]; then
    echo "cdd: $1: No such file or directory" >&2
    return 1
  fi
  cd "${newDir}"
}

请注意,如果$1 (您的搜索字符串)在路径中出现多次,则此函数将首选第一个。 还要注意,如果$1是路径的子字符串,则不会找到它。 例如:

[ghoti@pc ~]$ mkdir -p /tmp/foo/bar/baz/foo/one
[ghoti@pc ~]$ cd /tmp/foo/bar/baz/foo/one
[ghoti@pc /tmp/foo/bar/baz/foo/one]$ cdd foo
[ghoti@pc /tmp/foo]$ cd -
/tmp/foo/bar/baz/foo/one
[ghoti@pc /tmp/foo/bar/baz/foo/one]$ cdd fo
cdd: fo: No such file or directory

如果您想通过运行cdd 2来包括将功能提高2个级别的功能,则可能会起作用:

cdd () {
  newDir="${PWD%%$1*}$1"
  if [ "$1" -gt 0 -a "$1" = "${1%%.*}" -a ! -d "$1" ]; then
    newDir=""
    for _ in $(seq 1 $1); do
      newDir="../${newDir}"
    done
    cd $newDir
    return 0
  elif [ ! -d "$newDir" ]; then
    echo "cdd: $1: No such file or directory" >&2
    return 1
  fi
  cd "${newDir}"
}

long if语句验证您提供的整数本身不是目录。 我们构建了一个新的$newDir以便您可以进行cd -如果需要,可以返回到原始位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM