My 1st script - please comment

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
sammiev

My 1st script - please comment

Post by sammiev »

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
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
WharfRat

Re: My 1st script - please comment

Post by WharfRat »

If you want a real critique of your script install shellcheck and run shellcheck your_script :wink:
User avatar
trytip
Level 14
Level 14
Posts: 5371
Joined: Tue Jul 05, 2016 1:20 pm

Re: My 1st script - please comment

Post by trytip »

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
Image
sammiev

Re: My 1st script - please comment

Post by sammiev »

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. :)
sammiev

Re: My 1st script - please comment

Post by sammiev »

@WharfRat,

Used ShellCheck and it reported one correction

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

Many Thanks :D
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: My 1st script - please comment

Post by Termy »

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.
I'm also Terminalforlife on GitHub.
sammiev

Re: My 1st script - please comment

Post by sammiev »

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.
Locked

Return to “Scripts & Bash”