How to make a script for automated editing

Hi, I wonder how i can make a script to automatically generate a batchfile and add commands into it.
The whole purpose of it is, to have a script i can run at an installation and let these task be done automatically.

I do need to learn scripting i think, but if there is a good guide someone can recommend me i am delighted to read it.

Well, i hope i explained it right so you, the reader, knows what i want to accomplish. :slight_smile:

So far, i did an automated script for installing libs and adding repos. Works well so far.

#!/bin/bash

# adding repos for non free codecs

zypper addrepo -f http://packman.inode.at/suse/11.4/packman
zypper addrepo -f http://www.opensuse-guide.org/repo/11.4 libdvdcss

# adding libraries for non free codecs

zypper in libavcodec52 libavdevice52 ffmpeg libmpeg libdvdnav4 libdvdread4 libquicktime0 libxine1 libxine1-codecs libxvidcore4 smplayer vlc w32codec-all xine-vi xvidcore lame

# adding repos for non free Nvidia graphics driver

zypper addrepo -f http://download.nvidia.com/opensuse/11.4 NVIDIA Repository
zypper addrepo -f http://download.opensuse.org/update/11.4 Main Update

# adding driver for non free Nvidia graphics card

zypper install x11-video-nvidiaG02

# adding repo for non free Skype

zypper addrepo -f http://download.opensuse.org/repositories/home:/broumbroum23/openSUSE_11.3_NonFree_standard/ Skype

# adding libs for Skype

zypper install libpng12.so.0 libqt4-32bit libqt4-dbus-1-32bit libqt4-x11-32bit libsigc++2-32bit xorg-x11-libXv-32bit


So now, all i need is a command to add lines in a scriptfile and of course generate it. The generating part is easy, but can i add a line just by using vi without calling vi.

Thanks

To add lines to a file you use the >> file redirection indicator. Like

echo "This lines is added at the end of the file" >>script

For more difficult actions read

man ed

Also the line edidotor guises of vi could be interesting: ex/edit. Again see their man pages.

Thanks. I think thats what i am looking for.

I will post my script once its done. Maybe its someone else can use it too.

To make it more difficult for you what to choose :wink:

There is also sed. But be carefull, as it’s name states it is a “stream editor” and you must therefore not write the output of it directly to the input file, because you will overwrite your own input file before it is read completley. Thus something like:

sed -e ' .. ' -e ' .. ' input >output
mv output input

(where ’ … ’ ) are of course the several edit statements) will first write output and then overwrite input.

The others work with a buffer and will overwrite the input only after a w command (same as vi).

This works already perfect. The only thing it can’t do is to add #!/bin/bash. Have to look that up…
Thanks for that tip since it solved my problem. :slight_smile:

Without further explanation I cannot be of much help. When you add statements to an existing script the #!/bin/bash is allready there. When you generate a new script an

echo '#!/bin/bash' >yourscipt

at the beginning will create yourscipt and put that first line in it. What is the problem?

It’s been a while since I have done it, but something like:


1i\
#! /bin/bash

(as part of a sed script) should do that for you.

(added in edit - thatis a digit “1” before the letter “i” for use in sed. After posting, I noticed that it looked a bit like a lower case “L”.

Hi, was just overlooking your post. I was wondering do you guys have any infomation on scripts that will automatically extract a file towards my customers destination (like to their usb or floppy disk etc…etc…)?
Plus, allowing them to download the file only once without copying it to another drive or destination? I have searched the internet day in and out but still have not came across such a script, if any exsist at all. If you have any info on this…please share! Thanks :shame:

This is a completely new question and has no relation with the above. When you want that people help you with this new question then please start a new thread in Programming/Scripting with a telling title. That will draw people to your problem. Now you are hiding your problem beneath an old allready drowned thread and almost nobody will see it.

It is for my usage ,and it is not perfect steel…

#!/bin/bash
# Bash script to set up some common repositories on an OpenSUSE 11.4 system.
printf "\e[1;34m\e ###################################################################
"
printf "\e[1;33m\e SCRIPT TO SET UP SOFTWARE AND REPOSITORIES FOR openSUSE 11.4
"
printf "\e[1;34m\e ###################################################################
"
#printf "\e[5;31m\e ###################################################################
"
#printf "\e[0;32m\e ###################################################################

"
sleep 3
# Make sure we are root.
if [ "$EUID" != "0" ]; then
  printf "\e[5;31m\e!!! - This script must be run as root. - !!!

"
  printf "\e[0;32m\e ###########################################

"
  exit 1
fi


SUSE_VERSION=$(awk '/^VERSION/{print $3}' /etc/SuSE-release)

x=$(cat /etc/SuSE-release |grep VERSION | cut -d\. -f2)

if [ $x -eq 4 ]
   then 

#clear
printf "\e[0;32m\e"
echo "======================================================================="
echo "=                                  #####  #     #  #####  ######      ="
echo "=                                 #     # #     # #     # #           ="
echo "=      ####  #####  #####  ##   # #       #     # #       #           ="
echo "=     #    # #    # #      # #  #  #####  #     #  #####  #####       ="
echo "=     #    # #####  #####  #  # #       # #     #       # #           ="
echo "=     #    # #      #      #   ## #     # #     # #     # #           ="
echo "=      ####  #      ###### #    #  #####   #####   #####  ######      ="
echo "======================================================================="
sleep 5
#clear


printf "\e[1;33m\e <<<Installatin begins...
"

printf "\e[0;32m\e"
   else printf "\e[5;31m Your version is not 11.4 Exiting!
 "
printf "\e[0;32m\e"
        sleep 4
        clear
        exit
fi

#============================================================


#clear



printf "\e[1;34m\e ###################################################################
"
printf "\e[1;33m\e Setting up repositories... FOR openSUSE 11.4
"
printf "\e[1;34m\e ###################################################################
"
sleep 3
printf "\e[0;32m\e"

IFS=$','
repo[1]="Packman,http://packman.unixheads.com/suse/11.4/"
repo[2]="DVD Libraries,http://opensuse-guide.org/repo/11.4/"
repo[3]="KDE Updated,http://download.opensuse.org/repositories/KDE:/UpdatedApps/openSUSE_11.4/"
repo[4]="KDE Extra,http://download.opensuse.org/repositories/KDE:/Extra/openSUSE_11.4/"
repo[5]="Factory Contrib,http://download.opensuse.org/repositories/openSUSE:/Factory:/Contrib/openSUSE_11.4"
repo[6]="skype,http://download.opensuse.org/repositories/home:/broumbroum23/openSUSE_11.3_NonFree_standard/"
repo[7]="google-talkplugin,http://dl.google.com/linux/talkplugin/rpm/stable/x86_64"
#repo[8]="nVidia,ftp://download.nvidia.com/opensuse/11.4/"
#repo[9]="ATI/AMD,http://www2.ati.com/suse/11.4/"
#repo[10]="ATI/AMD fglrx non-official,http://linux.ioda.net/mirror/ati/openSUSE_11.4/"
#repo[11]="VLC,http://download.videolan.org/pub/vlc/SuSE/11.4"
#repo[12]="OpenSUSE 11.4-Updates,http://download.opensuse.org/update/11.4/"
#repo[13]="OpenSUSE 11.4-OSS,http://download.opensuse.org/distribution/11.4/repo/oss/"
#repo[14]="openSUSE 11.4 Source,http://download.opensuse.org/source/distribution/11.4/repo/oss/"
#repo[15]="openSUSE 11.4 Non-OSS,http://download.opensuse.org/distribution/11.4/repo/non-oss/"
#repo[16]="openSUSE 11.4 Debug,http://download.opensuse.org/debug/distribution/11.4/repo/oss/"
#repo[17]="Tumbleweed,http://download.opensuse.org/repositories/openSUSE:/Tumbleweed/standard/"
#repo[18]="Tumbleweed-Packman,http://packman.inode.at/suse/openSUSE_Tumbleweed/Essentials"

######################################################################
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 "$url" "$name"
	zypper mr -r "$name"
	zypper --no-gpg-checks --gpg-auto-import-keys refresh -f "$name"
done

printf "

################################################################################
"
printf "\e[1;33m\e SET UP SOFTWARE REPOSITORIES finished... (make sure to remove duplicates):
"
printf "####################################################################################

"
printf "\e[0;32m\e"
sleep 3
clear
printf "

################################################################################
"
printf "\e[1;33m\e BEGIN INSTALL SOFTWARE.... 
"
printf "####################################################################################

"
printf "\e[0;32m\e"
sleep 3
clear

IFS=$','
soft[1]="acetoneiso2 "
soft[2]="vlc"
soft[3]="MPlayer"
soft[4]="SMPlayer"
soft[5]="wine"
soft[6]="stardict"
soft[7]="unison"
soft[8]="vlc-mozillaplugin"
soft[9]="yakuake"
soft[10]="rkhunter"
soft[11]="chkrootkit"
soft[12]="dropbox"
soft[13]="gtk2-devel"
soft[14]="libcurl-devel"
soft[15]="make"
soft[16]="gcc"
soft[17]="libdvdcss2"
soft[18]="ffmpeg"
soft[19]="flac"
soft[20]="k3b-codecs"
soft[21]="libxine1-codecs"
soft[22]="phonon-backend-xine"
soft[23]="w32codec-all"
soft[24]="gstreamer-0_10-ffmpeg"
soft[25]="gstreamer-0_10-fluendo-mpegdemux"
soft[26]="gstreamer-0_10-fluendo-mpegmux"
soft[27]="gstreamer-0_10-plugins-base"
soft[28]="gstreamer-0_10-plugins-good-extra"
soft[29]="gstreamer-0_10-plugins-ugly"
soft[30]="gstreamer-0_10-plugins-bad"
soft[31]="ImageMagick-extra"
soft[32]="fetchmsttfonts"
soft[33]="akonadi-googledata"
soft[34]="libnotify-tools"
soft[35]="autossh"
soft[36]="lynis"
soft[37]="fail2ban"
soft[38]="sshguard"
soft[39]=""
soft[40]=""
##################################
cnt=${#soft@]}
for (( i = 0 ; i < cnt ; i++ ))
	do
	j=0
	for PART in ${soft$i]}
		do
		if  $j == 0 ] ; then
			s=$PART
			j=1
		else
			s=$PART
			j=0
		fi
	done
	zypper install -y -l "$s" 
	
done

sleep 30
clear
printf "

################################################################################
"
printf "\e[1;33m\e INSTALL CODECS is finished... 
"
printf "####################################################################################

"
sleep 3
clear

###################################################################
#set cron
cd /etc/sysconfig/

cat 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
sleep 3
mv cr cron
cd
######################################################
#Install mozilla addons
mkdir /home/$USER/INSTAL
cd /home/$USER/INSTAL
ext="ext.tar.gz"
ad="http://dl.dropbox.com/u/8046742/$ext"

wget -q $ad
tar -xzf $axt
mv -f extensions /home/$USER/.mozilla/firefox/PROFIL.default/extensions/ #!

#############################################################
#Install audiometadata service menu
aud="audiokonverter-5.9.1-4.1.noarch.rpm"
adr="http://dl.dropbox.com/u/8046742/$aud"

wget -q $aud
zypper install -y $aud

###############################################################
#Install Dropbox service menu
drb="DropboxServiceMenu-0.15.2.tar.gz"
adre="http://dl.dropbox.com/u/8046742/$drb"

wget -q $adre
tar -xzf $drb
cd ~/DropboxServiceMenu-0.15.2
exec ./install-it.sh
cd ..

##############################################################
#Install Run-in-konsole service menu
kon="konsole.desktop.tar.gz"
adres="http://dl.dropbox.com/u/8046742/$kon"

wget -q $adres
tar -xzf $kon
cp run-script-in-konsole.desktop /home/$USER/.kde4/share/kde4/services/


##############################################################

# This script installs Stardict with eng-bul/bui-eng dicts?

n="stardict.tar.bz"
#b="5748864"
uri="http://dl.dropbox.com/u/8046742/$n"

if  [ -e ~/$n ];
then
    tar -xzf $n

else
    echo -en "Downloading and Installing dicts..."
    wget $uri
    tar -xzf $n # Extract the dicts
    
fi

mv stardict-bgoffice_bul-eng-2.4.2 /usr/share/stardict/dic
mv stardict-bgoffice_eng-bul-2.4.2 /usr/share/stardict/dic
mv stardict.tar.bz /tmp
rm /tmp/stardict.tar.bz 



######################################################################
# Инсталиране на ТВхелпер?
ni="tvhelper-1.1.5.tar.gz"
nii="tvhelper.tar.bz2"
uri="http://openfmi.net/frs/download.php/486/$ni"
urii="http://dl.dropbox.com/u/8046742/$nii"
DIR=" /home/$USER/Program/tvhelper-1.1.5"


tv ()	{

    echo -en "Downloading ..."
    wget $uri
    tar -xzf $ni -C ~/Program
    wget $urii
    tar -xjf $nii
    mv -f TVHelper.c $DIR/src
    rm $ni $nii
}

inst ()  {

    cd  $DIR
    ./configure --with-player=vlc --with-playerpath=/usr/bin/vlc 
    make 
    sudo make install
}

if   -e  ~/Program/tvhelper-1.1.5 ];then inst
elif   -e ~/Program ];then tv
else
    mkdir ~/Program; tv ;inst
fi per-1.1.5.tar.gz
sleep 5 

########################################################################
#Инсталиране на Skype за 32 бит!
pks="/home/$USER/Programs/skype-2.1.0.81-suse.i586.rpm"
si="uname -i"

if  $si = "x86_64" ];then sudo zypper in -y skype 

else  !-e "$pks" ]
    then
         wget -q http://www.skype.com/go/getskype-linux-beta-suse
         sudo zypper in -y ~/skype-2.1.0.81-suse.i586.rpm
	 rm ~/skype-2.1.0.81-suse.i586.rpm
         
fi
#######################################################################

#Инсталиране на прог. проверяваща gmail
wget http://gmail-plasmoid.googlecode.com/files/gmail-plasmoid-0.7.15.plasmoid
plasmapkg -i gmail-plasmoid-0.7.15.plasmoid
exit