What this is good at, and what it is not
Its real advantage
A hosts file is consulted before a browser's own encrypted DNS, so unlike DNS filtering it is not bypassed by turning on Secure DNS or switching resolver. That is the most common way people defeat a filter, and it does not work here. It also needs no account, works offline, and sends nothing to anyone.
Its real limit
There are no wildcards. Each line matches one exact hostname, so a site you block may still load on a subdomain you did not list. This is precise rather than comprehensive, which makes it excellent for a specific list of sites and a poor choice as your only layer.
The script can make the file unwriteable, but an administrator can always undo that in a couple of commands. This only becomes a control if your daily account is a standard account and someone else holds the administrator password. If you are an administrator on your own machine, treat this as a speed bump you have chosen, which is still worth having.
Build your script
What to pair this with
On a phone instead?
iPhone and iPad take a configuration profile, which does more than this. Start there.
Add filtered DNS
Catches whole categories rather than a list you maintain by hand. The two complement each other.
Make it hold
The account split, disk encryption, and why the administrator password is the real control.