传递包含空格的shell变量作为参数 [英] Passing shell variables containing spaces as arguments

查看:126
本文介绍了传递包含空格的shell变量作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多这些问题已经存在了,但是我的情况是非常独一无二的,迄今没有人帮助我。



我正在写一个脚本将重新格式化(标签不好)的音乐库。它目前是音乐/流派/艺术家/专辑/ ## - Title.wav的形式。



从音乐目录,我运行我的script1,它调用code> find -name* .wav-exec script2 {} $ {other_args} 。 Script2解析变量的路径,如果不存在,创建相应的输出目录,然后调用ffmpeg来进行压缩。



ex:($ sourcefilepath contains { $ from $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

如果我的文件名或路径不包含空格,它的作用就像一个魅力。



如果它包含空格,ffmpeg失败,并出现以下错误:


蓝调/国家蓝调/节奏,乡村与蓝调/ 08-Natalie Cole& Reba
McEntire Since I Fell For You.wav:没有这样的文件或目录


注意:这是源文件找不到输出文件似乎正常工作,即使有空格。这不是我运行的完整命令(它也有一些-metadata标志,但也似乎正常工作我正在使用我上面写的命令进行调试)。


$ b $这里有趣的是,ffmpeg不会将$ sourcefilepath解释为两个参数,它只是找不到我指定的文件。从音乐目录中运行以下内容。

  ffmpeg -i蓝调/国家\\蓝调/节奏\,\国家\ And\ Blues / 08-Natalie\ Cole \ \& \ Reba\ McEntire\ \ \ Since\ I\ Fell\ For\ You.wav output.flac 

我被困在这里。 Web上的其他信息涉及获取一个命令来查看一个空格为一个arg而不是多个的变量。我似乎已经这样做了,但是ffmpeg现在找不到我的文件。



通常我只是使用 find -name*。 wav-exec ffmpeg -i {} $ {args} \; 但是我需要处理文件路径来提取我的元数据。



解决方案

我的精神力量表明,在你的代码的某个时候,你在做某事沿着以下行:

  something = $(echo $ filename | something)

这应该是:

  something = $(echo$ filename| something)

这是一个脚本,演示为什么这是一个问题: / p>

 #!/ bin / sh 
filename =Reba McEntire因为我跌倒了
echo$ filename #正确,引用
echo $ filename#不正确,未引用

它输出: p>

  Reba McEntire自从我跌了
Reba McEntire因为我跌了

你会注意到




  • 第一行有三空格在艺术家之后,正如您正确的命令。

  • seco nd线在艺术家后面有一个空格,如您的错误信息。



这是由于未引用可变膨胀正在进行分词。 echo 然后将参数与单个空格连接起来。最终的结果是多个空白合并成一个空格。


I know there are many of these questions out there already, but my situation is unique enough that none of them have helped me thus far.

I'm writing a script that will reformat my (poorly tagged) music library. It's currently in the form of Music/Genre/Artist/Album/##-Title.wav.

From the Music directory, I run my script1, which calls find -name "*.wav" -exec script2 {} ${other_args}. Script2 parses the path to variables, creates the appropriate output directory if it doesn't exist, and then calls ffmpeg to do the compression.

ex: ($sourcefilepath contains {} from find)

ffmpeg -i "$sourcefilepath" "$path/$to/$outfile.$extension"

If my file name or path contains no spaces, it works like a charm.

If it contains spaces, ffmpeg fails, with the following error:

Blues/Country Blues/Rhythm, Country And Blues/08-Natalie Cole & Reba McEntire Since I Fell For You.wav: No such file or directory

Note: that's the source file it can't find. The output file appears to be working correctly, even with spaces. That's not the full command I'm running (it has some -metadata flags too, but that also appears to be working correctly. I'm debugging with the command I wrote above).

The interesting thing here is that ffmpeg is NOT interpreting $sourcefilepath as two args, it just can't find the file I specified. Running the following from the Music directory works.

ffmpeg -i Blues/Country\ Blues/Rhythm\,\ Country\ And\ Blues/08-Natalie\ Cole\ \&\ Reba\ McEntire\ \ \ Since\ I\ Fell\ For\ You.wav output.flac

I'm kind of stuck here. The other information on the web involves getting a command to see a variable with a space as one arg instead of multiple. I appear to have done that, but ffmpeg can't find my file now.

Normally I'd just use find -name "*.wav" -exec ffmpeg -i {} ${args} \; but I need to process the file path to extract my metadata.

I'm fairly new to shell scripting; does anybody know what's going on here?

解决方案

My psychic powers suggest that at some point in your code, you're doing something along the lines of:

something=$(echo $filename | something)

This should instead have been:

something=$(echo "$filename" | something)

Here's a script demonstrating why this is a problem:

#!/bin/sh
filename="Reba McEntire   Since I fell"    
echo "$filename"    # Correct, quoted
echo $filename      # Incorrect, unquoted

It outputs:

Reba McEntire   Since I fell
Reba McEntire Since I fell

You'll notice that

  • the first line has three spaces after the artist, as in your correct command.
  • the second line has one space after the artist, as in your error message.

This is due to unquoted variable expansion undergoing wordsplitting. echo then concatenates the arguments with single spaces. The net result is multiple whitespace coalescing into one space.

这篇关于传递包含空格的shell变量作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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