在 bash 中使用命令的结果作为参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the result of a command as an argument in bash?
提问by HoboBen
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! So I'll have to trim the path. Thanks for spotting that - I would probably have spent ages wondering where my files went!
编辑:正如评论中提到的,pwd 最终会给出一个绝对路径,所以我的播放列表最终会在某个目录中被命名为 .txt - 天哪!所以我必须修剪路径。感谢您发现这一点 - 我可能会花很长时间想知道我的文件去了哪里!
回答by John Calsbeek
The best way to do this is with "$(command substitution)"
(thanks, Landon):
最好的方法是使用"$(command substitution)"
(谢谢,兰登):
ls > "$(pwd).txt"
You will sometimes also see people use the older backtick notation, but this has several drawbacksin terms of nesting and 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. 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:
请注意,未处理的替换为pwd
绝对路径,因此上述命令在与工作目录相同的目录中创建了一个同名文件,但带有.txt
扩展名。Thomas Kammeyer 指出该basename
命令会删除前导目录,因此这将在当前目录中创建一个具有该目录名称的文本文件:
ls > "$(basename "$(pwd)").txt"
Also thanks to erichui for bringing up the problem of spaces in the path.
也感谢erichui提出路径空间的问题。
回答by Landon
This is equivalent to the backtick solution:
这相当于反引号解决方案:
ls > $(pwd).txt
回答by Thomas Kammeyer
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.
如果您希望该目录包含以它命名的文件,您将获得当前目录的基本名称并将其与 .txt 附加到密码中。
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).
如果您这样做,文件将包含其自己的名称,因此大多数情况下,您可以通过以下几种方法之一来解决此问题。您可以重定向到其他地方并移动文件或以点开头命名文件以将其从 ls 命令中隐藏,只要您不使用 -a 标志(然后可以选择重命名生成的文件)。
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). It works out okay because my needs are simple and my collection small.
我编写自己的脚本来管理音乐文件的目录层次结构,并使用名为“.info”的子目录,例如,在一些备用文件中包含曲目数据(基本上,我以这种方式“隐藏”元数据)。效果很好,因为我的需求很简单,而且我的收藏很小。
回答by erichui
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". Bash will be confused thinking that you are trying to redirect to /home/user/music/artist and name.txt. You can fix this with double quotes
我怀疑问题可能是目录名称之一中有空格。例如,如果您的工作目录是“/home/user/music/artist name”。Bash 会感到困惑,认为您正在尝试重定向到 /home/user/music/artist 和 name.txt。你可以用双引号解决这个问题
ls > "$(pwd).txt"
Also, you may not want to redirect to $(pwd).txt. In the example above, you would be redirecting the output to the file "/home/user/music/artist name.txt"
此外,您可能不想重定向到 $(pwd).txt。在上面的示例中,您将输出重定向到文件“/home/user/music/artist name.txt”
回答by John Meagher
The syntax is:
语法是:
ls > `pwd`.txt
That is the '`' character up underneath the '~', not the regular single quote.
那是 '~' 下方的 '`' 字符,而不是常规的单引号。
回答by Mark Nold
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:
使用上述方法将创建比当前目录高一级的文件。如果您希望播放列表都转到一个目录,您需要执行以下操作:
#!/bin/sh
MYVAR=`pwd | sed "s|/|_|g"`
ls > /playlistdir/$MYVAR-list.txt
回答by technosaurus
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
但是播放列表会看起来很丑,甚至可能不适合选择窗口
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