bash, md5sum behaves strange - bash

Why is this different?
text="tralala"
echo -n $text | md5sum -
result: def7d827536761c20f449f69262ff20f
echo -n "tralala" | md5sum -
result : 7e4ef92d1472fa1a2d41b2d3c1d2b77a
what am I doing wrong?

I suspect you mistakenly did not provide the -n (output no newline) flag to echo. See sample from my machine below:
$ echo tralala | md5sum
def7d827536761c20f449f69262ff20f -
$ echo -n tralala | md5sum
7e4ef92d1472fa1a2d41b2d3c1d2b77a -
$ text="tralala"
$ echo $text | md5sum
def7d827536761c20f449f69262ff20f -
$ echo -n $text | md5sum
7e4ef92d1472fa1a2d41b2d3c1d2b77a -

Related

Shell exec command with | fails

I write a script with | in command , it run fail .
#! /bin/bash
datestr=`date -d '-1 day' '+%Y-%m-%d'`
cmd="cat /service/domain_detect/api/storage/logs/laravel.log | grep sub_domain | grep $datestr | wc -l"
echo $cmd
count=$($cmd)
echo $count
Expected output is the result line num.
The actual result is follow:
cat /service/domain_detect/api/storage/logs/laravel.log | grep sub_domain | grep 2019-01-17 | wc -l
cat: invalid option -- 'l'
Try 'cat --help' for more information.

Same KSH script run in test env but fails in production

I run the below script in a reference env and get the correct output in the output file, but when I run the same in production the output file is empty.
I tried debugging the using set -x, and understand that the for loop is not getting executed in the prod env.
please suggest what might be the issue.
#!/bin/ksh
DIR=/some/log/dir/error
DATE=$1
OUTPUT=/some/logs/dir/scripts/output/output.csv
. /calling/env/setupscript.ksh
for file in $(find $DIR 2>/dev/null| grep $ASOF | grep -i something1 | grep -vi someotherthing2 | grep -iv someotherthing3 | grep -vi someotherthing4 | grep -vi someotherthing4 )
do
echo "Checking file $file..."
if [[ -z "$DONE" ]] then
head -1 $file | read line
echo "File name;Filter name;Type;Scenario;$line" > $OUTPUT.tmp
DONE=1
fi
fullfile=$file
file=$(basename $file)
file=${file#SPR_RPT_}
file=${file#SPR_BY__}
echo $file | awk -F_ '{ print $(NF-2)" "$(NF-1) }' | read type scen
filter=${file%%_$type\_*}
grep ERROR $fullfile | while read line
do
echo "$(basename $fullfile);$filter;$type;$scen;$line" >> ${OUTPUT}.tmp
done
done

expression using grep is giving all zeros

So I have an expression that I want to extract some lines from a text and count them. I can grep them as follows:
$ cat medsCounts_totals.csv | grep -E 'NumMeds": 0' | wc -l
Which is fine. Now I want to loop over with the string ...
$ for i in {0..10}; do expr="NumMeds\": $i"; echo $expr; done
However, when I try to use $expr
for i in {0..10}; do expr="NumMeds:\" $i"; cat medsCounts_totals.csv | grep -E "$expr" | wc -l ; done
I get nothing. How do I solve this problem in an elegant manner?
there is a typo in
for i in {0..10}; do expr="NumMeds:\" $i"; cat medsCounts_totals.csv | grep -E "$expr" | wc -l ; done
it should be
"NumMeds\": $i"

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 grep for "+" or "-" values?

I need to grep for values with + or - symbol
eg: -3 or +3
I tried the following , doesnt help me
$ echo $accessTime | grep "^((\+)|(\-))[0-9]+$"
$ echo $accessTime | grep "+[0-9]+"
$ echo $accessTime | grep "\+[0-9]+"
$ echo $accessTime | grep "'+'[0-9]+"
$ echo $accessTime | grep "^'\+'[0-9]+"
$ echo $accessTime | grep "^(\+)[0-9]+"
$ echo $accessTime | grep "^(\+)[0-9]+"
Can you guys pls help me ....Im learning bash for past few days only..Thanks
Its easier than what you think, you should try
echo $accessTime | grep [+-]
You can use:
accessTime='+3'
echo "$accessTime" | grep "+[0-9]\+"
+3
Quantifier + needs to be escaped in normal grep and literal + must not be escaped.
With grep -E it is exactly reverse:
echo "$accessTime" | grep -E "\+[0-9]+"
+3
Quantifier + must not be escaped in grep -E (extended regex) and literal + . needs to be escaped.

Resources