简体   繁体   English

在 bash 中使用命令的结果作为参数?

[英]Using the result of a command as an argument in bash?

To create a playlist for all of the music in a folder, I am using the following command in bash:要为文件夹中的所有音乐创建播放列表,我在 bash 中使用以下命令:

ls > list.txt

I would like to use the result of the pwd command for the name of the playlist.我想使用pwd命令的结果作为播放列表的名称。

Something like:就像是:

ls > ${pwd}.txt

That doesn't work though - can anyone tell me what syntax I need to use to do something like this?但这不起作用 - 谁能告诉我我需要使用什么语法来做这样的事情?

Edit: 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.编辑:正如评论中提到的,pwd 最终会给出一个绝对路径,所以我的播放列表最终会在某个目录中被命名为.txt - 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!感谢您发现这一点 - 我可能会花很长时间想知道我的文件去了哪里!

The best way to do this is with "$(command substitution)" (thanks, Landon ):最好的方法是使用"$(command substitution)" (感谢Landon ):

ls > "$(pwd).txt"

You will sometimes also see people use the older backtick notation, but this has several drawbacks in terms of nesting and escaping:您有时还会看到人们使用较旧的反引号表示法,但这在嵌套和 escaping 方面有几个缺点

ls > "`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.注意pwd的未处理替换是绝对路径,所以上面的命令会在与工作目录相同的目录下创建一个同名文件,但扩展名为.txt 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: Thomas Kammeyer 指出basename命令会删除前导目录,因此这将在当前目录中创建一个带有该目录名称的文本文件:

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

Also thanks to erichui for bringing up the problem of spaces in the path.也感谢 erichui 提出路径中的空格问题。

This is equivalent to the backtick solution:这等效于反引号解决方案:

ls > $(pwd).txt

To do literally what you said, you could try:照你说的做,你可以试试:

ls > `pwd`.txt

which will use the full pathname, which should be fine.这将使用完整的路径名,这应该没问题。 Note that if you do this in your home directory, which might be in /home/hoboben, you will be trying the create /home/hoboben.txt, a text file in the directory above.请注意,如果您在您的主目录(可能位于 /home/hoboben)中执行此操作,您将尝试在上面的目录中创建 /home/hoboben.txt,这是一个文本文件。

Is this what you wanted?这是你想要的吗?

If you wanted the directory to contain a file named after it, you would get the basename of the current directory and append that with.txt to the pwd.如果您希望目录包含一个以它命名的文件,您将获得当前目录的基本名称和 append 与.txt 到 pwd。

Now, rather than use the pwd command... why not use the PWD environment variable?现在,与其使用 pwd 命令……为什么不使用 PWD 环境变量呢?

For example:例如:

ls > $PWD.txt

or或者

ls > ${PWD}.txt

is probably what you were trying to remember with your second example.可能是您在第二个示例中试图记住的内容。

If you're in /home/hoboben and you want to create /home/hoboben/hoboben.txt, try:如果您在 /home/hoboben 并且想要创建 /home/hoboben/hoboben.txt,请尝试:

ls > ${PWD}/${PWD##*/}.txt

If you do this, the file will contain its own name, so most often, you would remedy this in one of a few ways.如果您这样做,该文件将包含其自己的名称,因此大多数情况下,您可以通过以下几种方法之一来解决此问题。 You could redirect to somewhere else and move the file or name the file beginning with a dot to hide it from the ls command as long as you don't use the -a flag (and then optionally rename the resulting file).只要您不使用 -a 标志(然后可选地重命名生成的文件),您就可以重定向到其他地方并移动文件或以点开头的文件命名文件以将其隐藏在 ls 命令中。

I write my own scripts to manage a directory hierarchy of music files and I use subdirectories named ".info", for example, to contain track data in some spare files (basically, I "hide" metadata this way).我编写自己的脚本来管理音乐文件的目录层次结构,并使用名为“.info”的子目录,例如,在一些备用文件中包含曲目数据(基本上,我以这种方式“隐藏”元数据)。 It works out okay because my needs are simple and my collection small.效果很好,因为我的需求很简单,而且我的收藏很小。

I suspect the problem may be that there are spaces in one of the directory names.我怀疑问题可能是其中一个目录名称中有空格。 For example, if your working directory is "/home/user/music/artist name".例如,如果您的工作目录是“/home/user/music/artist name”。 Bash will be confused thinking that you are trying to redirect to /home/user/music/artist and name.txt. Bash 会感到困惑,以为您正在尝试重定向到 /home/user/music/artist 和 name.txt。 You can fix this with double quotes你可以用双引号解决这个问题

ls > "$(pwd).txt"

Also, you may not want to redirect to $(pwd).txt.此外,您可能不想重定向到 $(pwd).txt。 In the example above, you would be redirecting the output to the file "/home/user/music/artist name.txt"在上面的示例中,您会将 output 重定向到文件“/home/user/music/artist name.txt”

The syntax is:语法是:

ls > `pwd`.txt

That is the '`' character up underneath the '~', not the regular single quote.那是 '~' 下面的 '`' 字符,而不是常规的单引号。

Using the above method will create the files one level above your current directory.使用上述方法将创建比当前目录高一级的文件。 If you want the play lists to all go to one directory you'd need to do something like:如果您希望将所有 go 的播放列表放到一个目录中,您需要执行以下操作:

#!/bin/sh

MYVAR=`pwd | sed "s|/|_|g"`
ls > /playlistdir/$MYVAR-list.txt

to strip all but the directory name剥离除目录名称以外的所有内容

ls >/playlistdir/${PWD##/*}.txt

this is probably not what you want because then you don't know where the files are (unless you change the ls command)这可能不是您想要的,因为您不知道文件在哪里(除非您更改 ls 命令)

to replace "/" with "_"用“_”替换“/”

ls >/playlistdir/${PWD//\//_}.txt

but then the playlist would look ugly and maybe not even fit in the selection window但是播放列表看起来很丑陋,甚至可能不适合选择 window

So this will give you both a short readable name and usable paths inside the file因此,这将为您提供一个简短的可读名称和文件内的可用路径

ext=.mp3 #leave blank for all files
for FILE in "$PWD/*$ext"; do echo "$FILE";done >/playlistdir/${PWD##/*}.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM