Score:0

trying to see if how many times service Accounts was logged in

pf flag
NJN

Hi I am new to shell scripting ,I am trying to see how many times an accounts were logged in the log file. If an account was logged in more than 20 time I need the name of those accounts to be printed.

I was trying something on the below line

#!/bin/bash
a = 20
cat mongod.log* | grep sa_mg | wc -l
if [sa_mg <= a]
then
Score:1
uz flag
cat mongod.log* | grep sa_mg | wc -l

should be the same as

grep -c sa_mg mongod.log*

You should assign the return value to a (new) variable like this:

b=$(grep -c sa_mg mongod.log*)

Then you can compare that value (check it with echo $b) with your variable a.

The complete script would look like (also correcting the test):

#!/bin/bash
a = 20
b = $(grep -c sa_mg mongod.log*)
if [ $b -le a ]
then
  echo There are less then $a line matching
fi
Score:0
in flag

A very common pattern for novices that is considered "incorrect" is

cat "$filename" |  grep "$some_string"

because you don't need a pipe | when you use:

grep  "$some_string" "$filename"

Slightly more advanced and performant is to use some of the switches in the GNU version of grep:

man grep

  -c, --count
          Suppress normal output; instead print a count of matching
          lines for each input file. 

That immediately and natively gives the the same count of matching lines as piping your grep "$some_string" output to | wc -l

When you only want to know if a string occurs 20 times or more in the file: you can stop processing the file contents after grep has found 20 matching lines which is really useful in a file many 100's of GB's or more that may contain a great many occurrences of "$some_string".

man grep

-m NUM, --max-count=NUM
          Stop reading a file after NUM matching lines.  If NUM is
          zero, grep stops right away without reading input.  A NUM
          of -1 is treated as infinity and grep does not stop; this
          is the default.  If the input is standard input from a
          regular file, and NUM matching lines are output, grep
          ensures that the standard input is positioned to just
          after the last matching line before exiting, regardless of
          the presence of trailing context lines.  This enables a
          calling process to resume a search.  When grep stops after
          NUM matching lines, it outputs any trailing context lines.
          When the -c or --count option is also used, grep does not
          output a count greater than NUM.  

And that then comes together as (untested and unrefined)

/bin/bash 

some_string="sa_mg"
filename="mongod.log*"

count=$(grep -c -m 20 "$some_string" "$filename")

if [ "$count" -ge "20" ] ; then 
  echo "found 20 or more occurrences"
fi

Linting you shell script code with for example https://www.shellcheck.net/ (or the locally installed linter) will help you write "better" scripts.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.