Bash子shell,用于在脚本中设置SHELLOPTS [英] Bash subshell for setting SHELLOPTS in a script

查看:427
本文介绍了Bash子shell,用于在脚本中设置SHELLOPTS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题不是Cygwin特有的.但是,在cygwin邮件归档文件中, https://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html 是用于设置cygwin特定的igncr shellopt变量的各种指令,其中之一是指令:

This question is not cygwin specific. However, in the cygwin mail archive https://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html are various instructions for setting the cygwin specific igncr shellopt variable and one of them is the instruction:

4a.对于单个受影响的脚本,请在 she-bang:〜(set -o igncr)2>/dev/null&&设置-o igncr; #条评论是 需要

4a. For a single affected script, add this line just after the she-bang: ~ (set -o igncr) 2>/dev/null && set -o igncr; # comment is needed

我知道set -o igncr在SHELLOPTS中设置igncr.但是,我不明白为什么该指令还包括在子shell中调用它.据我了解,子shell的变量和环境不会在父进程中停留.它有什么用?

I understand that set -o igncr sets igncr in SHELLOPTS. However, I do not understand why the instruction also includes invoking it in a subshell. From what I understand, the variables and environment of the subshell do not stick around in the parent process. What is the use of it?

推荐答案

此命令中此处重要的细节

The detail that is material here in this command

(set -o igncr) 2>/dev/null && set -o igncr

是上下文.

想象一下一个旨在在非Windows机器和cygwin上运行的shell脚本.

Imagine a shell script designed to run on non-Windows machines as well as in cygwin.

通常(不是在cygwin下),使用set -o igncr会出现错误(正试图设置任何其他无效的set选项.

Normally (not under cygwin) the use of set -o igncr would be an error (as is attempting to set any other invalid set option.

但是,在cygwin下,set既有意义又有用.

However, under cygwin that set is both meaningful and helpful.

那么,您如何编写在两个地方都可以运行的脚本?您需要针对当前环境进行测试.您可以检查特定的环境变量,特定的路径等.(这类似于从浏览器嗅探和功能测试的转变),您只需检查当前环境是否支持该选项即可.您正在尝试设置.

So how do you write a script that operates in both places? You need a test for the current environment. You could check for specific environment variables, specific paths, etc. or (and this is similar to the move away from browser-sniffing and to feature testing) you can just check whether the current environment supports the option you are attempting to set.

因此,尝试设置它,但是如果它不起作用,则会抛出错误,因此,请尝试将其设置在子外壳中(并将错误输出扔到/dev/null来忽略它),然后是(仅当),则设置成功(&&),然后也在当前的shell环境中进行设置.

So try to set it but that throws an error if it doesn't work so try to set it in a sub-shell (and toss the error output to /dev/null to ignore it) and then if (and only if) that set succeeds (&&) then do the set in the current shell environment as well.

这篇关于Bash子shell,用于在脚本中设置SHELLOPTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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