【问题标题】:Unix command to create file:/// URI based on the file's directory (pwd)基于文件目录 (pwd) 创建 file:/// URI 的 Unix 命令
【发布时间】:2011-07-13 16:56:04
【问题描述】:

我想在 Unix 命令行模拟器中将文件路径写为 URI。

期望的输出:

file:///C|/directory/filename.ext

使用 sed 和 pwd 我可以接近但不是很接近,因为

$ pwd
/c/directory/

...给我一个小写的驱动器,没有管道。

有没有更好的办法?

【问题讨论】:

    标签: linux git unix command-line cygwin


    【解决方案1】:

    你可以用sed来做;它有一个y 命令来进行大小写转换,尽管它有些复杂,因为您必须通过保持空间和模式空间。使用 Perl 会更容易 - 它可能有一个或多个模块来执行此操作(例如 URIURI::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";
         }
     }
    

    (未经测试的代码。)

    【讨论】:

      猜你喜欢
      • 2011-07-27
      • 1970-01-01
      • 2020-12-13
      • 2013-10-12
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      相关资源
      最近更新 更多