【发布时间】:2014-12-04 11:26:25
【问题描述】:
我在使用输出重定向时发现了 csh 的这种非常奇怪的行为。 我有以下脚本:
#! /bin/csh
set dirs = "."
set files = "`find "'"$dirs"'" -type f |& grep -v '^find: '`"
echo "-----------WITH &-----------------------"
echo "ARRAY:" $files
echo "FIRST ELEMENT FROM ARRAY1:" $files[1]
set files2 = "`find "'"$dirs"'" -type f`"
echo "------------WITHOUT &-----------------------"
echo "ARRAY2" $files2
echo "FIRST ELEMENT SECOND ARRAY" $files2[1]
现在,如果您运行它,一切正常。结果是:
-----------WITH &-----------------------
ARRAY: ./.netscape/preferences.js ...
FIRST ELEMENT FROM ARRAY1: ./.netscape/preferences.js
------------WITHOUT &-----------------------
ARRAY2 ./.netscape/preferences.js ...
FIRST ELEMENT SECOND ARRAY ./.netscape/preferences.js
但是当我像这样运行它时 csh -x test.csh (它是较大脚本的一部分,所以我必须以某种方式对其进行调试)我得到这个:
set dirs = .
set files = `find "$dirs" -type f |& grep -v '^find: '`
grep -v ^find:
echo -----------WITH &-----------------------
-----------WITH &-----------------------
echo ARRAY: find . -type f ./.netscape/preferences.js ...
ARRAY: find . -type f ./.netscape/preferences.js ...
echo FIRST ELEMENT FROM ARRAY1: find . -type f
FIRST ELEMENT FROM ARRAY1: find . -type f <----WHY?
set files2 = `find "$dirs" -type f`
find . -type f
echo ------------WITHOUT &-----------------------
------------WITHOUT &-----------------------
echo ARRAY2 ./.netscape/preferences.js ...
ARRAY2 ./.netscape/preferences.js ...
echo FIRST ELEMENT SECOND ARRAY ./.netscape/preferences.js
FIRST ELEMENT SECOND ARRAY ./.netscape/preferences.js
现在,在调试时这会破坏脚本。我觉得这种行为很令人费解。有人能解释一下为什么会发生这种情况以及如何避免吗?
(不要发布我不应该使用 csh 的无用 cmets,这是垃圾。我知道。但我的大学有点强迫我使用它。)
编辑:我按照 John C 的建议做了所有事情。我的输出不同。
bash2-2.05a$ csh -x test.csh 2>catch_stderr
-----------WITH &-----------------------
ARRAY: find . -type f ./test.csh ./ff.csh ./ff ./catch_stderr
FIRST ELEMENT FROM ARRAY1: find . -type f
------------WITHOUT &-----------------------
ARRAY2 ./test.csh ./ff.csh ./ff ./catch_stderr
FIRST ELEMENT SECOND ARRAY ./test.csh
bash2-2.05a$ cat catch_stderr
set dirs = .
set files = `find "$dirs" -type f |& grep -v '^find: '`
grep -v ^find:
echo -----------WITH &-----------------------
echo ARRAY: find . -type f ./test.csh ./ff.csh ./ff ./catch_stderr
echo FIRST ELEMENT FROM ARRAY1: find . -type f
set files2 = `find "$dirs" -type f`
find . -type f
echo ------------WITHOUT &-----------------------
echo ARRAY2 ./test.csh ./ff.csh ./ff ./catch_stderr
echo FIRST ELEMENT SECOND ARRAY ./test.csh
【问题讨论】:
-
哪所大学?他们实际上是在强迫您使用它,还是只是将其作为默认的交互式 shell 提供?
-
嗯,他们给了我们两个选择,使用 CSH 或一些古老的 BASH 版本。但是,如果您选择 bash,您就只能靠自己了,因为讲座和练习中的每个示例都在 csh 中进行了解释。我宁愿不命名我的大学。
-
-x 将其输出发送到 stderr。我认为您只是看到 stdout 和 stderr 有点混乱。