我在这个 bash 函数中做错了什么?

发布于 2024-09-24 16:36:54 字数 1059 浏览 2 评论 0原文

我正在尝试创建一个转到特定目录的函数,如果指定了单个参数,则将其添加到目录字符串中。但无论我如何尝试,我都无法让它工作,并且 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦冥 2024-10-01 16:36:54

因此,一个问题是,如果您引用其中包含 ~ 的路径,shell 在将其传递到 cd 之前不会扩展该路径。您可以通过使用 $HOME 来解决此问题,或者将 ~/ 放在引号之外。

另外,您不需要根据 $1 有条件地设置 $DIR,也不需要使用 sedxargs< /代码>。只需在路径中直接使用 $1 即可;如果它为空,则该目录组件将为空,只留下 Working Branches 目录作为 cd 的参数。

简单地

function projects () {
  cd "$HOME/Projects/Working Branches/$1"
}

应该可以工作,稍微短但不太清晰的也可以

function projects () {
  cd ~/"Projects/Working Branches/$1"
}

So, one problem is that if you quote a path with ~ in it, the shell will not expand that before passing it into cd. 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 use sed and xargs. Just use $1 directly in your path; if it's empty, that directory component will be empty, leaving you with just the Working Branches directory as an argument to cd.

Simply

function projects () {
  cd "$HOME/Projects/Working Branches/$1"
}

should work, as would the slightly shorter but less clear

function projects () {
  cd ~/"Projects/Working Branches/$1"
}
緦唸λ蓇 2024-10-01 16:36:54

问题是波浪线扩展是由 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.

晌融 2024-10-01 16:36:54

也许你对此进行了过度设计:

cd "~/Projects/Working Branches/"
cd "$DIR"

不过,我并没有真正理解 sed 命令的目的,所以也许我遗漏了一些东西。

Maybe you're over-engineering this:

cd "~/Projects/Working Branches/"
cd "$DIR"

I don't really get the purpose of the sed command, though, so maybe I'm missing something.

岁月静好 2024-10-01 16:36:54

xargscd 在我的 bash 中不能一起工作。我认为这是因为 xargs 想要一个应用程序,而 cd 是 bash shell 中的内置函数。

尝试 echo“项目”| xargs cd 在您的主目录中以查看是否是这种情况。

另一个问题是 ~ 不会像您想象的那样通过 echo 进行扩展。尝试改用完整路径。或者使用反引号展开 ~

echo ´echo ~/`"Projects/Working Branches/$DIR"

xargs and cd does not work together in my bash. I think it's because xargs want an application and cd 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 ~

echo ´echo ~/`"Projects/Working Branches/$DIR"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文