“如果[-t 1]"是什么意思?在shell脚本中做什么? [英] What does "if [ -t 1 ]" do in shell scripting?

查看:212
本文介绍了“如果[-t 1]"是什么意思?在shell脚本中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用于将zsh设置为默认外壳的代码:

I have code for setting the zsh as default shell:

if [ -t 1 ]; then 
exec zsh 
fi

如果[[-t 1] 在这里执行命令到底是什么?

What exactly does the command if [ -t 1 ] do here?

推荐答案

if命令;然后other_command;fi 运行 command ,然后,如果该命令以返回码0(成功")退出,则运行 other_command .

if command; then other_command; fi runs command and then, if that command exits with a return code of 0 ("success"), runs other_command.

命令 [ ... ] 旨在代替您在传统编程语言中找到的布尔表达式.如果这些选项的评估结果为真值,则它们之间有许多选项,括号之间的内容为0 =成功.

The command [...] is designed to take the place of the Boolean expressions that you find in traditional programming languages. It has a number of options for what goes between the brackets and exits with 0=success if those options evaluate to a true value.

特定的子命令 -t 测试文件描述符,以查看它是否已附加到终端.文件描述符1是脚本输出的目的地(简称标准输出"或"stdout").因此, -t 1 为true,并且 [-t 1] 仅当脚本的输出将发送到终端(而不是发送到文件或管道)时才返回成功.等).

The specific subcommand -t tests a file descriptor to see if it is attached to a terminal. The file descriptor 1 is where the script's output is going (aka "standard output" or "stdout" for short). So -t 1 is true and [ -t 1 ] returns success if and only if the output of the script is going to a terminal (instead of into a file or pipe or something).

在那种情况下,当前的shell被 zsh 的副本替换(通过 exec ).希望不会运行相同的脚本,因为 zsh 以相同的方式工作,并且将做出相同的决定并进入无限循环,而 exec 本身会

In that case, the current shell is replaced (via exec) by a copy of zsh. Which will hopefully not run the same script, since zsh works the same way and will make the same decision and go into an infinite loop execing itself.

这篇关于“如果[-t 1]"是什么意思?在shell脚本中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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