Bash subshell for setting SHELLOPTS in a script
Asked Answered
E

2

4

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. 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

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?

Epistyle answered 12/5, 2015 at 3:3 Comment(0)
U
2

The detail that is material here in this command

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

is the context.

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

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

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.

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.

Underpinning answered 12/5, 2015 at 3:12 Comment(1)
ah, that makes perfect sense. Thank you!Epistyle
I
0

from within the script, try:

[[ $(uname -o) =~ Cygwin ]] && echo "this is Cygwin" || echo "this is not Cygwin"

replace the 2 endpoints of this ternary construction with appropriate things; set a variable you can look at, etc.

BE AWARE! This has a potential 'gotcha':

If the right-hand side of the && condition has a nonzero exit status, it will silently default to the or (||) expression.

[[ cond ]] && op1 || op2 ➜ op2 will be selected if op1 fails.

Takeaway: Be mindful of the op1 operation. Ensure it exits with a 0 status code, or you may receive a false negative. In fact, if the cond is true and op1 exits with a non-zero status, you will get BOTH op2 and op2 executing.

Intrauterine answered 17/9, 2021 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.