mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC3067 (markdown)
33
SC3067.md
Normal file
33
SC3067.md
Normal file
@@ -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!
|
Reference in New Issue
Block a user