问题描述
我想将一个单词用作输入,并在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中创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!