My 1st script - please comment

Forum rules
Before you post please read how to get help
sammiev
Level 4
Level 4
Posts: 241
Joined: Sat May 19, 2012 12:16 pm

My 1st script - please comment

Postby sammiev » Sat Oct 28, 2017 9:35 pm

Hi folks, my first script seems to work very well.

I'm sure it could be better.

Please comment.

Many Thanks

Code: Select all

#!/bin/bash
cd /tmp                                                     # switch to tmp directory
wget http://someonewhocares.org/hosts/ipv6/"hosts"          # download data
#
if test -s hosts                                            # file exists and it's size is greater than zero
then
        rm /etc/hosts                                       # remove file hosts from /etc
        cp /home/sam/.etchosts /etc/hosts                   # copy backup of file hosts back to /etc/hosts
        sed -i 1,10d hosts                                  # remove lines 1 to 10 from file hosts
        sed -i 2,66d hosts                                  # remove lines 2 to 66 from file hosts
        sed -i -e 's/127.0.0.1/0.0.0.0/g' hosts             # remove string 127.0.0.1 and replace it with 0.0.0.0
        cat hosts >> /etc/hosts                             # add the data from file hosts to /etc/hosts
        rm hosts                                            # remove tmp file hosts
else
        if test -e hosts                                    # file exists and it's size is likely zero
        then
                rm hosts                                    # remove tmp file hosts
        fi
fi


and the results, just the first few lines.

Code: Select all

127.0.0.1   localhost
127.0.1.1   sam-L500

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

# Last updated: Fri, 27 Oct 2017 at 07:29:49 GMT

#<shock-sites>
# For example, to block unpleasant pages, try:
0.0.0.0 goatse.cx       # More information on sites such as
::1 goatse.cx       # More information on sites such as
0.0.0.0 www.goatse.cx   # these can be found in this article
::1 www.goatse.cx   # these can be found in this article
0.0.0.0 oralse.cx       # en.wikipedia.org/wiki/List_of_shock_sites
::1 oralse.cx       # en.wikipedia.org/wiki/List_of_shock_sites
0.0.0.0 www.oralse.cx
::1 www.oralse.cx
0.0.0.0 goatse.ca
::1 goatse.ca
0.0.0.0 www.goatse.ca
::1 www.goatse.ca
0.0.0.0 oralse.ca
::1 oralse.ca
0.0.0.0 www.oralse.ca
::1 www.oralse.ca
0.0.0.0 goat.cx
Image

User avatar
WharfRat
Level 20
Level 20
Posts: 10280
Joined: Thu Apr 07, 2011 8:15 pm

Re: My 1st script - please comment

Postby WharfRat » Sat Oct 28, 2017 9:45 pm

If you want a real critique of your script install shellcheck and run shellcheck your_script :wink:
Image ImageImage

User avatar
trytip
Level 5
Level 5
Posts: 868
Joined: Tue Jul 05, 2016 1:20 pm

Re: My 1st script - please comment

Postby trytip » Sat Oct 28, 2017 10:22 pm

small and simple script and you're on the right track altho it would be better to backup the current etc/hosts to something like hostsBAK or similar. i'm not good at scripts but i do use someonewhocares and updating can get annoying at times

keep at it
I love Windows, they give my walls personality.

sammiev
Level 4
Level 4
Posts: 241
Joined: Sat May 19, 2012 12:16 pm

Re: My 1st script - please comment

Postby sammiev » Sat Oct 28, 2017 10:39 pm

Thanks WharfRat and point taken. :wink:

Thanks Trytip, I do have the original hosts file backup. If the data dosn't come through or has 0 bytes the file is not written over. :)
Image

sammiev
Level 4
Level 4
Posts: 241
Joined: Sat May 19, 2012 12:16 pm

Re: My 1st script - please comment

Postby sammiev » Sat Oct 28, 2017 11:11 pm

@WharfRat,

Used ShellCheck and it reported one correction

"cd /tmp" to "cd /tmp || exit"

Many Thanks :D
Image

User avatar
Termy
Level 3
Level 3
Posts: 170
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: My 1st script - please comment

Postby Termy » Sun Nov 05, 2017 8:15 pm

You're more than welcome to PM me if you'd like to talk shell (or Linux in general) with me. :) I'm a huge fan of both.

For now, here's something to consider:

Code: Select all

#!/bin/bash

# Check for having root access.
[ $UID -eq 0 ] || echo "ERROR: Root access is required." 1>&2
#       ↑             ↑                                   ↑   
#     If fails, then error.                  Redirects STDOUT to STDERR

# Runs quietly                Quoting protects from shell interpretation
#     ↓                                             ↓
wget -q "http://someonewhocares.org/hosts/ipv6/hosts"

#  No need to cd, thanks
#    to absolute paths
#               ↓
if test -s /etc/hosts
then
        rm /etc/hosts
        cp -i $HOME/.etchosts /etc/hosts
                ↑
#     Applicable for different user names
#         $HOME stores /home/somebody
#         $USER stores your username

        # Delete lines 1-10, then 2-66, and then substitute 127.0.0.1 for 0.0.0.0 on all lines.
        sed -i '1,10d; 2,66d; s/127.0.0.1/0.0.0.0/g' /etc/hosts
                            ↑
#                           One way to string together sed expressions.
#                           Another way is to use -e (expression) which
#                           allows you to quote each one.

        # Note that you will probably wind up with duplicates -- could use 'uniq'.
        cat /tmp/hosts >> /etc/hosts
        rm /etc/hosts
else
        [ -e /etc/hosts ] && rm /etc/hosts
fi
Last edited by Termy on Sun Nov 26, 2017 1:28 pm, edited 1 time in total.
Here to help.

sammiev
Level 4
Level 4
Posts: 241
Joined: Sat May 19, 2012 12:16 pm

Re: My 1st script - please comment

Postby sammiev » Sun Nov 05, 2017 9:29 pm

Thanks Termy,

I added a little more code to my script and willing to learn.

Will look at yours soon and many thanks for the info an invite to the PM. :D

edit: I will throw the reset of my script up tomorrow as it will likely be a few days before getting back to it.
Image


Return to “Scripts & Bash”