[英]Unix command to create file:/// URI based on the file's directory (pwd)
I would like to write a filepath as a URI in a Unix command-line emulator.我想在 Unix 命令行仿真器中将文件路径写为 URI。
Desired output:所需的 output:
file:///C|/directory/filename.ext
Using sed and pwd I can get close but not quite there because使用 sed 和 pwd 我可以接近但不完全在那里,因为
$ pwd
/c/directory/
...gives me a lower-case drive and no pipe. ...给我一个小写驱动器,没有 pipe。
Is there a better way?有没有更好的办法?
You can do it with sed
;你可以用
sed
; it has a y
command to do the case-conversion, though it is somewhat convoluted because you would have to go through the hold space as well as the pattern space.它有一个
y
命令来进行大小写转换,尽管它有些复杂,因为您必须通过保持空间和模式空间来 go 。 It would be easier to use Perl - it probably has one or more modules to do this (such as URI and URI::file ), though a simple version could be produced with regular expressions.使用 Perl 会更容易——它可能有一个或多个模块来执行此操作(例如URI和URI::file ),尽管可以使用正则表达式生成一个简单的版本。
#!/usr/bin/env perl
use strict;
use warnings;
foreach my $name (@ARGV)
{
if ($name =~ m%^/([a-zA-Z])(/.+)%)
{
printf "file:///%c|%s\n", uc($1), $2;
}
else
{
print STDERR "$0: unexpected name format - no /x/ drive letter prefix ($name)\n";
}
}
(Untested code.) (未经测试的代码。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.