加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 是否有一个很好的方法来替换bash中的波形符号的主

发布时间:2020-12-14 06:29:59 所属栏目:百科 来源:网络整理
导读:我试图用一个路径工作,并用bash替换一个波形符号的主目录,我希望用必要的少量外部程序完成它.有没有办法只用bash呢?我有 ${PWD/#$HOME/~} 但那不是很正确.它需要转换: /home/alice to ~/home/alice/ to ~//home/alice/herp to ~/herp/home/alicederp to /
我试图用一个路径工作,并用bash替换一个波形符号的主目录,我希望用必要的少量外部程序完成它.有没有办法只用bash呢?我有
${PWD/#$HOME/~}

但那不是很正确.它需要转换:

/home/alice to ~
/home/alice/ to ~/
/home/alice/herp to ~/herp
/home/alicederp to /home/alicederp

作为一个感兴趣的注意事项,在转换w value in the prompt时,应该怎么做bash源呢?

/* Return a pretty pathname.  If the first part of the pathname is
   the same as $HOME,then replace that with `~'.  */
char *
polite_directory_format (name)
     char *name;
{
  char *home;
  int l;

  home = get_string_value ("HOME");
  l = home ? strlen (home) : 0;
  if (l > 1 && strncmp (home,name,l) == 0 && (!name[l] || name[l] == '/'))
    {
      strncpy (tdir + 1,name + l,sizeof(tdir) - 2);
      tdir[0] = '~';
      tdir[sizeof(tdir) - 1] = '';
      return (tdir);
    }
  else
    return (name);
}
见 this unix.stackexchange answer:

If you’re using bash,then the dirs builtin has the desired
behavior:

06000

这可能会攻击您粘贴在那里的C代码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读