apt-get behaves differently when using loop structure - bash

I have a bash script which installs a set of packages from a file but it fails to find the packages. When executing the same commands from the bash terminal, there is no problem.
sources.list:
apt-transport-https
curl
BROKEN:
apt-get update
dl_flag=--download-only
pkglist=source.list
while read pkg; do
apt-get install -y --allow-downgrades "$dl_flag" "$pkg"
done <"$pkglist"
apt-get claims it can't find anything:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package apt-transport-https
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package curl
When doing the same commands from a terminal, everything is just fine...
# apt-get install -y --allow-downgrades --download-only curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
ca-certificates krb5-locales libasn1-8-heimdal libcurl3-gnutls libffi6 libgmp10 libgnutls30 libgssapi-krb5-2
libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhogweed4 libhx509-5-heimdal libidn11
libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libnettle6 libp11-kit0
libroken18-heimdal librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libsqlite3-0 libssl1.0.0 libtasn1-6
libwind0-heimdal openssl
Suggested packages:
gnutls-bin krb5-doc krb5-user libsasl2-modules-otp libsasl2-modules-ldap libsasl2-modules-sql libsasl2-modules-gssapi-mit
| libsasl2-modules-gssapi-heimdal
The following NEW packages will be installed:
ca-certificates curl krb5-locales libasn1-8-heimdal libcurl3-gnutls libffi6 libgmp10 libgnutls30 libgssapi-krb5-2
libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhogweed4 libhx509-5-heimdal libidn11
libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libnettle6 libp11-kit0
libroken18-heimdal librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libsqlite3-0 libssl1.0.0 libtasn1-6
libwind0-heimdal openssl
0 upgraded, 34 newly installed, 0 to remove and 13 not upgraded.
Need to get 5324 kB of archives.
After this operation, 19.0 MB of additional disk space will be used.
I can't think of anything that would cause the loop to behave differently.

Source.list contained carriage returns which were passed to the loop steps that broke the script.
I also found a note that the sources file should end in a line-feed to ensure proper parsing by the read function.

Related

unable to run apt-get -f install on debian

I have a system that is due to be upgraded but I'm having conflicts with apt-get -f install:
apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following extra packages will be installed:
login sysvinit-utils util-linux
Suggested packages:
kbd util-linux-locales
The following packages will be upgraded:
login sysvinit-utils util-linux
3 upgraded, 0 newly installed, 0 to remove and 106 not upgraded.
13 not fully installed or removed.
Need to get 0 B/1775 kB of archives.
After this operation, 1886 kB of additional disk space will be used.
Do you want to continue? [Y/n]
E: Sub-process false returned an error code (1)
E: Prior errors apply to /var/cache/apt/archives/sysvinit-utils_2.96-7_arm64.deb
E: Prior errors apply to /var/cache/apt/archives/util-linux_2.36.1-8_arm64.deb
E: Prior errors apply to /var/cache/apt/archives/login_1%3a4.8.1-1_arm64.deb
debconf: apt-extracttemplates failed: No such file or directory
(Reading database ... 9765 files and directories currently installed.)
Preparing to unpack .../sysvinit-utils_2.96-7_arm64.deb ...
Unpacking sysvinit-utils (2.96-7) over (2.88dsf-59) ...
dpkg: error processing archive /var/cache/apt/archives/sysvinit-utils_2.96-7_arm64.deb (--unpack):
trying to overwrite '/lib/init/vars.sh', which is also in package initscripts 2.88dsf-59
Errors were encountered while processing:
/var/cache/apt/archives/sysvinit-utils_2.96-7_arm64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
I don't understand why it says E: Sub-process false returned an error code (1) after I confirm installation of the packages. Can anyone help me with this?
initscripts is a purely virtual package, so it is safe to give dpkg permission to overwrite it. Open the terminal and type:
sudo dpkg -i --force-overwrite /var/cache/apt/archives/sysvinit-utils_2.96-7_arm64.deb
If you're not using dpkg directly and using an apt front-end like apt or apt-get, the following command defines the same custom dpkg option to use as the previous command.
sudo apt-get -o Dpkg::Options::="--force-overwrite" install sysvinit-utils
Try to flush the cache and reinstall then :
apt autoremove
apt clean
apt autoclean
apt update
apt install -f
if it doesn't help, try to remove the problematic packages and reinstall them normally.

Why does apt-get consume stdin when it installs something but not otherwise?

I am connected to a remote Debian system via macOS Terminal.
Command after apt-get never runs if apt-get installs something
At first, I copy these three commands from a text file on my macOS and paste it into the terminal with a single command+v press:
sudo apt-get -y remove tree
sudo apt-get -y install tree
echo hi
Here is what I see in the Terminal.
lone#lone:~$ sudo apt-get -y remove tree
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'tree' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.
lone#lone:~$ sudo apt-get -y install tree
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
tree
0 upgraded, 1 newly installed, 0 to remove and 17 not upgraded.
Need to get 0 B/46.1 kB of archives.
After this operation, 106 kB of additional disk space will be used.
Selecting previously unselected package tree.
(Reading database ... 31853 files and directories currently installed.)
Preparing to unpack .../tree_1.7.0-5_amd64.deb ...
Unpacking tree (1.7.0-5) ...
Setting up tree (1.7.0-5) ...
Processing triggers for man-db (2.7.6.1-2) ...
lone#lone:~$
The third command, echo hi, was never executed. Why?
Command after apt-get does run if apt-get does not install anything
Next time, I simply paste these two commands with a single command+v press:
sudo apt-get -y install tree
echo hi
This time, since tree is already installed, apt-get does not need to install it again. This is the output I see:
lone#lone:~$ sudo apt-get -y install tree
Reading package lists... Done
Building dependency tree
Reading state information... Done
tree is already the newest version (1.7.0-5).
0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.
lone#lone:~$ echo hi
hi
This time echo hi was executed. Why?
Both results are reproducible every time I perform these two sets of operations. Why does the echo hi command not run in the first example but does in the second example?
apt-get or a program called by apt-get is emptying its stdin (which happens to be the same as the shell's, where your list of commands originates).
Since you know nothing needs to be to read from the user, redirect stdin from /dev/null:
sudo apt-get -y remove tree </dev/null
sudo apt-get -y install tree </dev/null
echo hi
Append -o=DPkg::FlushSTDIN=0 to your apt-get install command.
Ref: Source Code

Getting error on ubuntu 10 ec2 server while apt install

sudo apt install tasksel
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: The package libopencv-video2.4v5 needs to be reinstalled, but I can't find an archive for it.
I'd recommend : sudo dpkg --remove --force-all libopencv-video* &&
sudo apt-get update
sometimes it needs the machine to be restarted
Also you could try :
apt-get -f install
apt-get autoremove
apt-get purge libopencv-video*
apt-get purge
apt-get clean
if apt didn't work you can also manually download .deb file and install it using dpkg -i file.deb
Note: pick the file based on your ubuntu version and arch
https://packages.ubuntu.com/trusty/i386/libopencv-video2.4

Depends: openjdk-7-jre-headless (= 7u75-2.5.4-1~deb7u1) but it is not going to be installed

i want to install Openjdk but i get this error :
root#debian~# apt-get install openjdk-7-jdk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
openjdk-7-jdk : Depends: openjdk-7-jre (= 7u75-2.5.4-1~deb7u1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
i try this commands by my issue still not solved :
sudo apt-get install -f
sudo dpkg --configure -a
sudo apt-get clean
sudo apt-get update
how should i fix this ?
try
sudo apt-get install tzdata=2014j-0wheezy1
sudo apt-get install openjdk-7-jdk

Unable to Install Heroku toolbelt on Samsung ARM Chromebook?

I have a Samsung Series 3 Chromebook with the ARM processor. I made a Crouton chroot and installed Ubuntu cli. I have Node.js 0.10.15 installed and running. I tried to install the heroku-toolbelt and it won't run. Below is the command:
(precise)root#localhost:/# wget --no-check-certificate -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh
I used --no-check-certificate because when I took quiet mode off I learned that was causing the install to punt. After it told me a few times it could not verify certificates, it did said:
gpg: no valid OpenPGP data found.
Get:1 http://toolbelt.heroku.com ./ Release.gpg [490 B]
Hit http://ports.ubuntu.com precise Release.gpg
Hit http://ports.ubuntu.com precise-updates Release.gpg
Hit http://ports.ubuntu.com precise-security Release.gpg
Hit http://ports.ubuntu.com precise Release
Hit http://ports.ubuntu.com precise-updates Release
Get:2 http://toolbelt.heroku.com ./ Release [1,673 B]
Ign http://toolbelt.heroku.com ./ Release
and a whole lot more Hit http://......
After which it said:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
ca-certificates foreman git git-core git-man heroku libasn1-8-heimdal libcurl3-gnutls liberror-perl libgcrypt11 libgnutls26 libgpg-error0
libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal libkrb5-26-heimdal libldap-2.4-2 libp11-kit0
libroken18-heimdal librtmp0 libruby1.9.1 libsasl2-2 libsasl2-modules libtasn1-3 libwind0-heimdal libyaml-0-2 openssl rsync ruby1.9.1
Suggested packages:
git-daemon-run git-daemon-sysvinit git-doc git-el git-arch git-cvs git-svn git-email git-gui gitk gitweb rng-tools gnutls-bin libsasl2-modules-otp
libsasl2-modules-ldap libsasl2-modules-sql libsasl2-modules-gssapi-mit libsasl2-modules-gssapi-heimdal openssh-server ruby1.9.1-examples ri1.9.1
graphviz ruby1.9.1-dev
The following NEW packages will be installed:
ca-certificates foreman git git-core git-man heroku heroku-toolbelt libasn1-8-heimdal libcurl3-gnutls liberror-perl libgcrypt11 libgnutls26
libgpg-error0 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal libkrb5-26-heimdal libldap-2.4-2
libp11-kit0 libroken18-heimdal librtmp0 libruby1.9.1 libsasl2-2 libsasl2-modules libtasn1-3 libwind0-heimdal libyaml-0-2 openssl rsync ruby1.9.1
0 upgraded, 32 newly installed, 0 to remove and 0 not upgraded.
Need to get 13.2 MB of archives.
After this operation, 28.1 MB of additional disk space will be used.
WARNING: The following packages cannot be authenticated!
foreman heroku heroku-toolbelt
E: There are problems and -y was used without --force-yes
(precise)root#localhost:/# heroku
bash: heroku: command not found
What happened?
In your apt-get output, the last line states:
E: There are problems and -y was used without --force-yes
If you look at the actual script Heroku uses to install itself on linux, that corresponds to apt-get install -y heroku-toolbelt line.
Try running this: apt-get install -y --force-yes heroku-toolbelt

Resources