OS X bash:目录名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1308823/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:08:40  来源:igfitidea点击:

OS X bash: dirname

bashmacosdirname

提问by Dinah

I want to create a simple bash script to launch a Java program on OS X. The names of the file, the file path, and the immediate working folder all contain spaces. When I do this:

我想创建一个简单的 bash 脚本来在 OS X 上启动 Java 程序。文件名、文件路径和直接工作文件夹都包含空格。当我这样做时:

#!/bin/sh
cd `dirname 
usage: dirname path
`

I get

我得到

cd "`dirname \"
#!/bin/sh
cd `dirname 
#! /bin/zsh
cd "${0:h}"
`
\"`"

I have also tried putting quotes in all kinds of different places. The most elaborate example being

我也试过在各种不同的地方加上引号。最详细的例子是

cd "$(dirname "
#!/bin/sh
cd "$(dirname "
cd "`dirname "
bash -x scriptname
"`"
")"
")"

Nothing has worked. I either get error messages or that cryptic "usage: dirname path" message.

没有任何效果。我要么收到错误消息,要么收到那条神秘的“用法:目录名路径”消息。

What are other methods that might work?

还有哪些可能有效的方法?



Edit:this doesn't seem to be an issue for anyone but me so it must just be my box. I'm going to accept my own post below because it's the only solution which worked for this specific problem. However I'm definitely upvoting the solutions which seem to be working for everyone else and really appreciate everyone's help.

编辑:这对除我之外的任何人来说似乎都不是问题,所以它必须只是我的盒子。我将在下面接受我自己的帖子,因为它是解决这个特定问题的唯一解决方案。但是,我绝对赞成似乎对其他人有用的解决方案,并且非常感谢大家的帮助。

采纳答案by Dinah

What finally worked for me is changing this:

最终对我有用的是改变这一点:

-u: abort on attempt to use undefined variable
-e: abort on first error

To this:

对此:

#!/bin/sh 

This also supports file names and file paths containing spaces. Here's where I found it: http://rentzsch.com/unix/locationAwareCommandFiles

这也支持包含空格的文件名和文件路径。这是我找到它的地方:http: //rentzsch.com/unix/locationAwareCommandFiles

回答by Sean Bright

What about:

关于什么:

#!/usr/bin/bash

That works for me here.

这对我有用。

回答by Ned Deily

##代码##

回答by pimlottc

Escaping the inner double quotes is unnecessary:

转义内部双引号是不必要的:

##代码##

But that doesn't get to the root of the problem, which is that somehow the value of $0 appears to be empty, or perhaps something strange. Try changing running your script this way:

但这并没有触及问题的根源,即以某种方式 $0 的值似乎是空的,或者可能是一些奇怪的东西。尝试以这种方式更改运行脚本:

##代码##

This will echo each line, with variables interpolated, before running it. It is very useful for debugging. Also quite helpful are:

这将在运行之前回显每一行,并插入变量。这对于调试非常有用。也很有帮助的是:

##代码##

回答by sixtyfootersdude

Hey not sure about this... But is it possible that your

嘿,不确定这个……但是你的

##代码##

Points to something that is not bash? What I usually use is:

指向不是 bash 的东西?我通常使用的是:

##代码##

Pretty new to the whole scripting thing so not sure.

整个脚本编写的东西很新,所以不确定。