本文介绍了使用命令在bash争论的结果如何呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要为所有文件夹中的音乐的播放列表,我在bash使用下面的命令:

To create a playlist for all of the music in a folder, I am using the following command in bash:

ls > list.txt

我想用的结果,在 PWD 作为播放列表的名称(打印工作目录)命令。

I would like to use the result of the pwd (print working directory) command for the name of the playlist.

是这样的:

ls > ${pwd}.txt

这不工作,虽然 - ?谁能告诉我,我需要什么样的语法用来做这样的事情。

That doesn't work though - can anyone tell me what syntax I need to use to do something like this?

编辑:正如评论PWD最终会给出一个绝对路径提到,所以我的播放列表将最终被命名为.TXT在某些目录中 - 德哦!因此,我将不得不削减的路径。感谢察觉了 - !我可能会花了年龄想知道我的文件了。

As mentioned in the comments pwd will end up giving an absolute path, so my playlist will end up being named .txt in some directory - d'oh! So I'll have to trim the path. Thanks for spotting that - I would probably have spent ages wondering where my files went!

推荐答案

使用反引号替换命令的结果是:

Use backticks to substitute the result of a command:

ls > "`pwd`.txt"

正如指出的, $(CMD)相当于:

ls > "$(pwd).txt"

注意的未处理替换 PWD 是绝对路径,所以上面的命令创建在同一目录作为工作目录中的同名文件,而是用一个 .TXT 扩展。 指出,基本名命令条上领先的目录,因此这将创建在当前目录下的文本文件与该目录的名称:

Note that the unprocessed substitution of pwd is an absolute path, so the above command creates a file with the same name in the same directory as the working directory, but with a .txt extension. Thomas Kammeyer pointed out that the basename command strips the leading directory, so this would create a text file in the current directory with the name of that directory:

ls > "`basename "$(pwd)"`.txt"

也感谢了解造就了空间问题的路径。

Also thanks to erichui for bringing up the problem of spaces in the path.

这篇关于使用命令在bash争论的结果如何呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:36