-1

I was tasked to do a bash script that can audit logins in mysql.

If you activate the general_log option in /etc/my.cnf, you can register all the activity that mysql does and it get write (in my case) in /var/lib/mysql/localhost.log.
If i cat it, i get this line:

2018-11-18T12:39:46.622298Z     5 Connect   Access denied for user 'root'@'localhost' (using password: YES)

So, with that in mind, I made this grep syntax (change the variables to actual numbers and it works!):

grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied

I also made a grep instruction that counts how many of these logins are made.

grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied

They all work and, AFAIK they are technically correct (spellchecked!).

But I need to bake them into a script so I can take variables from the user and put them into a proper script, but I can't make them work so far: I'm getting the error you can see below when bash tries to read the first variable.

The code so far:

#!/bin/bash
echo "Year input: "
read -r year
    if [[ $year -gt 2020 ]];
    then
        echo "Incorrect year input."
        exit 1
    else
        echo "Month input: "
        read -r month
            if [[ $month -gt 12 ]];
            then
                echo "Incorrect month input."
                exit 1
            else
                echo "Day input: "
                read -r day
                    if [[ $day -gt 31 ]];
                    then
                        echo "Incorrect day input."
                        exit 1
                    else
                        grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
                        logbien=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
                        echo "Correct logins: "echo "$logbien" | wc -1 " times."
                        echo ''
                        grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied
                        logmal=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
                        echo "Incorrect logins: "echo "$logmal" | wc -1 " times."
                    fi
            fi
    fi
exit 0

The error I get (original: image 1 below):

[root@localhost ~]# sh /medla/sf_compartida/two.sh
Year input:
2018
': not a valid identifiersh: line 3: read: `year
/media/sf_compartida/two.s: line 34: syntax error: unexpected end of file
[root@localhost ~]#

Edit

New code:

#!/bin/bash
read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d")
    then
        echo "Error: invalid date" >&2
        exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]
    then 
        echo "invalid year" >&2
        exit 1
        else
        grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
        logbien=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
        echo "Correct logins: "echo "$logbien" | wc -1" times."
        echo ''
        grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep denied
        logmal=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
        echo "Incorrect logins: "echo "$logmal" | wc -1" times."
fi

The error I get (original: image 2 below):

[root@localhost ~]# sh /media/sf_compartida/two2.sh
Enter the date (YYYY-mm-dd): 2018-11-20
': not a valid identifier.sh: line 2: read: `date
/media/sf compartida/two2.sh: line 9: syntax error in conditional expression
'media/sf_compartida/two2.sh: line 9: syntax error near `]]
'media/sf_compartida/two2.sh: line 9: `if [[ $year -gt 2020 ]]
[root@localhost ~]#

Error 1 - image: enter image description here

Error 2 - image: enter image description here

  • 3
    shellcheck.net is a good resource for syntax checking shell scripts. You don't have any glaring errors here: you could improve your quoting and command substitution (as pointed out by shellcheck.net). The main problem with the output is that logbien and logmal contain the grep output which is newline-separated lines. If you want the number of good logins, you need to count those lines: n_bien=$(echo "$logbien" | wc -l) – glenn jackman Nov 25 '18 at 13:58
  • You may also want to run your script as (export LC_MESSAGES=C; ./your_script) to report the actual error messages in English. – fra-san Nov 25 '18 at 14:06
  • I hope you don't have a user called "denied" :-) – Kusalananda Nov 25 '18 at 14:15
  • 1
    The : not an identifier lines are the key. You have a carriage return character at the end of one or more of your read lines. Get rid of them in an editor. They might be displayed as ^M. – Mark Plotnick Nov 25 '18 at 21:11
  • 1
    Looks like you definitely have CR (\r) in there. Remove that semicolon, then run dos2unix < two.sh > twofixed.sh and see if that new script still has those problems. – Mark Plotnick Nov 25 '18 at 23:46
1

This is not an answer, but a bit of code review: I would offer this to improve your date entry: get the user to enter the date at once, and use the date command to validate and normalize the user's input.

read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d"); then
    echo "Error: invalid date" >&2
    exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]; then echo "invalid year" >&2; exit 1; fi

# then: grep "$date" logfile ...
  • i appreciate that revision! it looks much more cleaner than my initial solution, no doubt. But i still get a lot of error messages. I used the shellcheck and it found no errors whatsoever. I'll add the new code in the post. Thanks! – jfalava Nov 25 '18 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.