我在这个 bash 函数中做错了什么?
我正在尝试创建一个转到特定目录的函数,如果指定了单个参数,则将其添加到目录字符串中。但无论我如何尝试,我都无法让它工作,并且 bash 总是报告 X 不是目录,即使我将其复制/粘贴到新的命令行,它也可以从那里开始工作。
function projects ()
{
DIR=
if [ -n "$1" ]
then
DIR=$1
fi
echo "~/Projects/Working Branches/$DIR" | sed 's/ /\\&/g' | xargs cd
}
-
$ projects Parametric\ Search\ API
/usr/bin/cd: line 4: cd: ~/Projects/Working Branches/Parametric Search API: No such file or directory
$ projects "Parametric Search API"
/usr/bin/cd: line 4: cd: ~/Projects/Working Branches/Parametric Search API: No such file or directory
我还尝试将 sed regexp 修改为 s/ /\\\\\\&/g
,这会生成看似正确转义的字符串,但会导致完全相同的
$ projects Parametric\ Search\ API
/usr/bin/cd: line 4: cd: ~/Projects/Working\ Branches/Parametric\ Search\ API: No such file or directory
$ projects "Parametric Search API"
/usr/bin/cd: line 4: cd: ~/Projects/Working\ Branches/Parametric\ Search\ API: No such file or directory
错误最后,我尝试用我能想到的各种引号组合包围 $DIR
,但到目前为止还没有运气。
I'm trying to create a function that goes to a specific directory, and if a single argument is specified add that to the directory string. But no matter what I try I can't get it to work and bash always reports that X is not a directory even though if I copy/paste it to a new command line it works from there.
function projects ()
{
DIR=
if [ -n "$1" ]
then
DIR=$1
fi
echo "~/Projects/Working Branches/$DIR" | sed 's/ /\\&/g' | xargs cd
}
-
$ projects Parametric\ Search\ API
/usr/bin/cd: line 4: cd: ~/Projects/Working Branches/Parametric Search API: No such file or directory
$ projects "Parametric Search API"
/usr/bin/cd: line 4: cd: ~/Projects/Working Branches/Parametric Search API: No such file or directory
I've also tried modifying the sed regexp to s/ /\\\\\\&/g
which produces what appears to be a properly escaped string, but results in the same exact error
$ projects Parametric\ Search\ API
/usr/bin/cd: line 4: cd: ~/Projects/Working\ Branches/Parametric\ Search\ API: No such file or directory
$ projects "Parametric Search API"
/usr/bin/cd: line 4: cd: ~/Projects/Working\ Branches/Parametric\ Search\ API: No such file or directory
An finally I've tried surrounding $DIR
with every combination of quotes I can think of, but no luck so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因此,一个问题是,如果您引用其中包含
~
的路径,shell 在将其传递到cd
之前不会扩展该路径。您可以通过使用$HOME
来解决此问题,或者将~/
放在引号之外。另外,您不需要根据
$1
有条件地设置$DIR
,也不需要使用sed
和xargs< /代码>。只需在路径中直接使用
$1
即可;如果它为空,则该目录组件将为空,只留下Working Branches
目录作为cd
的参数。简单地
应该可以工作,稍微短但不太清晰的也可以
So, one problem is that if you quote a path with
~
in it, the shell will not expand that before passing it intocd
. You can fix this by using$HOME
instead, or by putting the~/
outside of the quotes.Also, you don't need to conditionally set
$DIR
based on$1
, nor do you need to usesed
andxargs
. Just use$1
directly in your path; if it's empty, that directory component will be empty, leaving you with just theWorking Branches
directory as an argument tocd
.Simply
should work, as would the slightly shorter but less clear
问题是波浪线扩展是由 shell (bash) 执行的,并且 xargs、cd 和操作系统都不会执行此操作。这就是为什么包含 ~ 的目录是未知的。您可以使用 $HOME 代替 ~,同时将被双引号之间的工作目录替换,或者将波形符放在引号之外(即 ~/"...")。
此外,使用 xargs 调用 cd 不会更改工作目录,因为 cd 在单独的进程中运行,请考虑仅使用 cd ~/"Projects/Working Branches/" 代替。
The problem is that the tilde expansion is performed by the shell (bash) and that neither xargs nor cd nor the OS will do this. This is why the directory containing ~ is unknown. You could instead use $HOME instead of ~ whil will be replaced by the working directory between double quotes, or place the tilde outside the quotes (i.e., ~/"...").
Furthermore, calling cd with xargs will not change the working directory since cd is run in a separate process, consider just using cd ~/"Projects/Working Branches/" instead.
也许你对此进行了过度设计:
不过,我并没有真正理解 sed 命令的目的,所以也许我遗漏了一些东西。
Maybe you're over-engineering this:
I don't really get the purpose of the
sed
command, though, so maybe I'm missing something.xargs
和cd
在我的 bash 中不能一起工作。我认为这是因为 xargs 想要一个应用程序,而 cd 是 bash shell 中的内置函数。尝试 echo“项目”| xargs cd 在您的主目录中以查看是否是这种情况。
另一个问题是
~
不会像您想象的那样通过 echo 进行扩展。尝试改用完整路径。或者使用反引号展开~
xargs
andcd
does not work together in my bash. I think it's becausexargs
want an application andcd
is a built-in function in the bash shell.try
echo "Projects" | xargs cd
in your home directory to se if this is the case.Another problem is that
~
doesn't get expanded as you think by echo. Try using the full path instead. Or use backticks to expand the~