In a MacOS Mojave bash script "-w /etc/passwd" is false even for root
Checking to see if a file is &quot;writable&quot; as a way to determine if a user is root is a bad way to check permissions.  For instance....

you can add a user to a group (i.e. wheel) that has write permissions to a file that's normally owned by root

a file can be flagged to not have write permissions even by root even though you have root access.

a file may have had its permissions changed to &quot;non-writable&quot; by root (intentionally or not)

SIP, or System Integrity Protection (as you've already discovered) will prevent even the root user from writing to system files.


Check for the User ID
Instead, you should look at the UID of a particular user with the function id
$ id -u

A root user, will have an ID of 0.
To see how you can use this in a script to check for root privileges, see the answer in the post Running a Script with Administrator Privileges.
Checking SIP
This is actually pretty easy....
$ csrutil status

However, if it is enabled, you will have to boot to Recovery Mode to disable SIP.  This isn't something you can do on the command line (or from a script) in a normal session.  It is much easier to simply test for the UID as described above.