5
\$\begingroup\$

This script is run directly after a fresh install of Debian, to do:

  • sets up syntax highlighting in nano,
  • sets up iptables,
  • sets up ssh,
  • sets up custom bashrc files and ls colors,
  • creates users on the system if needed,
  • checks if user has a password set and sets it if not,
  • installs non-free firmware and sets up apt with virtualbox deb file and multimedia deb.

There was mention of debconf but I've never heard about that.

Would you use sudo as I did to access other users on the system?

Could you add some features to the program that are good practice for setting up new installs?

Can you see a way for me to use getops to strengthen the program?

Is there anything in the program that I don't need?

#!/bin/bash -x
SCRIPTNAME=`basename "0ドル"`
if [ "$#" -eq 0 ]
then
 echo "No arguments supplied"
 echo "Usage: $SCRIPTNAME user1name user2name\(optional\) user3name\(optional\)"
 sleep 10
 exit 27
fi
sleep 5
echo "Setting up server.........."
sleep 10
DIRBASHRCROOT="$HOME"/.bashrcroot
DIRBASHRC="$HOME"/.bashrc
#CURRENTDIR="./"
BASHRC=.bashrc
NANORC=.nanorc
BASHRCROOT=.bashrcroot
ROOT=root
USER1="1ドル"
USER2="2ドル"
USER3="3ドル"
USER_PROGRAMMER=""
SOURCE=sources.list
echo "Please select/provide the port-number for ssh in iptables and sshd_config:"
read port
PORT=$port
################# Make my variable global for all ########################3↓
echo "export CURRENTDIR=\"/tmp/svaka\"" >> /root/.bashrc
touch /etc/profile.d/bashProgrammer.sh
echo "export CURRENTDIR=\"/tmp/svaka\"" >> /etc/profile.d/bashProgrammer.sh
. /root/.bashrc
. /etc/profile
. /etc/profile.d/bashProgrammer.sh
################ Users and access settings #####################
############################### make all files writable, executable and readable in the working directory#########
chmod 777 $CURRENTDIR/*
if [ ! "$?" = 0 ]
then
 echo "Couldnt give write, read and execute permission to config files in svaka, exiting now........"
exit 127
fi
#########################################################
checkIfUser()
{
 for name in "$@"
 do
 if id -u "$name" #>/dev/null 2>&1
 then
 echo "User: $name exists....setting up now\!"
 sleep 5
 else
 echo "User: "$name" does not exists....creating now\!"
 useradd -m -s /bin/bash "$name" #>/dev/null 2>&1
 sleep 5
 fi
 done
}
checkIfUser 1ドル 2ドル 3ドル
################33 user passwords
userPass()
{
 for i in "$@"
 do
 if [ "$i" = root ]
 then
 continue
 fi
 if [[ $(passwd --status "$i" | awk '{print 2ドル}') = NP ]]
 then
 echo "$i doesn't have a password."
 echo "Changing password for $i:"
 echo $i:$i"YOURSTRONGPASSWORDHERE12345Áá" | chpasswd
 if [ "$?" = 0 ]
 then
 echo "Password for user $i changed successfully"
 sleep 5
 fi
 fi
 done
}
userPass 1ドル 2ドル 3ドル
################################################ setting up iptables ####################3
cat << EOT >> /etc/iptables.test.rules
*filter
IPTABLES CODE HERE
COMMIT
EOT
sleep 5
iptables-restore < /etc/iptables.test.rules
sleep 5
iptables-save > /etc/iptables.up.rules
sleep 3
printf "#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules" > /etc/network/if-pre-up.d/iptables
chmod +x /etc/network/if-pre-up.d/iptables
sleep 6
###################################################33 sshd_config
cp -f "$CURRENTDIR/sshd_config" /etc/ssh/sshd_config
sed -i "s/Port 34504/Port $PORT/g" /etc/ssh/sshd_config
chmod 644 /etc/ssh/sshd_config
/etc/init.d/ssh restart
#################################################3333 Remove or comment out DVD/cd line from sources.list
sed -i '/deb cdrom:\[Debian GNU\/Linux/s/^/#/' /etc/apt/sources.list
####################################################33 update system
apt update && apt upgrade -y
##########################################3 Disable login www #########
passwd -l www-data
###############################################################
############################# check if programs installed and/or install
if [ ! -x /usr/bin/git ] || [ ! -x /usr/bin/wget ] || [ ! -x /usr/bin/curl ] || [ ! -x /usr/bin/gcc ] || [ ! -x /usr/bin/make ]
then
 echo "Some tools with which to work with data not found installing now......................"
 apt install -y git wget curl gcc make
fi
#####################################################3 update sources.list
cp -f $CURRENTDIR/$SOURCE /etc/apt/sources.list
chmod 644 /etc/apt/sources.list
wget http://www.deb-multimedia.org/pool/main/d/deb-multimedia-keyring/deb-multimedia-keyring_2016年8月1日_all.deb
dpkg -i deb-multimedia-keyring_2016年8月1日_all.deb
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
apt update && apt upgrade -y
apt install -y vlc vlc-data browser-plugin-vlc mplayer youtube-dl libdvdcss2 libdvdnav4 libdvdread4 smplayer mencoder
sleep 5
apt update && apt upgrade -y
sleep 5
#################################### firmware
apt install -y firmware-linux-nonfree firmware-linux
sleep 5
################ NANO SYNTAX-HIGHLIGHTING #####################3
if [ ! -d "$CURRENTDIR/nanorc" ]
then
 if [ "$UID" != 0 ]
 then
 sudo -u "$ROOT" bash <<'EOF'
 sleep 5
 git clone https://github.com/nanorc/nanorc.git
 sleep 5
 cd nanorc
 make install-global
 sleep 5
 cp -f "$CURRENTDIR/.nanorc" /etc/nanorc
 chown root:root /etc/nanorc
 chmod 644 /etc/nanorc
 if [ "$?" = 0 ]
 then
 echo "Implementing a custom nanorc file succeeded\!"
 else
 echo "Nano setup DID NOT SUCCEED\!"
 fi
EOF
 else
 echo "Doing user: $USER....please, wait\!"
 git clone https://github.com/nanorc/nanorc.git
 sleep 5
 cd nanorc
 sleep 5
 make install-global
 sleep 5
 cp -f "$CURRENTDIR/$NANORC" /etc/nanorc
 chown root:root /etc/nanorc
 chmod 644 /etc/nanorc
 if [ "$?" = 0 ]
 then
 echo "Implementing a custom nanorc file succeeded\!"
 else
 echo "Nano setup DID NOT SUCCEED\!"
 fi
 fi
fi
echo "Finished setting up nano\!"
################ LS_COLORS SETTINGS #############################
if [ "$UID" != 0 ]
then
 echo "This program should be run as root, exiting\! now....."
 exit 1
 # sudo -i -u "$ROOT" bash <<'EOF'
 # BASHRCROOT=.bashrcroot
 # cp "$CURRENTDIR/$BASHRCROOT" "$HOME"/.bashrc
 # wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
 # echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
 # . "$HOME"/.bashrc
#EOF
else
 cp -f "$CURRENTDIR/$BASHRCROOT" "$HOME"/.bashrc
 chown root:root "$HOME"/.bashrc
 chmod 644 "$HOME"/.bashrc
 sleep 5
 wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
 sleep 5
 echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
 sleep 5
 . "$HOME"/.bashrc
fi
for user in "$@"
do
 if [ "$user" = root ]
 then
 continue
 fi
 sudo -i -u "$user" user="$user" bash <<'EOF'
 sleep 5
 cp -f $CURRENTDIR/.bashrc "$HOME"/.bashrc
 chown $user:$user "$HOME"/.bashrc
 sleep 5
 chmod 644 "$HOME"/.bashrc
 sleep 5
 wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
 sleep 5
 echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
 . "$HOME"/.bashrc
EOF
done
echo "Finished setting up your system\!"
echo rm -rf /tmp/svaka

Here's a question about this code on unix/linux stackexchange

Vogel612
25.5k7 gold badges59 silver badges141 bronze badges
asked Sep 4, 2018 at 7:21
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

Welcome to Code Review!

It is a good practice if you start verifying your shell scripts against shellcheck. Currently, it lists quite a few issues:

Line 3:
SCRIPTNAME=`basename "0ドル"`
 ^-- SC2006: Use $(...) notation instead of legacy backticked `...`.
Line 17:
DIRBASHRCROOT="$HOME"/.bashrcroot
^-- SC2034: DIRBASHRCROOT appears unused. Verify use (or export if used externally).
Line 18:
DIRBASHRC="$HOME"/.bashrc
^-- SC2034: DIRBASHRC appears unused. Verify use (or export if used externally).
Line 20:
BASHRC=.bashrc
^-- SC2034: BASHRC appears unused. Verify use (or export if used externally).
Line 24:
USER1="1ドル"
^-- SC2034: USER1 appears unused. Verify use (or export if used externally).
Line 25:
USER2="2ドル"
^-- SC2034: USER2 appears unused. Verify use (or export if used externally).
Line 26:
USER3="3ドル"
^-- SC2034: USER3 appears unused. Verify use (or export if used externally).
Line 27:
USER_PROGRAMMER=""
^-- SC2034: USER_PROGRAMMER appears unused. Verify use (or export if used externally).
Line 31:
read port
^-- SC2162: read without -r will mangle backslashes.
Line 37:
. /root/.bashrc
 ^-- SC1091: Not following: /root/.bashrc was not specified as input (see shellcheck -x).
Line 38:
. /etc/profile
 ^-- SC1091: Not following: /etc/profile was not specified as input (see shellcheck -x).
Line 39:
. /etc/profile.d/bashProgrammer.sh
 ^-- SC1091: Not following: /etc/profile.d/bashProgrammer.sh was not specified as input (see shellcheck -x).
Line 42:
chmod 777 $CURRENTDIR/*
 ^-- SC2086: Double quote to prevent globbing and word splitting.
Line 43:
if [ ! "$?" = 0 ]
 ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
Line 58:
 echo "User: "$name" does not exists....creating now\!"
 ^-- SC2027: The surrounding quotes actually unquote this. Remove or escape them.
 ^-- SC2086: Double quote to prevent globbing and word splitting.
Line 64:
checkIfUser 1ドル 2ドル 3ドル
 ^-- SC2086: Double quote to prevent globbing and word splitting.
 ^-- SC2086: Double quote to prevent globbing and word splitting.
 ^-- SC2086: Double quote to prevent globbing and word splitting.
Line 78:
 echo $i:$i"YOURSTRONGPASSWORDHERE12345Áá" | chpasswd
 ^-- SC2086: Double quote to prevent globbing and word splitting.
 ^-- SC2086: Double quote to prevent globbing and word splitting.
Line 79:
 if [ "$?" = 0 ]
 ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
Line 87:
userPass 1ドル 2ドル 3ドル
 ^-- SC2086: Double quote to prevent globbing and word splitting.
 ^-- SC2086: Double quote to prevent globbing and word splitting.
 ^-- SC2086: Double quote to prevent globbing and word splitting.
Line 126:
cp -f $CURRENTDIR/$SOURCE /etc/apt/sources.list
 ^-- SC2086: Double quote to prevent globbing and word splitting.
Line 165:
 cd nanorc
 ^-- SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
Line 172:
 if [ "$?" = 0 ]
 ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
Line 202:
 echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
 ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.
Line 204:
 . "$HOME"/.bashrc
 ^-- SC1090: Can't follow non-constant source. Use a directive to specify location.

In addition to shellcheck advices; I'd also point that you currently have a very long script executing statements one after the other. Split them into multiple small functions and call them as you deem fit.


Why do you wait 10 seconds when the number of required arguments was not available? Since it is a non-zero exit, user will anyway get sufficiently notified. Same goes for the sleep 5 and 10 just after the parameter count check.


You redirect echo "export CURRENTDIR=\"/tmp/svaka\"" twice to 2 different files. Make use of tee and pipe the echo a single time.


You have a lot of stub code in the script. Clean it up; remove unused statements/declarations/tasks and post another question with the updated code.

answered Sep 10, 2018 at 22:33
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.