[英]Bash alias utilizing the command inputs
I want to create a bash alias to do the following: 我想创建一个bash别名来执行以下操作:
Assume I am at the following path: 假设我位于以下路径:
/dir1/dir2/dir3/...../dirN
I want to go up to dir3 directly without using cd ..
. 我想不使用
cd ..
就直接上dir3。 I will just write cdd dir3
and it should go directly to /dir1/dir2/dir3
. 我将只写
cdd dir3
,它应该直接转到/dir1/dir2/dir3
。 cdd
is my alias name. cdd
是我的别名。
I wrote the following alias, but it doesn't work: 我写了以下别名,但是不起作用:
alias cdd='export newDir=$1; export myPath=`pwd | sed "s/\/$newDir\/.*/\/$newDir/"`; cd $myPath'
Simply it should get the current full path, then remove anything after the new destination directory, then cd to this new path 简单地说,它应该获取当前的完整路径,然后在新的目标目录之后删除所有内容,然后cd到该新路径
The problem with my command is that $1
doesn't get my input to the command cdd
我的命令的问题是
$1
无法获得我对命令cdd
输入
This is a slightly simpler function that I think achieves what you're trying to do: 我认为这是一个稍微简单的功能,可以实现您要执行的操作:
cdd() { cd ${PWD/$1*}$1; }
Explanation: 说明:
${PWD/$1*}$1
takes the current working directory and strips off everything after the string passed to it (the target directory), then adds that string back. ${PWD/$1*}$1
获取当前工作目录,并在将字符串传递给目标目录之后剥离所有内容,然后将该字符串添加回去。 This is then used as an argument for cd
. 然后将其用作
cd
的参数。 I didn't bother adding any error handling as cd
will take care of that itself. 我没有理会添加任何错误处理,因为
cd
会自行处理。
Example: 例:
[atticus:pgl]:~/tmp/a/b/c/d/e/f $ cdd b
[atticus:pgl]:~/tmp/a/b $
It's a little ugly, but it works. 这有点丑陋,但有效。
Here's a function - which you could place in your shell profile - which does what you want; 这是一个函数-您可以将其放置在shell配置文件中-它可以完成您想要的; note that in addition to directory names it also supports levels (eg,
cdd 2
to go up 2 levels in the hierarchy); 请注意,除了目录名称外 ,它还支持级别 (例如,
cdd 2
在层次结构中上升了2级); just using cdd
will move up to the parent directory. 仅使用
cdd
就会上移到父目录。
Also note that matching is case-INsensitive. 另请注意,匹配不区分大小写。
The code is taken from " How can I replace a command line argument with tab completion? ", where you'll also find a way to add complementary tab-completion for ancestral directory names. 该代码取自“ 如何用制表符替换命令行参数? ”,您还将在其中找到一种方法来为原始目录名添加互补的制表符补全。
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
}
I agree with mklement0, this should be a function. 我同意mklement0,这应该是一个函数。 But a simpler one.
但是比较简单。
Add this to your .bashrc
: 将此添加到您的
.bashrc
:
cdd () {
newDir="${PWD%%$1*}$1"
if [ ! -d "$newDir" ]; then
echo "cdd: $1: No such file or directory" >&2
return 1
fi
cd "${newDir}"
}
Note that if $1
(your search string) appears more than once in the path, this function will prefer the first one. 请注意,如果
$1
(您的搜索字符串)在路径中出现多次,则此函数将首选第一个。 Note also that if $1
is a substring of a path, it will not be found. 还要注意,如果
$1
是路径的子字符串,则不会找到它。 For example: 例如:
[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
If you'd like to include the functionality of going up 2 levels by running cdd 2
, this might work: 如果您想通过运行
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}"
}
The long if
statement verifies that you've supplied an integer that is not itself a directory. long
if
语句验证您提供的整数本身不是目录。 We build a new $newDir
so that you can cd -
to get back to your original location if you want. 我们构建了一个新的
$newDir
以便您可以进行cd -
如果需要,可以返回到原始位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.