Mac OS X的 - 默认的bash路径扩展或缺陷 [英] Mac OS X - bash default pathname expansion or bug

查看:194
本文介绍了Mac OS X的 - 默认的bash路径扩展或缺陷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行脚本在bash下面一行在Mac OS X 10.6.4(从的这个问题):

  $导出编辑器='mvim -f -c金VimLeave *!打开-a终端'

唉,我得到的是意想不到的事情:

$回声$ EDITOR

mvim -f -c金VimLeave桌面文档下载库电影音乐图片公共场所垃圾桶里去!开-a终端

预期的输出是:

$回声$ EDITOR

mvim -f -c金VimLeave *!打开-a终端

要解决这个问题,以这种方式是设置 noglob ,即运行设置-f 在紧接导出分配。然而,眼下的问题是,这是否是在Mac OS X中的正常现象,因为(因为 noglob 默认情况下,即设置+ F <未设置/ code>),或者因为在庆典在Mac OS X中的错误。

的bash的版本是:


$的bash --version
GNU bash的版本3.2.48(1) - 释放下(x86_64-苹果darwin10.0)
版权所有(C)2007自由软件基金会

有可能是由实用指南于Unix的Mac OS X用户 329页的方式有些帮助:除非noglob(页320)设置,外壳执行[路径扩展]当它遇到一个模糊文件引用 - 包含任何未加引号字符和放大器的象征;,[或]。但是因为 * 是晶片被保护是引号内,问题仍然存在:?是bash的行为的默认设置,或错误

这只是一种好奇,但我会为你可能有什么想法和投入表示感谢。

布赖恩


解决方案

EDITOR 变量的的设置正确。你可以看到这个,如果你执行:

 回声$ EDITOR

看一看下面的成绩单:


  PAX&GT;出口EDITOR ='mvim -f -c金VimLeave *!打开-a终端'PAX&GT;回声$ EDITOR
mvim -f -c金VimLeave SecretCiaDoc.txt NsaEchelonKeys.txt!开-a终端PAX&GT;回声$ EDITOR
mvim -f -c金VimLeave *!打开-a终端


您的问题不在于与设置语句,但与你的回声。在设置不会展开 *,因为它包含单引号之内,但做一个回声< / code>不带引号的将会的展开。

这决不会影响其使用环境变量的方案。


根据您的评论:


  

这仍然是奇怪的:即 * 仍是引号(双引号)为回声命令之内。 A ='ABC*XYZ;回声$ A 在任何bash或破折号不扩大我;其实,它包括引号作为第二个参数。


关注此:

  PAX&GT; A ='ABC*XYZ;回声$一个
ABC*XYZPAX&GT; A ='ABC* XYZ'; $回声一
ABC* XYZPAX&GT; A ='ABC* XYZ'; $回声一
ABCSecretCiaDoc.txt NsaEchelonKeys.txt XYZPAX&GT;触摸你好; A ='ABC* XYZ;回声$一个
ABC你好XYZ

看到发生了什么。这不是治疗的任何特殊,只是一个字符。为什么它扩展了你的的原因EDITOR 是因为它是。对自己在使用*,它实际上是试图扩大了开头的文件和结束 - 你可以看到,在我上面的最后一个例子。

I'm trying to run the following line of script in bash on Mac OS X 10.6.4 (from this question):

$ export EDITOR='mvim -f -c "au VimLeave * !open -a Terminal"'

Alas, what I get is something unexpected:

$ echo $EDITOR
mvim -f -c "au VimLeave Desktop Documents Downloads Library Movies Music Pictures Public Sites bin !open -a Terminal"

The expected output would be:

$ echo $EDITOR
mvim -f -c "au VimLeave * !open -a Terminal"

The way to fix this to this is to set noglob, i.e. run set -f immediately prior to the export assignment. However, the question at hand is whether this is the expected behaviour on Mac OS X because (because noglob is unset by default, i.e. set +f) or because there is a bug in bash on Mac OS X.

The version of bash is:

$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.

There may be some assistance by way of page 329 of A practical guide to Unix for Mac OS X users: "Unless noglob (page 320) is set, the shell performs [pathname expansion] when it encounters an ambiguous file reference--a token containing any of the unquoted characters &, ?, [, or ].". However because the * being globbed is within quotes, the question remains: Is the behaviour a default setting of bash, or a bug?

This is just a curiosity, but I'd be grateful for any thoughts and input you may have.

Brian

解决方案

Your EDITOR variable is set correctly. You can see this if you execute:

echo "$EDITOR"

Have a look at the following transcript:


pax> export EDITOR='mvim -f -c "au VimLeave * !open -a Terminal"'

pax> echo $EDITOR
mvim -f -c "au VimLeave SecretCiaDoc.txt NsaEchelonKeys.txt !open -a Terminal"

pax> echo "$EDITOR"
mvim -f -c "au VimLeave * !open -a Terminal"


Your problem lies not with the set statement but with your echo. The set will not expand the * because it's contained within single quotes but doing an echo without quotes will expand it.

This in no way affects programs which use the environment variable.


Based on your comment:

This is still weird: that * is still within quotes (the double quotes) for the echo command. a='abc "*" xyz'; echo $a does not expand for me in either bash or dash; in fact, it includes the quotes as the second argument.

Watch this:

pax> a='abc "*" xyz' ; echo $a
abc "*" xyz

pax> a='abc "* xyz' ; echo $a
abc "* xyz

pax> a='abc " * xyz' ; echo $a
abc " SecretCiaDoc.txt NsaEchelonKeys.txt xyz

pax> touch '"hello' ; a='abc "* xyz' ; echo $a
abc "hello xyz

See what's happening. It's not treating the " as anything special, just another character. The reason why it expands for your EDITOR is because it's on its own. When you use "*", it's actually trying to expand files that begin and end with " - you can see that in my last example above.

这篇关于Mac OS X的 - 默认的bash路径扩展或缺陷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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