getting SW version by bash script for uninstall preinstalled software/notifying easily by assigning variable to it. Please share more ideas - linux

Please share more ideas to get software version from bash command and use it as variable later.
su --version
su (GNU coreutils) 5.97
Copyright etc.
and create variable of the result of it.
Something like I tried below.
su --version >/tmp/temp.txt
if [ -f /tmp/temp.txt ]; then
elv=`cat /tmp/temp.txt | gawk 'BEGIN {FS="(GNU coreutils)"} {print $2}' | gawk 'BEGIN {FS="."} {print $1}'`
#Version String. Just a shortcut to be used later
els=el$elv
else
echo "Unable to determine version. I can't continue"
exit 1
fi
if [ `rpm -qa | egrep -c -i "^mysql-"` -gt 0 ]; then
cat << EOF
It appears that the distro-supplied version of MySQL is at least partially installed,
or a prior installation attempt failed.
Please remove these packages, as well as their dependencies (often postfix), and then
retry this script:
$(rpm -qa | egrep -i "^mysql-")
EOF
exit 1
fi

Related

awk parsing output to file with optirun not working

Noob here.
I have ccminer-cryptonight compiled and running on my ubuntu 16.04 thinkpad.
However, since my nvidia gpu is old and not supported by the latest cuda, I have to use the integrated Intel gpu for X and use bumblebee for ccminer and nvidia gpu so my screen wouldn't freeze...
here is the command I use:
optirun ccminer -a cryptonight -o stratum+tcp://miningpooladdress.com:5000 -u username -p "password" -P -R 15
ccminer gives a lot of output I only want to monitor the hashrate since there is a bug when the hashrate goes insanely high which means ccminer has stopped mining, so I have to kill it and restart.
this is the awk command I use to parse hashrate:
optirun ccminer -a cryptonight -o stratum+tcp://miningpooladdress.com:5000 -u username -p "password" -P -R 15 2>&1 | awk '/5400M,/ {print $7}'
which parse the line of hashrate readout, my card is NVS 5400M, the output looks like:
43.43
54.23
32.67
44.89
xx.xx
xx.xx
Now I want to write this output to a log file, I tried:
optirun ccminer -a cryptonight -o stratum+tcp://miningpooladdress.com:5000 -u username -p "password" -P -R 15 2>&1 | awk '/5400M,/ {print $7 >> "logfile"}'
and
optirun ccminer -a cryptonight -o stratum+tcp://miningpooladdress.com:5000 -u username -p "password" -P -R 15 2>&1 | awk '/5400M,/ {print $7}' >> "logfile"
none of these two works, the "logfile" will be created but remain empty what am I doing wrong? Why can I get the screen output but can't write to the file?
Thanks for helps.
Update regarding ccminer-cryptonight: a simple solution of lazy miner behaviours - run as root ;P
awk is buffering its output so change your awk command to:
awk '/5400M,/ {print $7; fflush()}'
For other buffering issues, google stdbuf.

syntax error near unexpected token `<<<'

I have a bash script, which runs correctly in my system:
uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64 GNU/Linux
But I need it to work in a Redhat 7.2 chroot:
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64 unknown
The same code executes correctly on the first one, but when running it on 7.2 first it doesn't recognize sed -i (just the -i argument). Commenting some lines y run into another problem:
bash: command substitution: line 1: syntax error near unexpected token `<<<'
The thing is, that this script need to be executed in a remote machine with a debian 7.2 (that's why i'm testing it in a chroot with the same distro), so it's no solution to install modules/upgrades to make it runnable.
A sample code:
#!/bin/bash
...
count=0
while read line; do
if echo "$line" | grep -q ')'
then
((count++))
comas=`grep -o "," <<< "$line" | wc -l`
num=`grep -o "byRef" <<< "$line" | wc -l`
...
sed -i 's/shortInteger/int/g' "test.h"
Any ideas?
Thanks.
Edit:
These are the commands that cause me trouble:
comas=`grep -o "," <<< "$line" | wc -l`
sed -i 's/shortInteger/int/g' "test.h"
Edit 2:
GNU bash, version 2.05.8 --> "here string" (<<<) doesn't exist
grep (GNU grep) 2.4.2 --> -o option doesn't exist
GNU sed version 3.02 --> -i option doesn't exist
Finally I got it with the following...
For the versions i have which i can't update:
sed solution
sed 's/shortInteger/int/g' "test.h" > test.temp.h;
mv "test.temp.h" "test.h"
This was pointed out by #sorontar. Thanks a lot!
To find special characters or substrings inside a string
comas=$(echo "$line" | tr " " "\n" | grep -c ",")
According to my source files, there's a pattern so after any comma I have an space. So tr " " "\n" separates substrings between spaces making newlines, then I can use grep -c "," to count each line with a comma inside.

need bash script to strip version in a binary file and compare to db version

trying to setup a bash script to look for the version in a binary file all the versions are included so I am thinking I have to strip out the AUDIT_TRAIL_#_##_A-Z,1-9
This is what I have so far any suggestions
#!/bin/bash
echo searching fmx files AUDIT_TRAIL FOR FORM VERSION
for file in `/bin/ls *.fmx`
do
current_release=`strings $file |sed "s/\"AUDIT_TRAIL/NOTNEEDED/" |grep -i "AUDIT_TRAIL_8" |sed "s/AUDIT\_TRAIL\_/AUDIT\-TRAIL /" |sed "s/\_/\./g" |sed "s/\.[A-Z,a-z]/\.00/" |awk '{print substr($0,0,18)}' |awk '{print $2}'`
export form=$(echo "$file" | cut -f1 -d'.')
export dbform=`echo $form |awk '{print toupper($0)}'`
echo "FORMNAME" $form
sqlplus -s /nolog <<EOF
connect system/xxx#xxxxx
set echo on
whenever oserror exit 88
whenever sqlerror exit 1
spool forms.lst
select GURAOBJ_CURRENT_VERSION from bansecr.GURAOBJ where GURAOBJ_OBJECT = '$dbform';
spool off
exit
EOF
echo $file $current_release
done
OUTPUT
bash-4.1$ ./find_current_release_fmx_db.shl
+ ./find_current_release_fmx_db.shl
./find_current_release_fmx_db.shl: line 1: !/bin/bash: No such file or directory
searching fmx files AUDIT_TRAIL FOR FORM VERSION
FORMNAME peaempl
FROM THE DATABASE
GURAOBJ_CU
----------
8.11.2
peaempl.fmx
FROM THE COMPILED FORM
8.0 8.0 8.0.00 8.0.00 8.1.0. 8.1.0. 8.2.00 8.2.00 8.3 8.3 8.4 8.4 8.7.1 8.7.1 8.7.1. 8.7.1. 8.8.0. 8.8.0. 8.8.1. 8.8.1. **THE ONE I NEED 8.11.2** 8.11.2 8.1.0. 8.8.0. 8.7.1. 8.11.2 8.0.00 8.2.00 8.0 8.3 8.4 8.8.1. 8.7.1
DESIRED OUTPUT
8.11.2
Any help would be greatly appreciated
Let's start by tidying up your script to not use so many pipes and commands, to be more robust, etc.:
#!/bin/bash
printf 'searching fmx files AUDIT_TRAIL FOR FORM VERSION\n'
for file in *.fmx
do
current_release=$(strings "$file" | awk '<something>')
form="${file%%.}"
dbform="${form^^}"
printf 'FORMNAME %s\n' "$form"
sqlplus -s /nolog <<EOF
connect system/xxx#xxxxx
set echo on
whenever oserror exit 88
whenever sqlerror exit 1
spool forms.lst
select GURAOBJ_CURRENT_VERSION from bansecr.GURAOBJ where GURAOBJ_OBJECT = '$dbform';
spool off
exit
EOF
printf '%s %s\n' "$file" "$current_release"
done
Now all we need to know is what the <something> in the awk command should be. I couldn't figure it out from your chain of piped commands as they seem to be adding stuff and then removing it again and escaping things that shouldn't need to be escaped, etc. but once you show the sample output of strings and what you want the awk script to convert that into it should be obvious.

Searching a string in shell script

I am trying to learn shell script. So sorry if my question is so simple.
I am having a file called one.txt and if either strings 1.2 or 1.3 is present in the string then I have to display the success message else the failure message.
The code I tried is follows,
#!/bin/bash
echo "checking"
if grep -q 1.2 /root/one | grep -q 1.3 /root/one; then
echo " vetri Your NAC version"
fi
What I am doing wrong here ?
You can also include the OR in your grep pattern like so:
grep '1.2\|1.3' /root/one
details here
Update:
as twalberg pointed out in the comment, my answer was not precise enough. The better pattern is:
grep '1\.2\|1\.3' /root/one
Or even better, because more compact:
grep '1\.[23]' /root/one
You have to use ||
#!/bin/bash
echo "checking"
if grep -q 1.2 /root/one || grep -q 1.3 /root/one; then
echo " vetri Your NAC version"
fi
Single | operator is called pipe. It will pass the output of the command before | to the command after |.
It is better to join these these greps with | (OR operator):
grep '1.2\|1.3'
or
grep -E '1.2|1.3'
I guess the easier way to do this is to create a variable to check the count of occurrences:
#!/bin/bash
echo "checking"
CHECK=`egrep -c '1\.(2|3)' /root/one`
if [ "$CHECK" -gt 0 ]; then
echo "vetri Your NAC version"
fi

Get version from file name

I'm creating a script that list all the jboss versions. But I was caught in a problem.
Jboss usually has different names for the version.
jboss-4.0.0.tar.gz
jboss-4.0.4.GA.tar.gz
I managed to obtain the version (for example 4.0.0 or 4.0.4). But I need to obtain all the version 4.0.4.GA
ls -1 | grep jboss |sed -r 's/^.*-([0-9.]+)\..*/\1/'
Thanks
Don't parse ls output.
ls is a tool for interactively looking at file information. Its output is formatted for humans and will cause bugs in scripts. Use globs (like I do here) or find instead. Understand why: http://mywiki.wooledge.org/ParsingLs
$ ls -1
jboss-4.0.0.tar.gz
jboss-4.0.4.GA.tar.gz
foobar
Using grep :
$ printf -- '%s\n' * | grep -oP 'jboss-\K.*(?=\.tar\.gz)'
Or using awk :
$ printf -- '%s\n' * | awk -F'jboss-|.tar.gz' '/jboss/{print $2}'
Or using perl :
printf -- '%s\n' * | perl -lne '/jboss-(.*?)\.tar\.gz/ && print $1'
Outputs
4.0.0
4.0.4.GA
$ cat test.sh
#!/bin/bash
for file in jboss-*.tar.gz; do
[ -f "${file}" ] || continue
version="${file#*-}"
version="${version%.tar.gz}"
echo "${version}"
done
Example:
$ find
.
./test.sh
./jboss-4.0.0.tar.gz
./jboss-4.0.4.GA.tar.gz
$ ./test.sh
4.0.0
4.0.4.GA

Resources