LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2019, 05:18 PM   #1
fthrtm
LQ Newbie
 
Registered: Mar 2019
Posts: 14

Rep: Reputation: Disabled
Unhappy creating a script to verify/install packages


I need to create a script with this logic:

## Packages
pack1
pack2
pack3

check if a $pack is already installed or not
if YES; then
echo "pack is installed"
else
install pack
if ERROR during install
pause and report error
loop and try this pack again until it is done
else
loop and go to next pack
fi
fi
done

Any ideas on how to properly do this ?¿


Tks you all
 
Old 03-22-2019, 06:29 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by fthrtm View Post
I need to create a script with this logic:
........
Any ideas on how to properly do this ?¿


Tks you all
Well I got a little time on my hands so.....
Code:
#!/bin/bash

#set -xv

#my test files are named
# package1 package3 package7 
#and are in this directory
packages_location=/home/userx/testpackages
Packages_2_install=( package1 package2 package3 package4 package5 package6 package7  )

length=${#Packages_2_install[@]}


is_installed()
{
      for ((  i = 0; i < ${#Packages_2_install[@]}; i++))
    {      #just check package name, by removing the path
            if [[  "${package##*/}" = "${Packages_2_install["$i"]}" ]] ; then
            {
                echo "is installed: $package"
             # remove installed package from array list
                 unset 'Packages_2_install[$i]'
            }
            fi
        }
}

while read package
do
{
is_installed
} done <<<"$(find "$packages_location" -type f )" # -name "*.deb")"

#here you install your packages, being what is left over in 
# your array of named packages you
#want to install if not installed.

#should be remaining names in array, (minus installed packages)
#package1 package3 package7

for (( i = 0 ; i < "$length" ; i++))
 {
        if [[  "${Packages_2_install[$i]}" != "" ]] ; then
        {
         echo    "${Packages_2_install[$i]}"
         #install packges here
        touch $packages_location/"${Packages_2_install[$i]}"-$((g++))



        while [[ "$?" -ne '0' ]] ;
        do
        {
            ## preform error install

            [[ "$count" -eq '3' ]] && break
            ((count++))
        }
        fi
        count=0
 }

..
something like that might work, but to have to punch in the relevant data,, and fix what needs fixing, if fixing is needed, and add your install commands, and error checking.

testing it at each stage of development, or when you feel
necessary to insure its integrity.

look up bash error handling and loops , break, continue, conditionals.

post back your progress, and if you need help, which I am sure you will.

you are going to have to figure out how to set up a test bed so you will not be installing programs on the willy nilly, and maybe screwing up your system while you're trying to get this to suit your actual needs. VBox is one idea,

so like you do not have a package maintainer on your Linux???

Last edited by BW-userx; 03-22-2019 at 09:03 PM.
 
Old 03-23-2019, 06:12 AM   #3
fthrtm
LQ Newbie
 
Registered: Mar 2019
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
you are going to have to figure out how to set up a test bed so you will not be installing programs on the willy nilly, and maybe screwing up your system while you're trying to get this to suit your actual needs. VBox is one idea,

so like you do not have a package maintainer on your Linux???
Thank you for your help.

I'll try your code right now.

I think I need to show you more about my case so, here it is:

1 - I'm using a physical "test" machine, not VM
2 - I'm on Debian 9.6
3 - I'm using "apt-get install" to install these packages
4 - Here is my last try:
Code:
#!/bin/sh

cat > pacs.txt <<_EOF_ ## create a list of desired packages to install.
pac1
pac2
pac3
pac4
pac5
_EOF_
HOSTFILE=pacs.txt
for host in $(cat $HOSTFILE) ## start check/install
do
	dpkg -s $host ## check if package is installed
	st=$?
	if [ $st -eq "0" ]; then ## if not installed... 
		apt-get install $host -y ## try to install it.
		st=$?
		if [ $st != "0" ]; then ## if error during installation...
			printf "%s\n" "Error installing $host" ## report installation error
			read pause ## wait for user interaction
		else
			printf "%s\n" "$host installed" ## if no errors installing, report.
		fi
	else
		printf "%s\n" "$host already installed" ## if already installed, report and go to next.
	fi
done
rm -rf /pacs.txt > /dev/null ## remove residual temporary created pacs list 
clear
first, i try to check if pacs is installed with dpkg -l | grep "$host" but it didn't work properly because some pacs have part of the name of other pacs, for instance: zip appears on gzip, p7zip, unzip and few others, and this will show a false positive.

in my second try i use "dpkg-query -W -f='${Status} ${Version}\n' $host" and it works like a charm when the package is installed but i can't figure out how to get a negative from this...

Right now i'll try your suggestion and reply with results as soon as i can.

Thank you once again

PS: Sorry about my poor English, I'm self-taught on it.

Last edited by fthrtm; 03-23-2019 at 06:23 AM.
 
Old 03-23-2019, 07:03 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
you can safely install all of them, the already installed ones will be automatically skipped (on debian).
 
Old 03-23-2019, 07:24 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
well there ya have it folks. Problem solved.
 
Old 03-23-2019, 07:42 AM   #6
fthrtm
LQ Newbie
 
Registered: Mar 2019
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by pan64 View Post
you can safely install all of them, the already installed ones will be automatically skipped (on debian).
I know that but, this script I'm working on has a lot more things being done and I need to check this just in case I need to run the script more then once.

Just for you to figure out what I'm talking about, here in Brazil, the internet and is crap, so let's say that I was running the script and all of a sudden the internet goes down! then, I'll need to sop all the work and wait until internet is up and run the script once again .

Then, my script will check all steps that is already done before and skip those.

The script has a few steps before this package installations a few other steps after it.

What i do to check these steps is:

1 - create a file with all steps named like this:
## File STEPS.txt
step1=0
step2=0
step3=0 ## here is the step for package install
step4=0
step5=0

2 - When script runs, it will check in this file if a step is set as 0 or 1
3 - If it's 0 it's not done yet; then do execute this step and set it as 1.
4 - If it's 1 then it's already done before; then go to next step

that's it...

So, if I need to start the script again, that is how it will skip all steps that was already done
 
Old 03-23-2019, 08:05 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
if you are going to use files, which would be the best way to keep the where you left off part. to a certain extent. the create a list of installed packages first.
Code:
sudo apt list --installed
using awk perhaps and get the needed information then redirect that into a file.

start a loop, then use that to cross reference all installed packages against packages needing to be installed, this creates another list, or two, one for already installed, and another of whats left to check, you'd (have) could to put in some kind of check for internet connection, if internet fail then create your where you left off list. or put it into another loop (wait state) checking for connection back up, when it is, then proceed where you left off.

maybe a time out, if x minutes down, dump the where you left off into another list, or over write the working list to update it to where you left off. keeping a master list if needed elsewhere.

Last edited by BW-userx; 03-23-2019 at 08:07 AM.
 
Old 03-23-2019, 09:23 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
or just: dpkg -l <package>
apt list does not require sudo
 
Old 03-23-2019, 09:25 AM   #9
fthrtm
LQ Newbie
 
Registered: Mar 2019
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
... the create a list of installed packages first.
Code:
sudo apt list --installed
when i try this from inside the script i get this msg: "WARNING: apt does not have a stable CLI interface. Use with caution in scripts."

Quote:
using awk perhaps and get the needed information then redirect that into a file.
I'm a total newbie on linux and have no idea on how to use awk to do this...

Quote:
start a loop, then use that to cross reference all installed packages against packages needing to be installed, this creates another list, or two, one for already installed, and another of whats left to check...
Once again no idea, but even though, i'll try to figure out how to do. Thank you again
 
Old 03-23-2019, 09:28 AM   #10
fthrtm
LQ Newbie
 
Registered: Mar 2019
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
or just: dpkg -l <package>...
This was my first try, as i mention before:
Quote:
first, i try to check if pacs is installed with dpkg -l | grep "$host" but it didn't work properly because some pacs have part of the name of other pacs, for instance: zip appears on gzip, p7zip, unzip and few others, and this will show a false positive.
 
Old 03-23-2019, 09:38 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Quote:
Originally Posted by fthrtm View Post
This was my first try, as i mention before:
that means you need to tweak your grep or use something else instead of grep. And you can find a lot of examples on the net:
https://askubuntu.com/questions/4233...d-on-my-server
 
Old 03-23-2019, 09:46 AM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by fthrtm
I'm a total newbie on linux and have no idea on how to use awk to do this...
time to learn....

when I was using debian knock offs, I'd see that too, then look at my results, and seeing them, shrug it off, as it is just a warning. I still got what I wanted, or use whatever works, all you need is a proper list of all packages installed on system. pan64 shows a method you should check. post #10


Code:
#!/bin/bash


#first remove all non-needed to be 
#installed packages from list
#so it does not matter if connection
#is up or not
#then process the remaining while
#checking to see if connection
#is active or not
#then handle that accordingly
#
#
#with debian you'll have to replace your
#commands and check formatting
#
#create package installed list
#chops off version number using the first - (hyphen after reversing it)
#then puts it back in order before going to file
sudo xbps-query -l | awk '{ print $2 }' | rev |  sed 's/^[^-]*-//g' | rev > installed_list

#create a backup master list(s) if you want them

cp -v installed_list installed_list-master
cp -v check-installed_list check-installed_list-master

#function to find all installed packages
#and removes the ones that are installed from the
#check list.

is_installed()
{
      for installed in $(cat ~/check-installed_list)
      do
      {     
            if [[  "$package" = "$installed" ]] ; then
            {
                echo "is installed: $package"
                 #remove  installed package from check list
                sed -i "/"$package"/d" check-installed_list

            }
            fi
     } done
 }

while read package
do
{
is_installed
}
done < installed_list

#How to tell if wifi is up, and using the new
#modified check-installed_list
# now process what is not installed here.

#wifi connection up
if  [[  $(cat /sys/class/net/wlo1/carrier) -eq '1' ]] ; then
echo "wifi up"
fi
#hard wire
if  [[  $(cat /sys/class/net/lo/carrier) -eq '1' ]] ; then
echo "net is up"
fi
#check your /sys/class/net subdir to get your
#data into you need to use for your system
that last part is still a work in progress. You need to get your list straight first, before moving on.

Last edited by BW-userx; 03-23-2019 at 10:06 AM.
 
Old 03-23-2019, 10:25 AM   #13
fthrtm
LQ Newbie
 
Registered: Mar 2019
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by BW-userx View Post
time to learn....

when I was using debian knock offs, I'd see that too, then look at my results, and seeing them, shrug it off, as it is just a warning. I still got what I wanted, or use whatever works, all you need is a proper list of all packages installed on system. pan64 shows a method you should check. post #10


Code:
#!/bin/bash

...
that last part is still a work in progress. You need to get your list straight first, before moving on.
Thank you again. I'll give a try on your code tomorrow and reply you if it works
 
Old 03-23-2019, 10:30 AM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by fthrtm View Post
This was my first try, as i mention before:

....
first, i try to check if pacs is installed with dpkg -l | grep "$host" but it didn't work properly because some pacs have part of the name of other pacs, for instance: zip appears on gzip, p7zip, unzip and few others, and this will show a false positive.

in my second try i use "dpkg-query -W -f='${Status} ${Version}\n' $host" and it works like a charm when the package is installed but i can't figure out how to get a negative from this...

Right now i'll try your suggestion and reply with results as soon as i can.

Thank you once again

PS: Sorry about my poor English, I'm self-taught on it.
as Pan64 pointed out, and I do not see why you're using a Host name to get a list. you seem to already know it is a debain OS distro. You just need to know what the package names are used for this distro, or as pan64 already pointed out, if this package is installed already then debian skips it, so there is your margin of error safety net.

each distro more then less uses its own naming scheme for packages, you need to familiarize yourself with the distro you are curtaining this script for. ie Debian.

then create your are these packages installed list using them package names, or using sub-string matching. I do not know what the margin of error will be on this, nor can I check it against debian, because I do not use Debian, nor have a knock off installed to even get close to it to find out.

gzip, p7zip, unzip are all different zip utilities, I do not know the particulars of them, but most zip utilities you only need one to work with zip files, where p7zip seems to be the best choice. if you want to just check for some kind of zip package is installed then you can use sub-string checking, and more code to cover it.

after running the is_installed function to weed out all matches of installed packages, then taking the left over packages you can then look closer at them and picking out things like is a zip utility installed, if yes which one, and do you want that one or a different one, then either remove what is there and install the one you want, or install it along side of it. meaning have both installed within the system.

example:
Code:
pack1=zip
installed_pack=p7zip

if [[ "$installed_pack" =~ "$pack1" ]] ; then
{
    echo "you got $installed_pack on system
$pack1 is what it is matching"
}
fi

Last edited by BW-userx; 03-23-2019 at 10:38 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to verify installed slackware packages? Gary Baker Slackware 13 12-28-2014 11:55 PM
Can you verify my syntax for creating multiple users in a batch file? grayceworks Linux - Newbie 5 03-03-2011 02:04 AM
openssl ssl error code 14090086 verify the CA cert is ok / certificate verify failed acummings Slackware 14 02-27-2009 01:51 AM
Verify Packages gn00kie Linux - General 5 02-11-2006 11:15 PM
creating packages (.tgz/.deb/.rpm) How from the source packages? l_9_l Linux - General 1 03-06-2002 06:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration