处理字符串以在UNIX中创建目录 [英] manipulating a string to create directories in unix

查看:77
本文介绍了处理字符串以在UNIX中创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个单词用作输入,并在unix shell中使用该单词的字母创建连续目录.我尝试了sed,awk和fold命令,但没有获得任何有用的结果.有什么建议吗?

I want to take a word as input and creating consecutive directories using the letters of this word in unix shell. I tried sed, awk and fold commands but did not obtaiın any useful result. Any advice?

例如:如果输入为hello,则应将h/e/l/l/o目录创建为另一个目录.换句话说,h将是顶级目录,而o将是最深的子目录.

For example: If input is hello, it should create h/e/l/l/o directories as one inside another. In other words, h will be the top directory and o will be the deepest subdirectory.

推荐答案

这应该在任何与Bourne兼容的外壳中都可以完成:

This should do the trick in any Bourne compatible shell:

$ mkdir -p $(echo foobar | sed 's:\(.\):\1/:g')
$ find f
f
f/o
f/o/o
f/o/o/b
f/o/o/b/a
f/o/o/b/a/r

请注意,\1是对第一对\(...\)匹配的文本的反向引用.还要注意,扩展结果为mkdir -p f/o/o/b/a/r/,但mkdir忽略了斜杠,这对我们有利.

Note that \1 is a backreference to the text matched by the first pair of \(...\). Note also that the expansion result is mkdir -p f/o/o/b/a/r/ -- but mkdir ignores the trailing slash to our advantage.

这篇关于处理字符串以在UNIX中创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆