After install script-testing and improving

Hello
I made a script for your own use (very useful if you constantly have to install new machines), as you can see it adds new repo, codecs and software, and also makes some changes to the default ssh, sshd, and others. The problem is that very often zypper does not install the necessary programs, although use-

zypper in -y prog1 prog2 prog3 etc

.
It seems that this method is not very good for some reason!

Also use this method, but it is too slow - it creates repetition of already past processes .

IFS=$','
soft[1]="autossh"
soft[2]="rkhunter"
soft[3]="lynis"
soft[4]="fail2ban"
soft[5]="sshguard"
soft[6]="chkrootkit"
soft[7]="aide"

cnt=${#soft@]}
for (( i = 1 ; i < cnt ; i++ ))
	do
	j=0
	for PART in ${soft$i]}
		do
		if  $j == 0 ] ; then
			s=$PART
			j=1
		fi
	done
	zypper install -y -l "$s"

done

Also at the beginning of the script has a system for self-update, which does not work yet because the problem with the returned error codes…

 check_version()->error_handler()->script_self_updater()

I would be grateful to anyone who has an idea to improve the script!

Script- OSCSI

#!/bin/bash
#OSCSI
#variables
Warn="e[5;31me[49m" #red bl
Err="e[1;33me[40m" #yel
Stand="e[1;34me[49m" #blu
Quest="e[1;31me[49m" #red
Mess="e[1;36me[49m" #lth blu
Comm="e[1;35me[49m" #lil
Norm="e[1;39me[m" #Green
PlainLine='###################################################################'
Line=${Stand}$PlainLine${Norm}
MessageLine=${Mess}$Line${Norm}
Version='1210'
VersionDate='2012-04-18'
downloadUrl='http://cazadordevarna-kennel.com/OSCSI'
SUSE_VERSION=$( lsb_release -s -r )
DIS=$(lsb_release -d |cut -d\( -f1|cut -f2 )
x=$(lsb_release -s -r | cut -d\. -f2)
PROG='Open Suse Codecs and Softwear Instaler ${Quest} $Version${Norm}'
USR=$(logname)
INSDIR="/home/$USR/INSTAL"



error_handler(){
	local error_code='' message=''
	case $1 in
		noroot)
			message="${Warn} - This script must be run as root!!${Norm}
${Comm} - Tipe ${Err}'su'${Comm} comand, hit ${Err}'ENTER'${Comm} and then execute script!!!${Norm}"
			error_code=1

			;;
                noupdate)
			message="${Quest}You have highest available version already !.${Norm}"
			error_code=0
			;;
		wgetfailed)
			message="${Warn}OSCSI self-update download exited with errors.
Maybe the server is offline or changed? Exiting  now.${Norm}"
			error_code=3
			;;
		baddownload)
			message="${Warn}OSCSI self-update file download appears to be corrupted.
Try updating again. Exiting  now.${Norm}"
			error_code=4
			;;
	esac
	echo -e ${Line}
	echo -e "$message ${Stand}(${Comm}$error_code${Stand})${Norm}"
	echo -e ${Line}

}


start_tests () {

	if [ "$(whoami)" != "root" ];then
		error_handler noroot;exit
	fi
}
print_version(){
	echo $Line
	echo "${Stand}Currently installed OSCSI version:"
	echo "Version Number: $Version"
	echo "Version Date: $VersionDate${Norm}"
}


check_version(){
  sv=$(wget -qO- $downloadUrl |head -n20|grep "Version="|cut -d\= -f2|sed "s/'/ /g"|sed 's/\.//g')
  if  "$sv" -gt "$Version" ]
    then script_self_updater
  else error_handler 'noupdate'
       exit 0
  fi
}

WARNUP() {
x=0
while  $x = 0 ]
do
      clear
      echo $Line
      echo  "${Quest}      IF you NOT update system after install,  ${Norm}"
      echo  "${Warn}       please EXIT and update FIRST!!! ${Norm}"
      echo $Line
      echo "${Quest}       CONTINUE ??? Y/N ${Norm}"
      read ans
       case "$ans" in
             Y)
             echo $Line
             echo "${Mess} OK, if you update, now CONTINUE.... ${Norm}"
             echo $Line
             echo ""; break;os_version
             x=0
             ;;
             N)
	     echo $Line
             echo "${Mess} OK!YOU NOT UPDATE, NOW EXIT!.... ${Norm}"
             echo $Line
             echo ""; exit 1
             x=1
             ;;
             *)
             clear
             echo "It is not answer!y or n"
             sleep 2
             ;;

      esac
done
sleep 3
clear
}

os_version () {
if  $x -eq 1 ]
   then
echo $Line
echo "${Stand} OPEN SUSE CODECS and SOFTWARE INSTALER (OSCSI) FOR openSUSE ${SUSE_VERSION}${Norm}"
echo $Line
sleep 3
clear

echo "======================================================================="
echo "=                                  @@@@@  @     @  @@@@@  @@@@@@      ="
echo "=                                 @     @ @     @ @     @ @           ="
echo "=      @@@@  @@@@@  @@@@@  @@   @ @       @     @ @       @           ="
echo "=     @    @ @    @ @      @ @  @  @@@@@  @     @  @@@@@  @@@@@       ="
echo "=     @    @ @@@@@  @@@@@  @  @ @       @ @     @       @ @           ="
echo "=     @    @ @      @      @   @@ @     @ @     @ @     @ @           ="
echo "=      @@@@  @      @@@@@@ @    @  @@@@@   @@@@@   @@@@@  @@@@@@      ="
echo "======================================================================="
sleep 3
echo "${Warn}<<<Installation begins......................${Norm}"
   else echo $Line
        echo "${Quest} Your version is not ${SUSE_VERSION} Exiting!${Norm}"
        echo $Line
        sleep 3
        clear
        exit
fi
}


script_self_updater(){
	local script_location=$( which OSCSI ) temp_data='' good_data=''

	if  -n "$script_location" ];then
		start_tests #check for root
		print_version
		echo $Line
		echo "${Stand}Starting OSCSI self updater.${Norm}"
		temp_data="$( wget -q -O - $downloadUrl )" || error_handler 'wgetfailed'
		good_data=$( grep '#file-download-good#' <<< "$temp_data" )
		# try it again just in case it fails
		if  -z "$good_data" ];then
			echo "${Stand}First download attempt failed, trying it again...${Norm}"
			# no -q because want to see error output on second failure
			temp_data="$( wget -O - $downloadUrl )" || error_handler 'wgetfailed'
			good_data=$( grep '#file-download-good#' <<< "$temp_data" )
		fi
		# then process it and restart
		if  -n "$good_data" ];then
			echo "$temp_data" > $script_location
			chmod 705 $script_location
			echo "${Stand}Success! Restarting OSCSI now to new version.${Norm}"
			OSCSI
			exit 0
		else
			error_handler 'baddownload'
		fi
	else
		echo "${Err}OSCSI is not in your system PATH. Unable to update.${Norm}"
		exit 0
	fi
}


start_tests
WARNUP
check_version
script_self_updater
os_version


# set up some common repositories on an OpenSUSE 12.1 system.

clear
echo $Line
echo "${Mess} Setting up repositories... FOR openSUSE ${SUSE_VERSION}${Norm}"
echo $Line
sleep 3

IFS=$','
repo[1]="Packman,http://packman.inode.at/suse/openSUSE_${SUSE_VERSION}/"
repo[2]="DVD,http://opensuse-guide.org/repo/${SUSE_VERSION}/"
repo[3]="KDE48,http://download.opensuse.org/repositories/KDE:/Release:/48/openSUSE_${SUSE_VERSION}/"
repo[4]="Extra48,http://download.opensuse.org/repositories/KDE:/Extra/KDE_Release_48_openSUSE_${SUSE_VERSION}/"
repo[5]="Security,http://download.opensuse.org/repositories/security/openSUSE_${SUSE_VERSION}/"
repo[6]="Security,http://download.opensuse.org/repositories/security/openSUSE_${SUSE_VERSION}/"
######################################################################################################
cnt=${#repo@]}
for (( i = 0 ; i < cnt ; i++ ))
	do
	j=0
	for PART in ${repo$i]}
		do
		if  $j == 0 ] ; then
			name=$PART
			j=1
		else
			url=$PART
			j=0
		fi
	done
	zypper ar -c "$url" "$name"
	zypper mr -r "$name"
	zypper --no-gpg-checks --gpg-auto-import-keys refresh -f "$name"
done
echo $Line
echo "${Mess} SET UP SOFTWARE REPOSITORIES finished... (make sure to remove duplicates)${Norm}"
echo $Line
sleep 3
clear

#########################################################################
#VGA Proprietary driver install
vga=`/sbin/lspci | grep VGA|awk '{print $5}'`
ur1="ftp://download.nvidia.com/opensuse/${SUSE_VERSION}/"
ur2="http://www2.ati.com/suse/${SUSE_VERSION}/"
if  $vga = "nVidia"]
     then
          zypper ar "$ur1" "$vga"
	  zypper mr -r "$vga"
	  zypper --no-gpg-checks --gpg-auto-import-keys refresh -f "$vga"
          zypper inr -y -l --recommends -r "$ur1" nvidia-computeG02
elif  $vga = "ATI" ]
     then
          zypper ar "$ur2" "$vga"
	  zypper mr -r "$vga"
	  zypper --no-gpg-checks --gpg-auto-import-keys refresh -f "$vga"
          zypper inr -y -l --recommends -r "$ur2" ##ati dep !!!
fi


###########################################################################
# allowVendorChange-temporaly
cd /etc/zypp/
cat zypp.conf |awk '/solver.allowVendorChange/ { gsub(/false/, "true") };
/# solver.allowVendorChange/ { gsub(/#/,"")}; {print}'>>zp
mv zp zypp.conf
cd
##########################################################################
#distribution_upgrade to KDE48
echo $Line
echo "${Mess} BEGIN UPGRADE DISTRIBUTION...${Warn}Wait!!${Norm}"
echo $Line
zypper dup -y --from KDE48

#########################################################################
echo $Line
echo "${Mess} BEGIN INSTALL SOFTWARE.... ${Norm}"
echo $Line
sleep 3
clear

zypper in -r Packman -f -y k3b gstreamer-0_10-plugins-base libxine1
zypper in -y -l --from Packman k3b-codecs gstreamer-0_10-fluendo-mp3 gstreamer-0_10-fluendo-mpegdemux gstreamer-0_10-fluendo-mpegmux gstreamer-0_10-plugins-good-extra gstreamer-0_10-plugins-ugly gstreamer-0_10-plugins-bad gstreamer-0_10-ffmpeg libxine1-codecs k3b-codecs libquicktime0 ffmpeg x264 transcode normalize libdvdcss2 mediainfo
zypper in -y -l flac acetoneiso2 ImageMagick-extra wine stardict yakuake fetchmsttfonts akonadi-googledata gtk2-devel libcurl-devel freetype LinuxLibertine unrar rar unison vlc MPlayer smplayer nmap
zypper in -y gcc


wait
clear
echo $Line
echo "${Mess} INSTALL CODECS is finished... ${Norm}"
echo $Line
sleep 3
clear
#######################################################
sec () {
echo $Line
echo "${Mess} BEGIN INSTALL SOFTWARE.... ${Norm}"
echo $Line
sleep 3
clear

zypper in -y -l autossh rkhunter lynis fail2ban sshguard chkrootkit aide

}

############################################
#set rkhunter
setrkh() {
cat /etc/rkhunter.conf |awk '/ALLOW_SSH_ROOT_USER/ { gsub(/yes/, "no") };
/MAIL-ON-WARNING/ { gsub(/""/, "mail@gmail.com")}; { print }'>>rh
mv rh /etc/rkhunter.conf
}
###############################################
x=0
while  $x = 0 ]
do
     clear
     echo "${Mess} DO YOU LIKE TO INSTALL SECURITY SOFTWARE ???(y/n) ${Quest}"
     read ans
     case "$ans" in
             y)
             echo $Line
             echo "${Mess} OK, BEGIN INSTALL SECURITY SOFTWARE.... ${Norm}"
             echo $Line
             echo ""; sec ;setrkh
             x=1
             ;;
             n)
	     echo $Line
             echo "${Mess} OK!CONTINUE INSTALLATION!.... ${Norm}"
             echo $Line
             echo ""; break
             x=1
             ;;
             *)
             clear
             echo "It is not answer!y or n"
             sleep 2
             ;;

      esac
done

zypper rr  DVD

############################################
#set MTU-if needеd only!

cat /etc/sysconfig/network/ifcfg-eth0|awk '{sub('/MTU=\'\'/',"MTU='\'1500\''"); print}'>>nt
mv nt /etc/sysconfig/network/ifcfg-eth0

############################################
#set cron

cat /etc/sysconfig/cron |awk '/CLEAR_TMP_DIRS_AT_BOOTUP/ { gsub(/no/, "yes") };
/MAX_DAYS_IN_TMP/ { gsub(/0/, "1") };
/LONG_TMP_DIRS_TO_CLEAR/ { gsub(/""/, "\"/var/tmp\"") };
/MAX_DAYS_IN_LONG_TMP/ { gsub(/0/, "28") }; { print }'>>cr
wait
mv cr /etc/sysconfig/cron

############################################
#set sshd
#error:: ->192.168.1.102,0.0.0.0->192.168.1.101!!
#PermitRootLogin error!

cat /etc/ssh/sshd_config |awk '/ALLOW_SSH_ROOT_USER/ { gsub(/yes/, "no") };
/#Protocol 2/ { gsub(/#/,"")};
/PermitRootLogin/ { gsub(/yes/, "no") };
/PermitRootLogin/ { gsub(/#/, "") };
/ListenAddress 0.0.0.0/ { gsub(/#/, "") };
/ListenAddress ::/ { gsub(/#/, "") };
/ListenAddress 0.0.0.0/ { gsub(/0.0.0.0/, "192.168.1.101")};
/ListenAddress ::/ { gsub(/::/, "192.168.1.102")}; {print}'>>sc
mv sc /etc/ssh/sshd_config
############################################
#set ssh

cat /etc/ssh/ssh_config |awk '/#   Protocol 2/ {gsub(/#/, " ")}; {print}'>>seh
mv seh /etc/ssh/ssh_config

############################################
#set rkhunter

cat /etc/rkhunter.conf |awk '/ALLOW_SSH_ROOT_USER/ { gsub(/yes/, "no") };
/MAIL-ON-WARNING/ { gsub(/""/, "mail@gmail.com")}; { print }'>>rh
mv rh rkhunter.conf

############################################
export GREP_OPTIONS='--color=always'

###########################################
#set iptables
/usr/sbin/iptables -A OUTPUT -p tcp -d 192.168.1.101 -m mport --dport 11900:12100 -j DROP

/usr/sbin/iptables-save >/home/$USR/iptables.log

###########################################################################
# allowVendorChange-recover

cat /etc/zypp/zypp.conf |awk '/solver.allowVendorChange/ { gsub(/true/, "false") };{print}'>>zp
#/# solver.allowVendorChange/ { gsub(/#/,"")};
mv zp /etc/zypp/zypp.conf
cd
########################################################################
#Install Skype!
echo $Line
echo "${Mess} Install SKYPE ${Norm}"
echo $Line
pks="/home/$USR/Programs/skype-*"
si=$(uname -i)

if  $si = "x86_64" ]
         then zypper in -y libasound2-32bit xorg-x11-libXv-32bit xorg-x11-libs-32bit libqt4-x11-32bit libpng12-0-32bit
         if  -e $pks ]
                then zypper in -y -l $pks
         else
	 echo $Line
	 echo "${Mess}Downloading skype.${Warn}Waite!...${Norm}"
	 echo $Line
         wget -O /home/$USR/INSTAL/suse.rpm http://www.skype.com/go/getskype-linux-beta-suse
         zypper in -y -l /home/$USR/INSTAL/suse.rpm

         fi

else  $si = "i586" ]

         if  -e $pks ]
                then zypper in -y -l $pks
         else
	 echo $Line
	 echo "${Mess}Downloading skype.${Warn}Wait!!...${Norm}"
	 echo $Line
         wget -O /home/$USR/INSTAL/suse.rpm http://www.skype.com/go/getskype-linux-beta-suse
         zypper in -y -l /home/$USR/INSTAL/suse.rpm

         fi
fi

wait


rm -rf /home/$USR/INSTAL

exit 0
#file-download-good#