How to get just a list of yum updates - bash

OK I have always had this problem. I want JUST the available updates listed in a file via bash script from a Linux system (RHEL or Fedora) using yum but I always have to deal with the Header information created which looks like this:
Loaded plugins: XXXX-repo XXXX-updates
: WWWWWW-repo something-updates QQQQQ-updates
Updated packages
package1.i686 1:234 RHEL 6.5 updates
package2.i686 1:234 RHEL 6.5 updates
package3.i686 1-234 RHEL 6.5 updates
package4.noarch 1.234 RHEL 6.5 updates
All I want is a list of package1,package2, etc. which seems simple enough but it isn't because I can't just grep on "updates" or ":". Am I looking at this wrongly? Why would I not want to capture what updates were found in a script? Should I just update and check what has been updated instead? Thoughts?
PS> I can not use --noplugins option.
EDIT: So far I have come up with this,
sudo yum check-update | grep "\." | awk '(NR >=1) {print $1;}' | grep '^[[:alpha:]]'
Basically grab the lines with a period in them, the first line, and make sure it first contains alpha letters. Perhaps over done but it seems to work.

To only print lines following (but not including) "Updated packages"
yum check-update | awk 'p; /Updated packages/ {p=1}'
Note, on my Fedora system, a blank line separates the "header" from the list of updatable packages, so I would use awk 'p;/^$/{p=1}'

If you pipe the output above into awk using this command:
| awk '(NR >=4) {print $1;}'
You will get the following output
package1.i686
package2.i686
package3.i686
package4.noarch
The (NR >=4) tells awk to ignore the first three lines. The {print $1;} tells awk to print the first word of each line.
You can read here for more information on cutting stuff out after certain characters on each line.
You can then use sed if stripping out everything after the . is important
| awk '(NR >=4) {print $1;}' | sed s/\.[^\.]*$//
Gives the following output
package1
package2
package3
package4
Then pipe it into another sed command to replace the linebreaks with a comma.
| awk '(NR >=4) {print $1;}' | sed s/\.[^\.]*$// | sed ':a;N;$!ba;s/\n/,/g'
Yields the following output
package1,package2,package3,package4

A more flexible solution
The solution below does not assume a specific number of lines in the Header (Ex. in CentOS I got much more header lines).
Nor does it suppose that you are only interested in the repository updates.
yum check-update | awk '/\S+\s+[0-9]\S+\s+\S+/ {print $1 }' > updates
Example
For the following yum check-update output
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 31 kB 00:00:00
* base: asi-fs-m.net
Excluding mirror: mirror.de.leaseweb.net
Excluding mirror: mirror.fra10.de.leaseweb.net
base | 3.6 kB 00:00:00
cwp | 2.9 kB 00:00:00
extras | 3.4 kB 00:00:00
mariadb | 2.9 kB 00:00:00
remi-safe | 3.0 kB 00:00:00
updates | 3.4 kB 00:00:00
remi-safe/primary_db | 1.4 MB 00:00:00
openvpn.x86_64 2.4.7-1.el7 epel
polkit.x86_64 0.112-18.el7_6.1 updates
pure-ftpd.x86_64 1.0.47-2.el7 epel
remi-release.noarch 7.6-2.el7.remi remi-safe
You can get
openvpn.x86_64
polkit.x86_64
pure-ftpd.x86_64
remi-release.noarch
Explanation
This solution assumes that the relevant lines have the pattern
<package name><spaces><version number><spaces><repo name>
If you want to output a particular repository, then use the pattern
/\S+\s+[0-9]\S+\s+repo_name/
PS:
If this solution does not work in your system, let me know in a comment

Try this:
yum check-update | awk '{if($5 ~ /updates/){print $1}}' | tr '\n' ','
If the input contains 'updates' on fifth column then print first column and create a csv list.

Isn't this easier:
yum check-update -q | awk '{print $1}'
Edited.
The explanation of the command:
yum check-update -p - will list the updates in a 3 column list which are package + new version + repository.
awk '{ print $1 }' - will pick up the first column ( package )

Related

Extracting release number from Jira created bitbucket branch

I am using Jira to create a bitbucket branch for releases. As part of the build process I need to extract the release number from the branch name.
An earlier part of the build dumps the whole branch name to a text file. The problem I'm having is removing all the text before the build number.
An example branch name would be:
release/some-jira-ticket-343-X.X.X
Where X.X.X is the release number e.g. 1.11.1 (each of X could be any length integer).
My first thought was to literally just select the last 3 characters with sed, however as X could be any length this won't work.
Another post (Removing non-alphanumeric characters with sed) suggesting using the sed alpha class. However this won't work as the jira ticket ID will have numbers in.
Any ideas?
You can remove all characters up to last -:
$ sed 's/.*-//' <<< "release/some-jira-ticket-343-1.11.2"
1.11.2
or with grep, to output only digits and dots at the end of the line:
grep -o '[0-9.]*$'
awk solution,
$ awk -F- '{print $NF}' <<< "release/some-jira-ticket-343-1.11.1"
grep solution,
grep -oP '[0-9]-\K.*' <<< "release/some-jira-ticket-343-1.11.1"
use string operators:
var="release/some-jira-ticket-343-2.155.7"
echo ${var##*-}
print:
2.155.7
Awk solution:
awk -F [-.] '{ print $5"."$6"."$7 }' <<< "release/some-jira-ticket-343-12.4.7"
12.4.7
Set the field delimiter to - and . and then extract the pieces of data we need.

How to restructure the line with proper alignment in shell script

I am trying to get the Software Information from Red Hat. As far as I have tried,
rpm -qi <softwarename>
provides the enough information
from the above information, I do require Name, Version, Release, Vendor, Build Date, & Install Date.
I could able to get the Name Version Release & Install Date by using the following command.(here I have grepped only name and version for simplicity)
rpm -qi perl-Git | grep -E '^Name :|^Version :' | awk -F 'Relocations:|Vendor:' '{print $1}'
I have no clue to get the Right side of the Information, since I could not able to find a split string between two fields.
Any suggestions ??
Use --queryformat
$ rpm -q --queryformat "%{NAME}\n%{VERSION}\n%{RELEASE}\n%{VENDOR}\n%{BUILDTIME}\n%{INSTALLTIME}\n" perl-Git
perl-Git
2.8.0
1.WANdisco.308
(none)
1459260423
1493311622
Then you can print it how you like, and parse it how you like.

Get Macbook screen size from terminal/bash

Does anyone know of any possible way to determine or glean this information from the terminal (in order to use in a bash shell script)?
On my Macbook Air, via the GUI I can go to "About this mac" > "Displays" and it tells me:
Built-in Display, 13-inch (1440 x 900)
I can get the screen resolution from the system_profiler command, but not the "13-inch" bit.
I've also tried with ioreg without success. Calculating the screen size from the resolution is not accurate, as this can be changed by the user.
Has anyone managed to achieve this?
I think you could only get the display model-name which holds a reference to the size:
ioreg -lw0 | grep "IODisplayEDID" | sed "/[^<]*</s///" | xxd -p -r | strings -6 | grep '^LSN\|^LP'
will output something like:
LP154WT1-SJE1
which depends on the display manufacturer. But as you can see the first three numbers in this model name string imply the display-size: 154 == 15.4''
EDIT
Found a neat solution but it requires an internet connection:
curl -s http://support-sp.apple.com/sp/product?cc=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | cut -c 9-` |
sed 's|.*<configCode>\(.*\)</configCode>.*|\1|'
hope that helps
The next script:
model=$(system_profiler SPHardwareDataType | \
/usr/bin/perl -MLWP::Simple -MXML::Simple -lane '$c=substr($F[3],8)if/Serial/}{
print XMLin(get(q{http://support-sp.apple.com/sp/product?cc=}.$c))->{configCode}')
echo "$model"
will print for example:
MacBook Pro (13-inch, Mid 2010)
Or the same without perl but more command forking:
model=$(curl -s http://support-sp.apple.com/sp/product?cc=$(system_profiler SPHardwareDataType | sed -n '/Serial/s/.*: \(........\)\(.*\)$/\2/p')|sed 's:.*<configCode>\(.*\)</configCode>.*:\1:')
echo "$model"
It is fetched online from apple site by serial number, so you need internet connection.
I've found that there seem to be several different Apple URLs for checking this info. Some of them seem to work for some serial numbers, and others for other machines.
e.g:
https://selfsolve.apple.com/wcResults.do?sn=$Serial&Continue=Continue&num=0
https://selfsolve.apple.com/RegisterProduct.do?productRegister=Y&country=USA&id=$Serial
http://support-sp.apple.com/sp/product?cc=$serial (last 4 digits)
https://selfsolve.apple.com/agreementWarrantyDynamic.do
However, the first two URLs are the ones that seem to work for me. Maybe it's because the machines I'm looking up are in the UK and not the US, or maybe it's due to their age?
Anyway, due to not having much luck with curl on the command line (The Apple sites redirect, sometimes several times to alternative URLs, and the -L option doesn't seem to help), my solution was to bosh together a (rather messy) PHP script that uses PHP cURL to check the serials against both URLs, and then does some regex trickery to report the info I need.
Once on my web server, I can now curl it from the terminal command line and it's bringing back decent results 100% of the time.
I'm a PHP novice so I won't embarrass myself by posting the script up in it's current state, but if anyone's interested I'd be happy to tidy it up and share it on here (though admittedly it's a rather long winded solution to what should be a very simple query).
This info really should be simply made available in system_profiler. As it's available through System Information.app, I can't see a reason why not.
Hi there for my bash script , under GNU/Linux : I make the follow to save
# Resolution Fix
echo `xrandr --current | grep current | awk '{print $8}'` >> /tmp/width
echo `xrandr --current | grep current | awk '{print $10}'` >> /tmp/height
cat /tmp/height | sed -i 's/,//g' /tmp/height
WIDTH=$(cat /tmp/width)
HEIGHT=$(cat /tmp/height)
rm /tmp/width /tmp/height
echo "$WIDTH"'x'"$HEIGHT" >> /tmp/Resolution
Resolution=$(cat /tmp/Resolution)
rm /tmp/Resolution
# Resolution Fix
and the follow in the same script for restore after exit from some app / game
in some S.O
This its execute command directly
ResolutionRestore=$(xrandr -s $Resolution)
But if dont execute call the variable with this to execute the varible content
$($ResolutionRestore)
And the another way you can try its with the follow for example
RESOLUTION=$(xdpyinfo | grep -i dimensions: | sed 's/[^0-9]*pixels.*(.*).*//' | sed 's/[^0-9x]*//')
VRES=$(echo $RESOLUTION | sed 's/.*x//')
HRES=$(echo $RESOLUTION | sed 's/x.*//')

argument getting truncated while printing in unix after merging files

I am trying to combine two tab seperated text files but one of the fields is being truncated by awk when I use the command (pls suggest something other than awk if it is easier to do so)
pr -m -t test_v1 test.predict | awk -v OFS='\t' '{print $4,$5,$7}' > out_test8
The format of the test_v1 is
478 192 46 10203853138191712
but I only print 10203853138 for $4 truncating the other digits. Should I use string format?
Actually I found out after a suggestion given that pr -m -t itself does not give the correct output
478^I192^I46^I10203853138^I^I is the output of the command
pr -m -t test_v1 test.predict | cat -vte
I used paste test_v1 test.predict instead of pr and got the right answer.
You problem is use pr -m (merge) here which as per manual:
-m, --merge
print all files in parallel, one in each column, truncate lines, but join lines of full length with -J
You can use:
paste test_v1 test.predict
Run dos2unix on your files first, you've just got control-Ms in your input file(s).

find latest version of rpms from a mirror

I want to write a script to find the latest version of rpm of a given package available from a mirror for eg: http://mirror.centos.org/centos/5/updates/x86_64/RPMS/
The script should be able to run on majority of linux flavors (eg centos, redhat, ubuntu). So yum based solution is not an option. Is there any existing script that does this? Or can someone give me a general idea on how to go about this?
Thx to levislevis85 for the wget cli. Try this:
ARCH="i386"
PKG="pidgin-devel"
URL=http://mirror.centos.org/centos/5/updates/x86_64/RPMS
DL=`wget -O- -q $URL | sed -n 's/.*rpm.>\('$PKG'.*'$ARCH'.rpm\).*/\1/p' | sort | tail -1`
wget $URL/$DL
I Will put my comment here, otherwise the code will not be readable.
Try this:
ARCH="i386"
PKG="pidgin-devel"
URL=http://mirror.centos.org/centos/5/updates/x86_64/RPMS
DL=`wget -O- -q $URL | sed -n 's/.*rpm.>\('$PKG'.*'$ARCH'.rpm\).*<td align="right">\(.*\)-\(.*\)-\(.*\) \(..\):\(..\) <\/td><td.*/\4 \3 \2 \5 \6 \1/p' | sort -k1n -k2M -k3n -k4n -k5n | cut -d ' ' -f 6 | tail -1`
wget $URL/$DL
What it does is:
wget - get the index file
sed - cut out some parts and put it together in different order. Should result in Year Month Day Hour Minute and Package, like:
2009 Oct 27 01 14 pidgin-devel-2.6.2-2.el5.i386.rpm
2009 Oct 30 10 49 pidgin-devel-2.6.3-2.el5.i386.rpm
sort - order the columns n stays for numerical and M for month
cut - cut out the filed 6
tail - show only last entry
the problem with this could be, if some older package release comes after a newer then this script will also fail. If the output of the site changes, the script will fail. There are always a lot of points where a script could fail.
using wget and gawk
#!/bin/bash
pkg="kernel-headers"
wget -O- -q http://mirror.centos.org/centos/5/updates/x86_64/RPMS | awk -vpkg="$pkg" 'BEGIN{
RS="\n";FS="</a>"
z=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",D,"|")
for(i=1;i<=z;i++){
date[D[i]]=sprintf("%02d",i)
}
temp=0
}
$1~pkg{
p=$1
t=$2
gsub(/.*href=\042/,"",p)
gsub(/\042>.*/,"",p)
m=split(t,timestamp," ")
n=split(timestamp[1],d,"-")
q=split(timestamp[2],hm,":")
datetime=d[3]date[d[2]]d[1]hm[1]hm[2]
if ( datetime >= temp ){
temp=datetime
filepkg = p
}
}
END{
print "Latest package: "filepkg", date: ",temp
}'
an example run of the above:
linux$ ./findlatest.sh
Latest package: kernel-headers-2.6.18-164.6.1.el5.x86_64.rpm, date: 200911041457
Try this (which requires lynx):
lynx -dump -listonly -nonumbers http://mirror.centos.org/centos/5/updates/x86_64/RPMS/ |
grep -E '^.*xen-libs.*i386.rpm$' |
sort --version-sort |
tail -n 1
If your sort doesn't have --version-sort, then you'll have to parse the version out of the filename or hope that a regular sort will do the right thing.
You may be able to do something similar with wget or curl or even a Bash script using redirections with /dev/tcp/HOST/PORT. The problem with these is that you would then have to parse HTML.

Resources