路径中带有空格的Shell脚本根据目录的执行方式不同 [英] Shell script with whitespace in path executes differently depending on directory

查看:395
本文介绍了路径中带有空格的Shell脚本根据目录的执行方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个脚本来使用wine打开Spotify:

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

由于程序文件"中的空格,我将带引号的"$ DIR"传递给cd;如果我没有引号"/home/jorgsk/.wine/drive_c/Programs",将被视为cd的参数,这显然会导致错误消息.

如果我使用./spotify从其本地目录(/home/jorgsk/bin)中启动上述脚本,则

Spotify可以正常启动.但是,由于我希望从任何地方启动它,所以我将/home/jorgsk/bin添加到了.bashrc中的$ PATH变量中.当我从主目录中写入"spotify"时,会收到错误消息

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

如果从脚本目录中使用./spotify启动tje脚本时,如果在引号中不包含$ DIR,则会收到相同的错误消息.

这是怎么回事?

解决方案

我不知道答案,但我想可以给您提供一个确定问题的过程.

添加set -x以使脚本回显正在运行的行.

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

此外,将脚本命名为spotify以外的其他名称.尽管看起来并不立即,但谁能知道一旦获得葡萄酒的控制权,就会发生什么复杂的行为?

I have made a script to open Spotify with wine:

#!/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"; 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.

Spotify starts fine if I launch the above script from its local directory (/home/jorgsk/bin) with ./spotify. However, since I wish to launch it from wherever, I have /home/jorgsk/bin added to the $PATH variable in .bashrc. When I write "spotify" from for example my home directory, I get the error message

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.

What is happening here?

解决方案

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.

 #!/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. Although it doesn't immediately look like it would matter, who knows what complex behavior is happening once wine gets control?

这篇关于路径中带有空格的Shell脚本根据目录的执行方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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