From 06e5ab67a59770ce791c66445b0e22189c5704a6 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Fri, 11 Apr 2025 15:04:20 -0700 Subject: [PATCH] Created SC3066 (markdown) --- SC3066.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 SC3066.md diff --git a/SC3066.md b/SC3066.md new file mode 100644 index 0000000..f0a728e --- /dev/null +++ b/SC3066.md @@ -0,0 +1,32 @@ +## In POSIX sh, test -G is undefined. + +### Problematic code: + +```sh +if [ -G file ] +then + echo "File is owned by your effective group" +fi +``` + +### Correct code: + +```sh +if [ -n "$(find file -prune -group "$(id -g)")" ] +then + echo "File is owned by your effective group" +fi``` + +### Rationale: + +`test -G` is a bash/ksh/dash/ash extension to check whether the file is owned by your effective group. + +To ensure compatibility with other shells, you can use `find -group "$(id -g)"`. + +### Exceptions: + +If you expect your shell to support `test -G`, specify this explicitly in the shebang (or with `# shellcheck shell=dash` directive). + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file