Get list of packages to be installed from script? - linux

On Ubuntu 12.04 it is possible to do
k=0
if apt-get --assume-no upgrade | grep -q linux-; then
k=1
fi
aptitude -y safe-upgrade
if [ $k == 1 ]; then
/sbin/reboot
fi
but on older versions, apt-get doesn't have --assume-no.
Question
Is there another way to get the list of packages that are going to be installed?

Have you tried --dry-run (or -s)? According to the man page:
No action; perform a simulation of events that would occur but do not actually change the system.
On another level: if your goal is to determine whether you need a reboot, consider using the script /usr/lib/update-notifier/update-motd-reboot-required or do what this script does: check whether the file /var/run/reboot-required exists.

Related

Bash Script - How to check if a package is installed and perform action based on terminal results

I'm currently working on a project where I need to update several systems that aren't connected to the internet. Each rpm package I have to manually download and install/upgrade the packages on each system.
I'm trying to make a script that will check if an older version of a package is installed and if it is to perform the upgrade. Otherwise, move on to the next package (I'm only upgrading package, not installing new packages.)
Here is what I have currently. The script refers to a file that has a list of all the packages. I don't have the package name for each package but I do have the rpms, so I'm curious how I can do a proper comparison in the if statement to make script perform the other action if true.
Working Solution:
IFS="="
while read -r name value
do
if [[ $(rpm -qi ${name//\"/}) == "package ${name//\"/} is not installed" ]]
then
echo "Package ${name//\"/} is not installed, moving on..."
else
if [[ "${value//\"/}" == *"kernel"* ]]
then
rpm -ivh ${value//\"/}
else
rpm -Uvh ${value//\"/}
fi
fi
echo 'Done!'
done < patches.conf >> patching_log.conf
Thank you in advance!
Input:
RHSA-2019:3979(kernel-tools-debuginfo)=kernel-tools-debuginfo-3.10.0-1062.7.1.el7.x86_64.rpm
RHSA-2019:3979(kernel-tools-libs)=kernel-tools-libs-3.10.0-1062.7.1.el7.x86_64.rpm
RHSA-2019:3979(perf)=perf-3.10.0-1062.7.1.el7.x86_64.rpm
RHSA-2019:3979(perf-debuginfo)=perf-debuginfo-3.10.0-1062.7.1.el7.x86_64.rpm
RHSA-2019:3979(python-perf)=python-perf-3.10.0-1062.7.1.el7.x86_64.rpm
RHSA-2019:3979(python-perf-debuginfo)=python-perf-debuginfo-3.10.0-1062.7.1.el7.x86_64.rpm
RHSA-2019:4024(SDL-32)=SDL-1.2.15-15.el7_7.i686.rpm
RHSA-2019:4024(SDL-64)=SDL-1.2.15-15.el7_7.x86_64.rpm
When you get a chance, please show us an example of your input.
I've looked at your program and I'm providing this rewrite:
IFS="="
while read -r patchName patchrpm
do
RPM=${patchrpm//\"/}
if
! rpm -qi $RPM
then
echo "Package $RPM is not installed, moving on to next package."
else
rpm -Uvh $RPM
fi
done < patches.conf >> patching_log.conf
Here's the same program again with line numbers in case you need to ask specific questions:
1 IFS="="
2 while read -r patchName patchrpm
3 do
4 RPM=${patchnum//\"/}
5 if
6 ! rpm -qi $RPM
7 then
8 echo "Package $RPM is not installed, moving on to next package."
9 else
10 rpm -Uvh $RPM
11 fi
12 done < patches.conf >> patching_log.conf
Instead of adding a lot of logic why do not read the manual of rpm and use command:
rpm -F package_name
This will update it if it is installed and continue further if not.
For kernel you need to do install, not update so part of the logic should be still there
actually you can simply do this using dpkg
dpkg-query -f='${Status:Want}\n\r' \-W Package
This will return;
0 if the requested query was successfully performed.
1 The requested query failed either fully or partially, due to no file or package being found
2 Fatal or unrecoverable error due to invalid command-line usage, or interactions with the system, such as accesses to the database, memory allocations, etc.

why bash changing my command 'mysql*' to 'mysql.sql'?

I am sure that there is no such file called mysql.sql in the directory
The operating system infor
cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
I have a shell script. One line is returning nong zero code
yum install -y mysql* 1>/dev/null 2>/dev/null
When I run this line in terminal, everything is fine and the return code( echo $?) is zero.
But in script, it will return 1 and whith the -x option, I can see bash changing the line into
yum install -y mysql.sql
So why mysql* is changed to mysql.sql?
There is a file called mysql.sql in the current directory of the script.
Quote the argument to avoid bash expanding it, so that yum can.
yum install -y "mysql*" 1>/dev/null 2>/dev/null
In general, you always want to quote arguments with * if you don't want them expanded as a glob. Bash by default passes them through literally if they don't match anything, but you can't usually guarantee that, so it's best to just quote it.

Running bash script in another directory

I know this question has been asked many times before but I have not been able to get my code working.
I am using the Raspberry Pi 3, with a CAN-BUS Shield. As this will be going into a production environment I need the Pi setup to be nice and easy. I have started to write a bash script so the production staff can run the script and the Pi will update and install everything it needs from the one script.
I have been following this web site https://harrisonsand.com/can-on-the-raspberry-pi/ and I have run into a problem when it comes to compiling can-utils.
I am able to clone the can-utils.git from here https://github.com/linux-can/can-utils.git
by using sudo git clone https://github.com/linux-can/can-utils.git
but my issues come when I need to run the ./autogen.sh & the ./configure as these are located in the dir can-utils.
If I run this from the Pi terminal as described on the web site, it works fine as I change dir cd can-utils and then just sudo ./autogen.sh but it isn't working when I run it in the bash script.
Below is the script I have so far, I know that most of it is commented out this is so that I can test each part as I write it and don't need to constantly download and install stuff I already have
#!/bin/bash
## Change Password
#printf "***********************************************************************\n"
#printf "Changing Password\n"
#echo "pi:***********" | sudo chpasswd # Password hidden
#sleep 1
#printf "Password Changed\n"
## Update & Upgrade Pi
#printf "***********************************************************************\n"
#printf "Update & Upgrade Pi\n\n"
#sudo apt-get update && sudo apt-get upgrade -y
#sleep 1
## Upgrade dist
#printf "***********************************************************************\n"
#printf "Upgrade Dist\n\n"
#sudo apt-get dist-upgrade -y
#sleep 1
## Install libtools
#printf "***********************************************************************\n"
#printf "Installing libtools\n\n"
#sudo apt-get install git autoconf libtool -y
#sleep 1
## Download required files
#printf "***********************************************************************\n"
#printf "Downloading required files\n\n"
## can-utils
#sudo git clone https://github.com/linux-can/can-utils.git
#sleep 1
## Auto configure can-utils
printf "***********************************************************************\n"
printf "Auto Configure can-utils\n\n"
# Things I have tried and do not work
#(cd /c && exec /can-utils/autogen.sh)
#sudo source /can-utils/autogen.sh
#sudo ./can-utils.autogen.sh
sleep 1
When I try the sudo ./can-utils.autogen.shin the Pi terminal the script starts to work so I think this is sort of the right command I need but then I get an error autoreconf: 'configure.ac or 'configure.in' is required these files are in the can-utils dir but for some reason it can't find them. Please can someone help me I have been searching for the answer for the last 2 days
Thank you for your help, rightly or wrongly I have ended up using cd /home/pi/can-utils I had thought I had tried that in the past but I think cd ./can-utils which didn't work.
sudo with script is for me, a nightmare. I just read in the man of sudo of my fedora 25:
Running shell scripts via sudo can expose the same kernel bugs that make setuid shell scripts unsafe on some operating systems (if your OS has a /dev/fd/ directory, setuid shell scripts are generally safe).
sudo command should protect the root account to avoid to run scripts written by user to gain root privilege.
If you keep the use of sudo, my advise should to add a cd command on top of your script:
cd /where_everithing_is
to be sure to be in the right place.
But, may be, sudo will fight again against you !

How do I install Haskell Stack locally?

I am working on my school server and I need to install Haskell's stack. In the README file and on the website I could not find how to install locally. What can I do if I am not a sudo user?
You don't need superuser privileges to install stack; you can as well install it in your own home directory. All you need for this to work is a Linux system with GMP installed (which GHC depends on at a very fundamental level). If GMP is not installed – the admins really shouldn't have any concerns installing that. (Alternatively, follow these instructions to install GMP without root permissions.)
#!/bin/bash
# Stack installation script, adapted from:
# https://github.com/yantonov/install-ghc/blob/af0b968b9e8423efb152ccec4224821e29317710/ubuntu/install-ghc-ubuntu.md
DOWNLOADS_DIR=$HOME/Downloads
STACK_INSTALL_DIR="$HOME/Development/bin"
mkdir -p ${STACK_INSTALL_DIR}
STACK_VERSION="2.1.3"
STACK_ARCHITECTURE="x86_64"
STACK_PLATFORM="linux"
# Check that libgmp is installed. This is the main critical system-level
# dependency of the Haskell environment that may not be present.
function check_lib()
{
echo "int main(){}" | gcc -o /dev/null -lgmp -x c -
return $?
}
GMP_OK=false
if (ldconfig -p | grep -q "libgmp.so.10"); then
GMP_VERSION_POSTFIX=""
if (check_lib -lgmp); then GMP_OK=true; fi
elif (ldconfig -p | grep -q "libgmpxx.so.4"); then
GMP_VERSION_POSTFIX="-gmp4"
if (check_lib -lgmp); then GMP_OK=true; fi
fi
if [ $GMP_OK = false ]; then
echo >&2 "Haskell requires the GNU multi-precision library (with headers)"
echo >&2 "in version 4 or 10, but neither can be found. Try"
echo >&2
echo >&2 "$ sudo apt-get install libgmp-dev"
echo >&2
echo >&2 "or https://unix.stackexchange.com/questions/265239/how-to-install-a-custom-gmp-lib-for-just-one-user"
echo >&2
exit 1
fi
STACK_DIST_FILENAME="stack-$STACK_VERSION-$STACK_PLATFORM-$STACK_ARCHITECTURE.tar.gz"
STACK_DIST_UNZIPPED_DIR="stack-$STACK_VERSION-$STACK_PLATFORM-$STACK_ARCHITECTURE"
STACK_DIST_URL="https://www.stackage.org/stack/$STACK_PLATFORM-$STACK_ARCHITECTURE"
STACK_TARGET_DIR="stack-$STACK_VERSION"
cd $DOWNLOADS_DIR
curl -L -o $STACK_DIST_FILENAME $STACK_DIST_URL
tar xvfz $STACK_DIST_FILENAME
# in case if error like this:
#curl: (77) error setting certificate verify locations: CAfile:
# /etc/pki/tls/certs/ca-bundle.crt CApath:
# ...
# create ~/.curlrc file
# and put this lines to it
# capath=/etc/ssl/certs/
# cacert=/etc/ssl/certs/ca-certificates.crt
# move to home development dir
rm -rf $STACK_INSTALL_DIR/$STACK_TARGET_DIR
mv $STACK_DIST_UNZIPPED_DIR $STACK_INSTALL_DIR/$STACK_TARGET_DIR
cd $STACK_INSTALL_DIR
# sym link
rm -rvi stack
ln -s `pwd`/$STACK_TARGET_DIR stack
# add to PATH environment
STACK_HOME=$HOME/Development/bin/stack
PATH=$STACK_HOME:$PATH
# clean up
cd $DOWNLOADS_DIR
rm -rf stack-$STACK_VERSION*
# install ghc
stack setup
The Haskell stack is successfully installed using the instructions in Documentation here.
As the case with "sudo user", the command sudo grants a user with super user privileges by flipping the mode bit. The details regarding the mechanism can be found here.
The problem in your case might be the reason that in "School Networks", users are restricted to use sudo for security purposes and hence, either the administrators must grant your account privileges or they must install the Haskell stack themselves. If this is a part of an assignment, adminstrators should have no problem doing so and you must inform administrators regarding this. Thereafter, you must be able to use it comfortably.
If the above steps are not possible, I would suggest you to try out Haskell stack in your personalized account on a device. You may even try out Cloud services like Cloud9, Nitrous and others. An unlikely reason might be that you are not using the Haskell stack properly.
Note: I have used the Haskell stack for some time, hence, I can conclude that it works.

What to do if dpkg nor apt-get will remove the program on Ubuntu?

The problem is that I installed a .deb file, and when I tried getting rid of it with dpkg -r ..., dpkg claimed to have removed it. Nevertheless, I can type in the "removed" command, and it still works.
I need to get it off, because I realized what I needed was a larger program that included it. When I try to run make on the larger program, it attempts to use the smaller with different options (the larger appears to be assuming a later version of the smaller).
Anyway, it's just weird that I can't get rid of it. I've re-installed and tried using the purge option, tried apt-get clean, tried restarting the machine, etc.
Any ideas would be appreciated. Thanks!
Try this:
rm /var/lib/dpkg/info/program.*
dpkg --remove --force-remove-reinstreq program
Replace 'program' with the one you want to remove.
Thanks H2CO3: "If everything else fails, perhaps delete the executable manually.... executable files [are] in the search paths of the shell which aren't executed if nonexistent"
rm `which flop`
flop is the name of the program.
WARNING!!!: Do this only if you know that the package does not do anything crazy with the filesystem!
Download but don't install the debian package. Then run
$ touch clean_up.sh
$ chmod +X clean_up.sh
$ gedit clean_up.sh
In the file add the following:
#!/bin/bash
all=$(dpkg -c steam*deb | awk '{print $6}')
for item in $all; do
#echo "Checking $item"
item=$(echo $item | sed 's/^\.//g')
if [[ -d ${item} ]]; then
#echo "-is a directory. Skipping"
continue
fi
echo "Removing file ${item}"
sudo rm -f ${item}
done
Afterwards, save and exit gedit and run:
./clean_up.sh
which will remove all the files it statically drops on your system.

Resources