Bash script automation checking if certain version of app is installed or not [closed] - linux

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to automate some basics in my Linux Server, I would like to check if the certain version of application like git, python or some other apps are installed or not, if installed than update to latest stable version. Here is my script and it seems I'm missing something:
#!/bin/sh
CURRENT_OS=$(hostnamectl | awk '/Operating/ { print $3 }')
CURRENT_PY=$(python3 -V | awk '{ print $2 }')
CURRENT_GT=$(git --version | awk '{ print $3 }')
if [ $CURRENT_OS=~"Fedora" || $CURRENT_OS=~"Centos" || $CURRENT_OS=~"Red Hat" ]; then
echo "➥➥➥ Current System Is Based On $CURRENT_OS"
sudo dnf clean all
sudo dnf -y upgrade
if [ $CURRENT_PY!="Python 3.8" ]; then
echo "➥➥➥ Installing Latest Python Release"
sudo dnf -y install python3.8
else
echo "➥➥➥ Latest Python Release Found In System"
fi
fi
Also is there any chance to swap output of command python3 -V | awk '{ print $2 }' to 3.8 instead of 3.8.5?
And also how can I refactor the OS release check so either is Red Hat, Centos or Fedora?

You can filter the the Python version using sed, e.g.
python3 -V | awk '{ print $2 }' | sed 's/\([0-9]*\.[0-9]*\).*/\1/'
The question about the OS release check is not clear. If you only want to have the correct distribution name in the output you can use e.g.
echo "➥➥➥ Current System Is Based On $CURRENT_OS"
Edit:
This was not meant as a fix for a "typo" but my (possibly wrong) interpretation of the unclear question.
If you want to find a common criteria for Red Hat, Centos or Fedora, the existence of a file /etc/redhat-release might be an indication for this family of distributions as /etc/debian_version is an indication for a Debian based distribution (including e.g. Ubuntu). That means a check like
if [ -e /etc/redhat-release ]; then
echo "➥➥➥ Current system seems to be a distribution of the RedHat family"
# ...
fi
might do what you need.
Please clarify the question.

If you are only dealing with CentOS/Fedora/Red Hat you can use /etc/redhat-release to find which distribution it is, or also lsb_release -i instead of hostnamectl.
Also on Fedora and CentOS you usually also have fedora-release and centos-release files, in addition to redhat-release.
You can also grep ID from /etc/os-release.
[user#redhat8 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.1 (Ootpa)
[user#redhat8 ~]# lsb_release -i
Distributor ID: RedHatEnterprise
[user#redhat8 ~]# ls -l /etc/*release
-rw-r--r--. 1 root root 501 Sep 25 2019 /etc/os-release
-rw-r--r--. 1 root root 45 Sep 25 2019 /etc/redhat-release
lrwxrwxrwx. 1 root root 14 Sep 25 2019 /etc/system-release -> redhat-release
[user#redhat8 ~]# grep -w ID /etc/os-release
ID="rhel"
[user#centos7 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root#centos7 ~]# lsb_release -i
Distributor ID: CentOS
[user#centos7 ~]# ls -l /etc/*release
-rw-r--r--. 1 root root 38 Nov 23 2018 /etc/centos-release
-rw-r--r--. 1 root root 393 Nov 23 2018 /etc/os-release
lrwxrwxrwx. 1 root root 14 Apr 15 09:05 /etc/redhat-release -> centos-release
lrwxrwxrwx. 1 root root 14 Apr 15 09:05 /etc/system-release -> centos-release
[user#centos7 ~]# grep -w ID /etc/os-release
ID="centos"
[user#Fedora ~]$ cat /etc/redhat-release
Fedora release 32 (Thirty Two)
[user#Fedora ~]$ lsb_release -i
Distributor ID: Fedora
[user#fedora ~]$ ls -l /etc/*release
lrwxrwxrwx. 1 root root 25 Jun 5 22:00 /etc/fedora-release -> ../usr/lib/fedora-release
lrwxrwxrwx. 1 root root 21 Jun 5 22:00 /etc/os-release -> ../usr/lib/os-release
lrwxrwxrwx. 1 root root 14 Jun 5 22:00 /etc/redhat-release -> fedora-release
lrwxrwxrwx. 1 root root 14 Jun 5 22:00 /etc/system-release -> fedora-release
[user#fedora ~]$ grep -w ID /etc/os-release
ID=fedora
You will need to have redhat-lsb-core package installed to have lsb_release command on Red Hat based distributions.

Related

Get clean list of file sizes and names using SFTP in unix

I want to fetch list of files from a server using SFTP one by one only if their size is less than 1 GB.
I am running the following command :
$sftp -oIdentityFile=/home/user/.ssh/id_rsa -oPort=22 user#hostname >list.txt <<EOF
cd upload/Example
ls -l iurygify*.zip
EOF
This results in:
$cat list.txt
sftp> cd upload/Example
sftp> ls -l iurygify*.zip
-rwxrwx--- 0 300096661 300026669 0 Mar 11 16:38 iurygify1.zip
-rwxrwx--- 0 300096661 300026669 0 Mar 11 16:38 iurygify2.zip
I could then use awk to calculate get the size and filename which I can save into logs for reference and then download only those files which meet the 1 GB criteria.
Is there any simpler approach to accomplish getting this file list and size? I want to avoid the junk entires of the prompt and commands in the list.txt and do not want to do this via expect command.
We are using SSH key authentication
You could place your sftp commands in a batch file and filter the output - no need for expect.
echo 'ls -l' > t
sftp -b t -oIdentityFile=/home/user/.ssh/id_rsa -oPort=22 user#hostname | grep -v 'sftp>' >list.txt
Or take it a step further and filter out the "right size" in the same step:
sftp -b t -oIdentityFile=/home/user/.ssh/id_rsa -oPort=22 user#hostname | awk '$1!~/sftp>/&&$5<1000000000' >list.txt
Maybe using lftp instead of sftp ?
$ lftp sftp://xxx > list.txt <<EOF
> open
> ls -l
> EOF
$ cat list.txt
drwxr-xr-x 10 ludo users 4096 May 24 2019 .
drwxr-xr-x 8 root root 4096 Dec 20 2018 ..
-rw------- 1 ludo users 36653 Mar 31 19:28 .bash_history
-rw-r--r-- 1 ludo users 220 Mar 21 2014 .bash_logout
-rw-r--r-- 1 ludo users 362 Aug 16 2018 .bash_profile
...

Unable to upgrade Perl 5.8 to 5.16 in RHEL 7.6

6 and am unable to upgrade my machine from 5.8.9. When I install using yum install perl, it says successfully installed, but when I check version it is still the old one. I need the version to be 5.10 or above. Following are the details:
bash$ cat /etc/*release
NAME="Red Hat Enterprise Linux Server"
VERSION="7.6 (Maipo)"
bash$
bash$ yum info perl
Name : perl
Arch : x86_64
Epoch : 4
Version : 5.16.3
Release : 294.el7_6
Size : 22 M
Repo : installed
From repo : rhel-7-server-rpms
Summary : Practical Extraction and Report Language
URL : http://www.perl.org/
bash$ perl -v
This is perl, v5.8.9 built for x86_64-linux-thread-multi
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2008, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
bash$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/puppetlabs/bin:/a
bash$ whereis perl
perl: /usr/bin/perl /usr/local/bin/perl5.6 /usr/local/bin/perl5.8 /usr/local/bin/perl /auto/usrcisco-linux-rhel7.0-x86-64/packages/perl/perl-5.8.9/bin/perl /usr/share/man/man1/perl.1.gz
Solution:
bash$ whereis perl5.16.3
perl5.16: /usr/bin/perl5.16.3
bash$ ll | grep -i perl
lrwxrwxrwx. 1 root root 19 Jul 2 2019 perl -> /usr/ravi/bin/perl
bash$ sudo ln -s -f /usr/bin/perl5.16.3 perl
bash$
bash$ ll | grep -i perl
lrwxrwxrwx 1 root root 19 Feb 22 17:49 perl -> /usr/bin/perl5.16.3
bash$ perl --version
perl -v
This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 39 registered patches, see perl -V for more detail)
Copyright 1987-2012, Larry Wall
Thanks in Advance for any input
bash$ whereis perl5.16.3
perl5.16: /usr/bin/perl5.16.3
bash$ ll | grep -i perl
lrwxrwxrwx. 1 root root 19 Jul 2 2019 perl -> /usr/ravi/bin/perl
bash$ sudo ln -s -f /usr/bin/perl5.16.3 /usr/bin/perl
bash$
bash$ ll | grep -i perl
lrwxrwxrwx 1 root root 19 Feb 22 17:49 perl -> /usr/bin/perl5.16.3
bash$ perl --version
perl -v
This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 39 registered patches, see perl -V for more detail)
Copyright 1987-2012, Larry Wall

how to get uuid of filesystem given a path?

I am handed a path of a directory ( sometimes path of a file ).
Which utility / shell script will reliably give me the UUID of the filesystem on which is this directory ( or file ) located / stored ?
( by UUID of filesystems I mean the "UUID=..." entry as shown by e.g. blkid )
( this is happnening on a redhat linux )
give this line a try:
sudo blkid -o value $(\df --output=source "$file"|tail -1)|head -1
in above line, $file is the variable to save the file/dir. You may want to check if the file/dir exists, before call the line.
And this line needs root permission (sudo)
\df is just for avoiding to use alias if you had one, for example with -T option, it conflicts with --output
Some test :
kent$ file="/home/kent/.vimrc"
kent$ sudo blkid -o value $(\df --output=source "$file"|tail -1)|head -1
9da1040a-4a24-4a00-9c62-bad8cc3c028d
kent$ file="/etc"
kent$ sudo blkid -o value $(\df --output=source "$file"|tail -1)|head -1
2860a386-af71-4a28-86d7-00ccf5d12b4d
Find the device of the mount point of the path,
DEVICE=$(df /path/to/some_file_or_directory | grep "$MOUNTPOINT\$"| cut -f1 -d" ")
and get the UUID of the device:
sudo blkid $DEVICE
Simply, you can type like this,
pchero#mywork:~$ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Jan 23 09:03 0267689b-b929-4f30-b8a4-08c742f0746f -> ../../sda2
lrwxrwxrwx 1 root root 10 Jan 23 09:03 2d682ea1-dab0-49ba-a77a-9335ccd47e58 -> ../../sda3
lrwxrwxrwx 1 root root 10 Jan 23 09:03 64e733e9-2e6a-4d3e-aabe-d0d26fbfc991 -> ../../sda1
lrwxrwxrwx 1 root root 10 Jan 23 09:03 a99fb356-4e01-4a1c-af41-001b0fd8a844 -> ../../sdb1
lrwxrwxrwx 1 root root 10 Jan 23 09:03 f2f7618e-76c5-4e9a-9657-e002d9a66ccf -> ../../sda4

how do I retrieve 34 from "#34 PREEMPT Mon Mar 25 14:24:26 SGT 2013"

I am new on Linux, how do I retrieve "34" from uname -v command?
My uname -v command result is #34 PREEMPT Mon Mar 25 14:24:26 SGT 2013
I wanna the number 34 only.
How about:
[cnicutar#ariel ~]$ echo '#34 PREEMPT ...' | sed 's/^#\([0-9]\+\).*/\1/'
34
You can also make that easier with -r:
sed -r 's/^#([0-9]+).*/\1/'
Using grep to show only the matching part of a line (-o) matching a # followed by one or more (+) digits ([0-9]):
uname -v | grep -o '#[0-9]\+'

How to check syslog in Bash on Linux?

In C we log this way:
syslog( LOG_INFO, "proxying %s", url );
In Linux how can we check the log?
How about less /var/log/syslog?
On Fedora 19, it looks like the answer is /var/log/messages. Although check /etc/rsyslog.conf if it has been changed.
By default it's logged into system log at /var/log/syslog, so it can be read by:
tail -f /var/log/syslog
If the file doesn't exist, check /etc/syslog.conf to see configuration file for syslogd.
Note that the configuration file could be different, so check the running process if it's using different file:
# ps wuax | grep syslog
root /sbin/syslogd -f /etc/syslog-knoppix.conf
Note: In some distributions (such as Knoppix) all logged messages could be sent into different terminal (e.g. /dev/tty12), so to access e.g. tty12 try pressing Control+Alt+F12.
You can also use lsof tool to find out which log file the syslogd process is using, e.g.
sudo lsof -p $(pgrep syslog) | grep log$
To send the test message to syslogd in shell, you may try:
echo test | logger
For troubleshooting use a trace tool (strace on Linux, dtruss on Unix), e.g.:
sudo strace -fp $(cat /var/run/syslogd.pid)
A very cool util is journalctl.
For example, to show syslog to console: journalctl -t <syslog-ident>, where <syslog-ident> is identity you gave to function openlog to initialize syslog.
tail -f /var/log/syslog | grep process_name
where process_name is the name of the process we are interested in
If you like Vim, it has built-in syntax highlighting for the syslog file, e.g. it will highlight error messages in red.
vi +'syntax on' /var/log/syslog
On some Linux systems (e.g. Debian and Ubuntu) syslog is rotated daily and you have multiple log files where two newest files are uncompressed while older ones are compressed:
$ ls -l /var/log/syslog*
-rw-r----- 1 root adm 888238 Aug 25 12:02 /var/log/syslog
-rw-r----- 1 root adm 1438588 Aug 25 00:05 /var/log/syslog.1
-rw-r----- 1 root adm 95161 Aug 24 00:07 /var/log/syslog.2.gz
-rw-r----- 1 root adm 103829 Aug 23 00:08 /var/log/syslog.3.gz
-rw-r----- 1 root adm 82679 Aug 22 00:06 /var/log/syslog.4.gz
-rw-r----- 1 root adm 270313 Aug 21 00:10 /var/log/syslog.5.gz
-rw-r----- 1 root adm 110724 Aug 20 00:09 /var/log/syslog.6.gz
-rw-r----- 1 root adm 178880 Aug 19 00:08 /var/log/syslog.7.gz
To search all the syslog files you can use the following commands:
$ sudo zcat -f `ls -tr /var/log/syslog*` | grep -i error | less
where zcat first decompresses and prints all syslog files (oldest first), grep makes a search and less is paging the results of the search.
To do the same but with the lines prefixed with the name of the syslog file you can use zgrep:
$ sudo zgrep -i error `ls -tr /var/log/syslog*` | less
$ zgrep -V | grep zgrep
zgrep (gzip) 1.6
In both cases sudo is required if syslog files are not readable by ordinary users.

Resources