Using xargs parameterrs as variables to compare two md5sum - bash

I'm extracting two md5sums by using this code:
md5sum test{1,2} | cut -d' ' -f1-2
I'm receiving two md5sums as in example below:
02eace9cb4b99519b49d50b3e44ecebc
d8e8fca2dc0f896fd7cb4cb0031ba249
Afterwards I'm not sure how to compare them. I have tried using the xargs:
md5sum test{1,2} | cut -d' ' -f1-2 | xargs bash -c '$0 == $1'
However, it tries to execute md5sum as a command
Any advice?

Try using a command subsitution instead
#!/bin/bash
echo 1 > file_a
echo 2 > file_b
echo 1 > file_c
file1=file_a
# try doing "file2=file_b" as well
file2=file_c
if [[ $(sha1sum $file1 | cut -d ' ' -f1-2) = $(sha1sum $file2 | cut -d ' ' -f1-2) ]]; then
echo same
else
echo different
fi

Related

Enclose text after equals with double quotes

I am getting some data and putting that into a file with a sed command
awk '/name_of_the_person/{getline; print}' file_name | cut -d '>' -f 2 | cut -f 1 -d'<' |sed 's/^/export FIRST_NAME=/' >> /tmp/source
awk '/clan_of_the_person/{getline; print}' file_name | cut -d '>' -f 2 | cut -f 1 -d'<' |sed 's/^/export LAST_NAME=/' >> /tmp/source
and then I finally source this file to export the variables.
The problem that I am facing is that sometimes some people have multiple first names and last names and my file ends up like this
export FIRST_NAME=JEAN PAUL
export LAST_NAME=SMITH
This ends up breaking my export statement. Is there a way I can add quotes after the = till the end of the line so that it becomes
export FIRST_NAME="JEAN PAUL"
With GNU sed and a back-reference:
echo 'export FIRST_NAME=JEAN PAUL' | sed -E 's/=(.*)/="\1"/'
Output:
export FIRST_NAME="JEAN PAUL"
You could use a subshell to isolate the match. This has an added advantage of taking out the sed:
echo "export FIRST_NAME=\"$(awk '/name_of_the_person/{getline; print}' file_name | cut -d '>' -f 2 | cut -f 1 -d'<')\"" >> /tmp/source
echo "export LAST_NAME=\"$(awk '/clan_of_the_person/{getline; print}' file_name | cut -d '>' -f 2 | cut -f 1 -d'<')\"" >> /tmp/source
It might look cleaner with separate variables:
fname="$(awk '/name_of_the_person/{getline; print}' file_name | cut -d '>' -f 2 | cut -f 1 -d'<')"
lname="$(awk '/clan_of_the_person/{getline; print}' file_name | cut -d '>' -f 2 | cut -f 1 -d'<')"
{
echo "export FIRST_NAME=\"$fname\""
echo "export LAST_NAME=\"$lname\""
} >> /tmp/source

grep search with filename as parameter

I'm working on a shell script.
OUT=$1
here, the OUT variable is my filename.
I'm using grep search as follows:
l=`grep "$pattern " -A 15 $OUT | grep -w $i | awk '{print $8}'|tail -1 | tr '\n' ','`
The issue is that the filename parameter I must pass is test.log.However, I have the folder structure :
test.log
test.log.001
test.log.002
I would ideally like to pass the filename as test.log and would like it to search it in all log files.I know the usual way to do is by using test.log.* in command line, but I'm facing difficulty replicating the same in shell script.
My efforts:
var-$'.*'
l=`grep "$pattern " -A 15 $OUT$var | grep -w $i | awk '{print $8}'|tail -1 | tr '\n' ','`
However, I did not get the desired result.
Hopefully this will get you closer:
#!/bin/bash
for f in "${1}*"; do
grep "$pattern" -A15 "$f"
done | grep -w $i | awk 'END{print $8}'

bash script prints "No such file or directory" when comparing filenames to string

I am checking to see if a file I am hoping to create conflicts with a file that has the same name.
FILEPATH=/root/logs/pData*.csv
COMPPATH=/root/logs/pData*.csv.gz
shopt -s nullglob
thisYear="$(date +"%Y")"
thisMonth="$(date +"%m")"
thisDay="$(date +"%d")"
thisTime="$(date | cut -d ' ' -f 4 | tr : _)"
for file in $FILEPATH
do
fileYear="$(stat -c %y $file | cut -d'-' -f 1)"
fileMonth="$(stat -c %y $file | cut -d'-' -f 2)"
fileDay="$(stat -c %y $file | cut -d'-' -f 3 | cut -d' ' -f 1)"
fileTime="$(stat -c %y $file | cut -d ' ' -f 2 | cut -d '.' -f 1 | tr : _)"
if (("$fileYear" < '1990'))
then
fName="pData_"$thisYear"_"$thisMonth"_"$thisDay"_"$thisTime".csv.gz"
else
fName="pData_"$fileYear"_"$fileMonth"_"$fileDay"_"$fileTime".csv.gz"
fi
echo $fName
for file in $COMPPATH
do
if ('/root/logs/'$fName == $file)
then
echo "OOPS"
fi
done
done
The script works as intended for the most part, printing OOPS when I run into a file of the same name, but for files that don't exist it prints
./compress.sh: line 31: /root/logs/pData_2015_09_18_22_25_44.csv.gz: No such file or directory
Why is this printed?
How do I prevent this from happening?
The string comparison is wrong. Using single parentheses is creating a sub-shell and trying to execute '/root/logs/'$fName
Set your string compare to be:
if [[ '/root/logs/'$fName = $file ]]
See: http://www.tldp.org/LDP/abs/html/comparison-ops.html

BASH: Remove newline for multiple commands

I need some help . I want the result will be
UP:N%:N%
but the current result is
UP:N%
:N%
this is the code.
#!/bin/bash
UP=$(pgrep mysql | wc -l);
if [ "$UP" -ne 1 ];
then
echo -n "DOWN"
else
echo -n "UP:"
fi
df -hl | grep 'sda1' | awk ' {percent+=$5;} END{print percent"%"}'| column -t && echo -n ":"
top -bn2 | grep "Cpu(s)" | \sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \awk 'END{print 100 - $1"%"}'
You can use command substitution in your first sentence (notice you're creating a subshell in this way):
echo -n $(df -hl | grep 'sda1' | awk ' {percent+=$5;} END{print percent"%"}'| column -t ):

How to reduce the use of `echo` in a bash script?

My bash script contains the following line:
echo $(echo "$STRING_VAR" | cut -d' ' -f 2) >> $FILE
Here we have two echo calls, but are they really necessary ?
I wrote them, because otherwise the bash would think the string in first place is a command.
Simply echo "$STRING_VAR" | cut -d' ' -f 2 >> $FILE does the same thing.
echo "$STRING_VAR" | cut -d' ' -f 2 >> $FILE
should be all you need
Also, bash has the handy "here-string" redirection mode: you don't need echo at all:
cut -d' ' -f2 <<< "$STRING_VAR" >> "$FILE"

Resources