Thursday, January 5, 2012

BASH Script Download Programs and ALL dependencies for offline installation

This is useful for linux users who may not always have an internet connection for apt-get and wish to create a database of offline installable deb files.
Note to basic users:

You can install *.deb files with the following command (ignore the $, just means its on the command line)
 $ sudo dpkg -i *.deb  

I have written and tested this script in Ubuntu 11.04 Desktop. FYI It is a work in progress. What I am trying to say is that it is not idiot-proof.

HOW DOES IT WORK?

You make a plain text file, typing each program you wish to install. Put spaces in between programs but no carriage return. Example: python2.6 alarm easyshutdown gimp
Another example:
Command line example of program list (although you could use a text editor):
1:  $ echo "vlc alarm-clock apache2 conky curl ffmpeg" > programs.txt  

Copy this script to a text editor, save as installsource-generator.sh
Before you can run a new script, you have to make it executable with the command:

1:  $ sudo chmod a+x installsource-generator.sh  
Before running the script, open the script and edit the variable where you would like to set up an installsource directory. (you can find it in between all of the pound signs towards the beginning)

Ensure stable internet connection (the command wget -c will be used to download files, which will attempt to reconnect if link is lost)
Make sure you download grabpackages.py and move it to the /usr/local/bin folder with the command
1:  $ sudo mv /folder/of/grabpackages.py /usr/local/bin  
2:  $ sudo chmod a+x grabpackages.py /usr/local/bin/grabpackages.py #(to make it executable)  


You are now ready to download programs with ALL dependencies
To run, simple invoke the following command (you may need to type 'sudo -s' without quotes first)
1:  $ sudo sh installsource-generator.sh nameoftextfile.txt  


HERE IS THE CODE:

 #!/bin/sh  
 #this program generates an installsource of packages + all dependencies from an array of files. use the spaces  
 #installsource-generator  
 #version 2.0  
 #requires grabpackages.py in /usr/local/bin (must make it executable ie sudo chmod a+x grabpackages.py)  
 #SUMMARY OF ACTIONS  
 #get list of package names (programs)  
 #mkdir in installsource for each package  
 #run grabpackages.py on each package and output to the corresponding list of dependencies (sh file) to folder (auto appropriately name for each program)  
 #chmod a+x on each sh file from grabpackages.py  
 #ask user whether to install the deb files  
 ##################################################  
 ##################################################  
 ##################################################  
 ##################################################  
 ############VARIABLES TO BE SET BY USER###########  
 ##################################################  
 #i set installLoc  
 installLoc="$HOME/installsource-ubuntu/generated"  
 ##################################################  
 ##################################################  
 #create installsource directory if its not there already  
 mkdir -p "$installLoc"  
 #array=( one two three )#i am an example of an array  
 #testfor$1input  
 if test ! -s "$1"  
 then  
  echo 'syntax> installsource-generator listofprograms.txt'  
 fi  
 #i assign array to variable  
 #declare -a arrayFile  
 #array=`cat "$1"`  
 #declare -a arrayFile=($array)  
 ##################################################  
 ##################################################  
 for i in `cat "$1"`  
 do  
   # do whatever on $i  
  echo "creating folder $i"  
  mkdir "$installLoc/$i"  
  echo "downloading packages with grabpackages.py and saving to folder "$i""  
  grabpackages.py -o "$installLoc/$i/$i.sh" "$i"  
  #make files executable  
  sudo chmod a+x "$installLoc/$i/$i.sh"  
  fullPath="$installLoc/$i/"  
  sed -i "s|wget -c|wget -c -P $fullPath|g" "$installLoc/$i/$i.sh"  
  sh "$installLoc/$i/$i.sh"  
 done  
 ##################################################  
 echo "COMPLETED ALL DOWNLOADS AND PREPARING TO INSTALL"  
 echo "WOULD YOU LIKE TO INSTALL ALL DOWNLOADED PACKAGES?"  
 read installAnswer  
 if [ $installAnswer = "y" ]; then  
 echo 'STARTING INSTALL'  
 for i in `cat "$1"`  
 do  
   # do whatever on $i  
  echo "installing package $1.sh"  
  sudo dpkg -i "$installLoc/$i/*.sh"  
 done  
  sudo dpkg -i $mediaDrive/$installsource/*.deb  
  echo 'install complete, script complete'  
 else  
  echo 'no install, script complete'  
 fi  

No comments:

Post a Comment