当zsh在Mac OS X上设置为登录shell时,当它由iTerm启动时,zsh不会认为它是作为登录shell运行的,尽管它以'-zsh'开头(' - '作为arg [0]的第一个字符,它应该意味着它应该作为登录shell启动.
因此,当我将登录shell设置为bash时,bash在$ 0中首先识别出这个' - '并作为登录shell运行,但是zsh不会,尽管它似乎应该.
有没有办法让zsh识别arg [0]中的' - ',或者让iTerm使用--login命令行参数运行shell?
我正在写一个bash脚本set -u
,我有一个空数组扩展的问题:bash似乎在扩展期间将空数组视为未设置的变量:
$ set -u
$ arr=()
$ echo "foo: '${arr[@]}'"
bash: arr[@]: unbound variable
Run Code Online (Sandbox Code Playgroud)
(declare -a arr
也没帮助.)
对此的常见解决方案是使用${arr[@]-}
替代,从而替换空字符串而不是("未定义")空数组.然而,这不是一个好的解决方案,因为现在你无法辨别出一个带有一个空字符串的数组和一个空数组.(@ -expansion在bash中是特殊的,它扩展"${arr[@]}"
为"${arr[0]}" "${arr[1]}" …
,这使它成为构建命令行的完美工具.)
$ countArgs() { echo $#; }
$ countArgs a b c
3
$ countArgs
0
$ countArgs ""
1
$ brr=("")
$ countArgs "${brr[@]}"
1
$ countArgs "${arr[@]-}"
1
$ countArgs "${arr[@]}"
bash: arr[@]: unbound variable
$ set +u
$ countArgs "${arr[@]}"
0
Run Code Online (Sandbox Code Playgroud)
那么有没有办法解决这个问题,除了检查一个数组的长度if
(参见下面的代码示例),或关闭-u
该短片的设置? …
首先,我不确定解决方案是否存在.我花了不少时间试图想出一个,所以要小心.
r1包含任意整数,不根据其值设置标志.如果r1是0x80000000,则将r0设置为1,否则,仅使用两条指令将r0设置为0.
在3条指令中很容易做到这一点(有很多方法),但是在2中这样做似乎很难,而且很可能是不可能的.
在外壳程序脚本set -e
中,当从脚本执行的某些命令以非零退出代码退出时,通常通过停止脚本来使脚本更健壮。
通常很容易指定您不关心|| true
最后添加的某些命令是否成功。
当您实际上关心返回值但不希望脚本在非零返回码上停止时,会出现问题,例如:
output=$(possibly-failing-command)
if [ 0 == $? -a -n "$output" ]; then
...
else
...
fi
Run Code Online (Sandbox Code Playgroud)
在这里,我们既要检查退出代码(因此不能|| true
在命令替换表达式内部使用)并获取输出。但是,如果命令替换中的命令失败,则由于导致整个脚本停止set -e
。
有没有一种干净的方法可以防止脚本在此之前停止而无需取消-e
设置然后重新设置呢?
我使用以下代码执行管道:
PCollection<TableRow> test1 = ...
test1
.apply(BigQueryIO.Write
.named("test1 write")
.to("project_name:dataset_name.test1")
.withSchema(tableSchema)
.withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
.withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_APPEND));
PCollection<TableRow> test2 = ...
test2
.apply(BigQueryIO.Write
.named("test2 write")
.to("project_name:dataset_name.test2")
.withSchema(tableSchema)
.withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
.withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_APPEND));
Run Code Online (Sandbox Code Playgroud)
如果我执行管道并且表"test1"和"test2"都不存在,我将获得以下信息:
jun 09, 2015 12:29:24 PM com.google.cloud.dataflow.sdk.util.BigQueryTableInserter tryCreateTable
INFORMACIÓN: Trying to create BigQuery table: project_name:dataset_name.test1
jun 09, 2015 12:29:27 PM com.google.cloud.dataflow.sdk.util.RetryHttpRequestInitializer$LoggingHttpBackoffUnsuccessfulResponseHandler handleResponse
ADVERTENCIA: Request failed with code 404, will NOT retry: https://www.googleapis.com/bigquery/v2/projects/pragmatic-armor-455/datasets/audit/tables/project_name:dataset_name.test2/insertAll
Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : …
Run Code Online (Sandbox Code Playgroud)