OS X的bash:目录名称 [英] OS X bash: dirname

查看:269
本文介绍了OS X的bash:目录名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的bash脚本在OS X上启动Java程序的文件,文件路径,以及即时的工作文件夹中所有包含空格的名称。当我这样做:

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:

#!/bin/sh
cd `dirname $0`

我得到

usage: dirname path

我也试图把报价在各种不同的地方。最复杂的例子是

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

cd "`dirname \"$0\"`"

没有奏效。我要么得到错误信息或者神秘用法:目录名称路径消息

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

什么是其他可能的工作方式?

What are other methods that might work?


编辑:这似乎并没有对任何人都是一个问题,但我所以它必须仅仅是我的盒子。我会接受低于,因为它只有那些工作了这一特定问题的解决方案我自己的职位。不过我肯定upvoting这似乎是工作的其他人,真的AP preciate大家的帮助下解决方案。

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.

推荐答案

最后什么工作对我来说是改变这一点:

What finally worked for me is changing this:

#!/bin/sh
cd `dirname $0`

要这样:

#! /bin/zsh
cd "${0:h}"

这也支持文件名和包含空格的文件路径。这里就是我发现: http://rentzsch.com/unix/locationAwareCommandFiles

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

这篇关于OS X的bash:目录名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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