Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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???
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.
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
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.
... 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
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.
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.
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
....
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.