简体   繁体   English

路径中带有空格的Shell脚本根据目录的执行方式不同

[英]Shell script with whitespace in path executes differently depending on directory

I have made a script to open Spotify with wine: 我已经编写了一个脚本来使用wine打开Spotify:

#!/bin/bash
DIR="/home/jorgsk/.wine/drive_c/Program Files/Spotify/"
cd "$DIR"
wine spotify.exe 2>/dev/null

I'm passing "$DIR" to cd with quotes because of the whitespace in "Program Files"; 由于“程序文件”中的空格,我将带有引号的“ $ DIR”传递给cd; if I don't have the quotes "/home/jorgsk/.wine/drive_c/Programs" will be considered as the argument to cd, which obviously will result in an error message. 如果我没有引号“ /home/jorgsk/.wine/drive_c/Programs”,将被视为cd的参数,这显然会导致错误消息。

Spotify starts fine if I launch the above script from its local directory (/home/jorgsk/bin) with ./spotify. 如果我使用./spotify从本地目录(/ home / jorgsk / bin)启动上述脚本,则Spotify可以正常启动。 However, since I wish to launch it from wherever, I have /home/jorgsk/bin added to the $PATH variable in .bashrc. 但是,由于我希望从任何地方启动它,所以我将/ home / jorgsk / bin添加到了.bashrc中的$ PATH变量中。 When I write "spotify" from for example my home directory, I get the error message 当我从主目录中写入“ spotify”时,出现错误消息

bash: /home/jorgsk/.wine/drive_c/Program: No such file or directory

which is the same error message I get if I don't include $DIR in quotes when launching tje script with ./spotify from the script's directory. 如果从脚本目录中使用./spotify启动tje脚本时,如果在引号中不包含$ DIR,则将收到相同的错误消息。

What is happening here? 这是怎么回事

I'm not sure why this is happening - looks like it should work to me. 我不确定为什么会这样-看起来应该对我有用。

You say this is in your path but is that the version that is actually being called? 您说这是您的路径,但是实际上是该版本吗?

Try running: which spotify from your home directory. 尝试运行: which spotify从你的主目录。 The which command tells you the path of the script that runs. which命令告诉您运行脚本的路径。

I don't know the answer but I think I can give you a procedure that will identify the issue. 我不知道答案,但我想可以给您一个确定问题的步骤。

Add set -x to have the script echo the lines it is running. 添加set -x以使脚本回显正在运行的行。

 #!/bin/bash
 set -x
 DIR="/home/jorgsk/.wine/drive_c/Program Files/Spotify/"
 cd "$DIR"
 wine spotify.exe 2>/dev/null

Also, name the script something other than spotify . 另外,将脚本命名为spotify其他名称。 Although it doesn't immediately look like it would matter, who knows what complex behavior is happening once wine gets control? 尽管看上去并不立即,但谁能知道一旦获得葡萄酒控制权,就会发生什么复杂的行为?

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

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