Answer the question
In order to leave comments, you need to log in
How to make a condition in a condition in bash?
Good afternoon. The second day I'm digging with the script, I can't figure out how to make a condition in the condition.
There is a log file in which user actions are logged, bots enter there, the bot performs a sequence of tasks in one second, that is, log in, change the password, log out.
I'm trying to write a script that will define a bot, but I ran into such a problem, how to enter a condition in a condition, so you can't do it in the bash? If so, can you advise something on how to implement such a script?
Here's what I did:
#!/bin/bash
read -p "Please enter filename: " filename;
count=`cat $filename | awk {'print $6'} | cut -c 21- | rev | cut -c 2- | rev | uniq | wc -l`
allusers=`cat $filename | awk {'print $6'} | cut -c 21- | rev | cut -c 2- | rev | uniq`
action=`cat $filename | awk {'print $8,$9,$10'} | cut -c 2- | rev | cut -c 2- | rev`
#countact=`cat $filename | grep "$username" |awk {'print $6'} | cut -c 21- | rev | cut -c 2- | rev | wc -l`
for ((i=1; i<="$count" ; i++));
do
username=`cat $filename | awk {'print $6'} | cut -c 21- | rev | cut -c 2- | rev | uniq | sed -n "$i"p`
firstop=`cat $filename| grep "$username" | awk '{print $5}' | head -n1 | cut -c 7-8`
lastop=`cat $filename | grep "$username" | awk '{print $5}' | tail -1 | cut -c 7-8`
countact=`cat $filename | grep "$username" |awk {'print $6'} | cut -c 21- | rev | cut -c 2- | rev | wc -l`
#####ACTIONS
action1=`cat $filename | grep "$username" |awk {'print $8,$9,$10'} | cut -c 2- | rev | cut -c 2- | rev | sed -n 1p`
action2=`cat $filename | grep "$username" |awk {'print $8,$9,$10'} | cut -c 2- | rev | cut -c 2- | rev | sed -n 2p`
action3=`cat $filename | grep "$username" |awk {'print $8,$9,$10'} | cut -c 2- | rev | cut -c 2- | rev | sed -n 3p`
act1="user logged in"
act2="user changed password"
act3="user logged off"
if [ $countact = 3 ]; then
if [ $firstop = $lastop ]; then
if [ $action1 = $act1 ]; then
if [ $action2 = $act2 ]; then
if [ $action3 = $act3 ]; then
echo $username "- BOT"
fi
fi
fi
fi
else
echo $username "- NOT BOT"
fi
done
Mon, 22 Aug 2016 13:15:39 +0200|178.57.66.225|fxsciaqulmlk| - |user logged in| -
Mon, 22 Aug 2016 13:15:39 +0200|178.57.66.225|fxsciaqulmlk| - |user changed password| -
Mon, 22 Aug 2016 13:15:39 +0200|178.57.66.225|fxsciaqulmlk| - |user logged off| -
Mon, 22 Aug 2016 13:15:42 +0200|178.57.66.225|faaaaaa11111| - |user logged in| -
Mon, 22 Aug 2016 13:15:40 +0200|178.57.66.215|terdsfsdfsdf| - |user logged in| -
Mon, 22 Aug 2016 13:15:49 +0200|178.57.66.215|terdsfsdfsdf| - |user changed password| -
Mon, 22 Aug 2016 13:15:49 +0200|178.57.66.215|terdsfsdfsdf| - |user logged off| -
Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user logged in| -
Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user logged in| -
Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user changed password| -
Mon, 22 Aug 2016 13:15:59 +0200|178.57.66.205|erdsfsdfsdf| - |user logged off| -
Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user logged in| -
Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user changed password| -
Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user changed profile| -
Mon, 22 Aug 2016 13:17:50 +0200|178.57.66.205|abcbbabab| - |user logged off| -
Mon, 22 Aug 2016 13:19:19 +0200|178.56.66.225|fxsciaqulmla| - |user logged in| -
Mon, 22 Aug 2016 13:19:19 +0200|178.56.66.225|fxsciaqulmla| - |user changed password| -
Mon, 22 Aug 2016 13:19:19 +0200|178.56.66.225|fxsciaqulmla| - |user logged off| -
Mon, 22 Aug 2016 13:20:42 +0200|178.57.67.225|faaaa0a11111| - |user logged in| -
Answer the question
In order to leave comments, you need to log in
square brackets in bash is not language syntax, it is a specific test command
i.e.
"[ $a==$b ]" is the same as "test $a==$b"
And when you write [ $a==$b && $ c==$d ], bash parses as "test $a==$b && $c==$d", which is a syntax error.
Close brackets or use test command options
1) "if [ $a==$b ] && [ $c==$d ]; then"
2) "if [ $a==$b -a $c==$ d]; then"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question