鱼壳,测试命令的行为不起作用
我写了以下文件,但是 它无法正常工作。 如果通过使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白您的代码应该做什么,您还不清楚要达到的目标或所遇到的错误。
让我们一键浏览一下:
此功能创建一个称为
touch
的函数。虽然它允许函数具有与触摸之类的知名命令相同的名称,但绝对不建议它基本上是相同的(例如,诸如GREP
function之类的东西> grep -color = auto
可能还可以)。在您运行touch
的任何地方,这都可以尝试进行特殊的XML处理,包括在钓鱼已经运送的功能中。鱼功能并非以任何方式范围范围内或隔离,这使它成为一个坏主意。我建议您将名称更改为“ mytouch”或“ newxml”之类的东西。
这会创建一个或多个文件(或将选项传递到
touch
程序),或调整其当日和MTIME。据推测,这可以在您给出的文件上获得扩展。如果您传递多个文件,则会断开,因为
echo
打印了参数分开的参数,但是grep
希望它们每行一条。这给文件提供了完整的路径 - 如果您给出了相对文件名。如果您给出一个绝对的文件名,则将打印Gibberish。
例如做
touch/tmp/foo
当您进入目录/home/rivi
时,这将打印/home/rivi // tmp/foo 。
它也将使用多个文件名打破。您可以将其替换为
可以处理多个参数的,尽管它仍然会通过绝对路径打破。
我也不知道为什么在这里需要绝对路径,因为您已经为上述
touch
提供了相对路径。这两个件事 - 首先,它检查
不测试-e $ fname
,如果文件不存在,则将是正确的。如果那是真的,它将检查扩展名是否为“ XML”。如果两个测试都是错误的,则不会进入IF块(由于和
)。第一个测试几乎可以保证是错误的,因为您在该文件上运行
touch
。因此,只有您没有创建该文件或类似内容的权限,这才是真的。我相信问题是您在检查之前运行
touch
- 创建文件。您可能想要类似的东西
(假设$ 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:
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 agrep
function that doesgrep --color=auto
is probably okay). Anywhere you runtouch
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.
This creates one or multiple files (or passes options to the
touch
program), or adjusts their atime and mtime.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 thegrep
wants them one per line.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
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.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 theand
).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
(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.