Ubuntu: Bash script help [closed]


The --print-uris in apt will print the entire output along with the urls to a specified txt file. I need a command which will only select the packges to be installed from the output of print uris and redirect them to a file. As you might have guessed i m trying to create a script. Thanks in advance


Mowiewowie, another proof of apt supercow ability!! Maybe this file, called supercow, helps:

#!/bin/bash    # exactly one argument  [[ $# -ne 1 ]] && echo "Usage: $( basename $0 ) PACKAGE" && exit 1    PACKAGE_DIRECTORY=$( mktemp -d )    # Target directory  PACKAGE_LOGFILE=$( mktemp )         # Target file  cd $PACKAGE_DIRECTORY || exit 1     # Exit if directory does not exist  apt_out_all=$(( apt-get --print-uris --yes install $1 ) 2>&1 )    apt_out_check=$( echo $apt_out_all | sed s/^.*information...\ // )    # error checks  if ! [[ $( echo $apt_out_check | grep "packages will be installed" ) ]]  then      echo -n "ERROR: "        if   [[ "${apt_out_check%% *}" == "E:" ]]          then echo "Unable to locate package $1."        elif [[ "${apt_out_check%% *}" == "$1" ]]          then echo "$1 already installed." # look at /var/cache/apt/archives        # other, eg: Package solid is not available, but is referred to ...      else echo "${apt_out_check}"      fi      exit 1  fi    # loop through all lines: tee to file if package, wget uri if not empty  for i in $apt_out_all ; do      uri=$(  echo $i | tee >(grep ".deb$" >> $PACKAGE_LOGFILE) \                      | grep "^'http" | sed -e s/^\'// -e s/\'$// )      [[ $uri ]] && if ! wget $uri ; then                  echo "ERROR: wget failed" ; exit 1 ; fi  done    echo "Here is the content of the logfile:" ; cat $PACKAGE_LOGFILE  echo -e "\nHere is the content of the directory:" ; ls $PACKAGE_DIRECTORY  

My first attempt was a big fail because there was neither a check for mad user input nor if the package is already installed. Moreover, it appeared that if eg. gcc is already installed drops an error string and for instance geany just an empty line. So i would still use it with healthy suspicion although this attempt feels way more solid. I also erased double-checks, etc.

Example: $ bash supercow kobodeluxe

#... wget messages    Here is the content of the logfile:  kobodeluxe-data_0.5.1-6_all.deb  kobodeluxe_0.5.1-6_amd64.deb    Here is the content of the directory:  kobodeluxe_0.5.1-6_amd64.deb  kobodeluxe-data_0.5.1-6_all.deb  

Example: $ bash supercow bash

bash already installed.  

Example: `$ bash supercow windows3.11

Unable to locate package windows3.11.  

Example: $bash supercow solid

ERROR: Package solid is not available, but is referred to by another package.  #...  

Hopefully this helps!

EDIT TO QUESTION: Then replace grep ".deb$" >> $PACKAGE_LOGFILE inside brackets with grep ".deb$" | sed s/_.*// >> $PACKAGE_LOGFILE because i don't know an official package that contains an underscore unless I am mistaken.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »