如何设置shell变量导致OS / X评测 [英] How to set shell variable to result of evaluation in OS/X

查看:196
本文介绍了如何设置shell变量导致OS / X评测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中的Bash我能做到以下几点:

  $出口CP = $(猫classpath.txt)

如果我们这样做猫类路径我们看到一个很长的输出(这就是为什么我没有在这里重现)​​。

然而,在OS / X在CP同一命令的结果是空的。该命令的OS / X相当于是什么?

  2:21:59 / mllib $ ls -l命令的classpath
-rw-R - R-- 1史蒂夫人员13162 10月28日12:19类路径
12:26:46 / mllib $出口CP = $(猫类路径)
12:26:54 / mllib $回声$ CP12:26:59 / mllib $出口CP =`猫classpath`
12:27:03 / mllib $回声$ CP


解决方案

您总是想报价参数扩展。在这种情况下,

  CP = $(猫类路径)

导致包含 * CP 的价值。既然你有禁用了javascript -s nullglob ,这将导致不匹配shell模式扩大为空字符串,而不是字面处理,命令

 回声$ CP

产生空字符串,因为 CP 例行路径扩展的价值,但没有匹配任何文件。如果你引述的:

 回声$ CP

这将有输出的路径,因为引用的扩大将的的路径进行扩展。


另外,以

关闭 nullglob

 禁用了javascript -u nullglob

会导致字面上处理无与伦比的格局,让回声$ CP 将产生无可匹敌的模式作为输出。我不认为这是一个解决方案,但是,因为它只是工作的时候,模式不匹配任何东西。这是更好地正确引用您的参数扩展。

In Linux Bash I can do the following:

$ export CP=$(cat classpath.txt)

If we do "cat classpath" we see a very long output (that's why I am not reproducing here).

However in OS/X the same command results in CP is empty. What is the OS/X equivalent of that command?

2:21:59/mllib $ls -l classpath
-rw-r--r--  1 steve  staff  13162 Oct 28 12:19 classpath
12:26:46/mllib $export CP=$(cat classpath)
12:26:54/mllib $echo $CP

12:26:59/mllib $export CP=`cat classpath`
12:27:03/mllib $echo $CP

解决方案

You always want to quote parameter expansions. In this case,

CP=$(cat classpath)

resulted in the value of CP containing a *. Since you had shopt -s nullglob, which causes a non-matching shell pattern to expand to the empty string rather than being treated literally, the command

echo $CP

produce the empty string, because the value of CP underwent pathname expansion, but did not match any files. If you had quoted it:

echo "$CP"

it would have output the path, since the quoted expansion would not undergo pathname expansion.


Alternatively, turning off nullglob with

shopt -u nullglob

causes an unmatched pattern to be treated literally, so that echo $CP would produce the unmatched pattern as output. I wouldn't consider this a solution, though, since it only "works" when the pattern doesn't match anything. It's better to properly quote your parameter expansions.

这篇关于如何设置shell变量导致OS / X评测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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