RHEL:bashスクリプトに構文エラーがあります。

RHEL:bashスクリプトに構文エラーがあります。

私の使命は、mysqlログインを監査できるbashスクリプトを書くことです。でオプションを

有効にすると、mysqlが実行するすべてのアクティビティを登録して作成できます(私の場合)。これにより、次の行が表示されます。general_log/etc/my.cnf/var/lib/mysql/localhost.log
cat

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

だからこれを念頭に置いて、次のgrep構文を作成しました(変数を実際の数値に変更すると機能しました!)。

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

また、これらのログイン数を計算するためにgrepコマンドを作成しました。

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

すべてが機能し、私が知っている限り、技術的に正確です(スペルチェックされています!)。

ただし、ユーザーから変数を取得して正しいスクリプトに配置できるようにスクリプトとして書き込む必要がありますが、これまでは機能させることはできません。bash読み取ろうとしたときにエラーが発生しました。以下の最初の変数を参照してください。

これまでのコード:

#!/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

発生したエラー(原本:下図1):

[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 ~]#

編集する

新しいコード:

#!/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

発生したエラー(原本:下図2):

[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 ~]#

エラー1 - 画像: ここに画像の説明を入力してください。

間違い2 - 図: ここに画像の説明を入力してください。

ベストアンサー1

これは答えではありませんが、いくつかのコードレビューです。日付入力を改善するためにこれを提案します。ユーザーはすぐに日付を入力し、コマンドを使用してdateユーザー入力を検証して正規化します。

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 ...

おすすめ記事