From 4e09be15f9c30801e3f7fcedd6bdcfdac1a8162c Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Fri, 11 Apr 2025 15:05:56 -0700 Subject: [PATCH] Created SC3067 (markdown) --- SC3067.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SC3067.md diff --git a/SC3067.md b/SC3067.md new file mode 100644 index 0000000..b8da601 --- /dev/null +++ b/SC3067.md @@ -0,0 +1,33 @@ +## In POSIX sh, test -O is undefined. + +### Problematic code: + +```sh +if [ -O file ] +then + echo "File is owned by you" +fi +``` + +### Correct code: + +```sh +if [ -n "$(find file -prune -user "$(id -u)")" ] +then + echo "File is owned by you" +fi +``` + +### Rationale: + +`test -O` is a bash/ksh/dash/ash extension to check whether the file is owned by you. + +To ensure compatibility with other shells, you can use `find -user "$(id -u)"`. + +### Exceptions: + +If you expect your shell to support `test -O`, 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