鱼壳,测试命令的行为不起作用

发布于 2025-01-25 17:14:18 字数 407 浏览 3 评论 0原文

我写了以下文件,但是 它无法正常工作。 如果通过使用Test命令选项的组合而不是...

function touch
  /usr/bin/touch $argv
  set -l ext (echo $argv | grep -Eo "\.(.*)\$" | grep -oE '[a-zA-Z]+')
  set -l fname (echo (pwd)/$argv)
  if not test -e $fname; and test "$ext" = "xml"
      echo "detection xml format"
      cat /home/mizuiro/template/xml/template.xml >> $argv
  end
end

请帮助我,我已经设置了测试命令以运行以运行。

I wrote the following file, but
It does not work as expected.
I have set up the test command to run if the file does not exist by using a combination of the test command option and not...

function touch
  /usr/bin/touch $argv
  set -l ext (echo $argv | grep -Eo "\.(.*)\
quot; | grep -oE '[a-zA-Z]+')
  set -l fname (echo (pwd)/$argv)
  if not test -e $fname; and test "$ext" = "xml"
      echo "detection xml format"
      cat /home/mizuiro/template/xml/template.xml >> $argv
  end
end

Please help me!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

岛徒 2025-02-01 17:14:18

我不明白您的代码应该做什么,您还不清楚要达到的目标或所遇到的错误。

让我们一键浏览一下:

功能触摸

此功能创建一个称为touch的函数。虽然它允许函数具有与触摸之类的知名命令相同的名称,但绝对不建议它基本上是相同的(例如,诸如GREP function之类的东西> grep -color = auto可能还可以)。在您运行touch的任何地方,这都可以尝试进行特殊的XML处理,包括在钓鱼已经运送的功能中。鱼功能并非以任何方式范围范围内或隔离,这使它成为一个坏主意。

我建议您将名称更改为“ mytouch”或“ newxml”之类的东西。

/usr/bin/touch $ argv

这会创建一个或多个文件(或将选项传递到touch程序),或调整其当日和MTIME。

set -l ext (echo $argv | grep -Eo "\.(.*)\$" | grep -oE '[a-zA-Z]+')

据推测,这可以在您给出的文件上获得扩展。如果您传递多个文件,则会断开,因为echo打印了参数分开的参数,但是grep希望它们每行一条。

set -l fname(echo(pwd)/$ argv)

这给文件提供了完整的路径 - 如果您给出了相对文件名。如果您给出一个绝对的文件名,则将打印Gibberish。

例如做touch/tmp/foo当您进入目录/home/rivi时,这将打印/home/rivi // tmp/foo 。

它也将使用多个文件名打破。您可以将其替换为

set -l fname $PWD/$argv
# or
set -l fname (pwd)/$argv

可以处理多个参数的,尽管它仍然会通过绝对路径打破。

我也不知道为什么在这里需要绝对路径,因为您已经为上述touch提供了相对路径。

如果不是测试-E $ fname;并测试“ $ ext” =“ xml”

这两个件事 - 首先,它检查不测试-e $ fname,如果文件不存在,则将是正确的。如果那是真的,它将检查扩展名是否为“ XML”。如果两个测试都是错误的,则不会进入IF块(由于)。

第一个测试几乎可以保证是错误的,因为您在该文件上运行touch。因此,只有您没有创建该文件或类似内容的权限,这才是真的。

我相信问题是您在检查之前运行touch - 创建文件。

您可能想要类似的东西

if not test -e "$fname"; and test "$ext" = xml
   # do your thing
else
   # not a non-existent xml file, just run touch
   /usr/bin/touch $argv
end

(假设$ fname有意义 - 就像我说的那样,我不明白您为什么在那里使用绝对路径)

这也绝对不准备处理多个参数。

I don't understand what your code is supposed to be doing, and you haven't been clear on what you want to achieve or what the error you're getting is.

Let's go through it one-by one:

function touch

This creates a function called touch. While it's allowed for a function to have the same name as a well-known command like touch, it's absolutely not recommended unless it does basically the same thing (e.g. something like a grep function that does grep --color=auto is probably okay). Anywhere you run touch this would now try to do your special xml-handling, including in completions and functions that fish already ships. Fish functions aren't scoped or isolated in any way, making this a bad idea.

I suggest you change the name to "mytouch" or "newxml" or something.

/usr/bin/touch $argv

This creates one or multiple files (or passes options to the touch program), or adjusts their atime and mtime.

set -l ext (echo $argv | grep -Eo "\.(.*)\
quot; | grep -oE '[a-zA-Z]+')

This presumably gets the extensions on the file you've given. If you pass multiple files, this will break because echo prints the arguments space-separated but the grep wants them one per line.

set -l fname (echo (pwd)/$argv)

This gives the full path to the file - if you've given a relative filename. If you give an absolute filename this will print gibberish.

E.g. do touch /tmp/foo while you're in the directory /home/rivi and this will print /home/rivi//tmp/foo.

It will also be broken with multiple filenames. You can replace this with just

set -l fname $PWD/$argv
# or
set -l fname (pwd)/$argv

That would handle multiple arguments, although it would still be broken with absolute paths.

I also have no idea why you need the absolute path here because you've given the relative path to touch above.

if not test -e $fname; and test "$ext" = "xml"

This checks two things - first it checks not test -e $fname, which will be true if the file doesn't exist. If that was true, it checks if the extension is "xml". If either of the tests is false, it won't go into the if-block (because of the and).

The first test is almost guaranteed to be false because you ran touch on that file. So it'll only really ever be true if you didn't have permissions to create that file or something like that.

I believe the issue is that you run touch before the check - which creates the file.

You might want something like

if not test -e "$fname"; and test "$ext" = xml
   # do your thing
else
   # not a non-existent xml file, just run touch
   /usr/bin/touch $argv
end

(assuming $fname makes any sense - like I said I don't see why you're using the absolute path there at all)

This also is absolutely not prepared to handle multiple arguments.

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