Skip to content
Advertisement

Writing Bash Script to Log Out if Incorrect upon Login. Need guidance

I am trying to write a Bash Script that will log the user out if they cannot get the security question.

I am needing to find either an array or multiple if conditions to reflect this. I know there are a lot of sub-stuff such as stopping any override input(CTRL+Z, etc).

Below is the script I have so far, and I am getting the error below. I am not wanting the answer, just direction of what to look for. It doesn’t even prompt the security question, just goes straight to logging the root user out in this case.

./txtscript: line 13: [: =: unary operator expected

#!/bin/bash

echo "What is your name?"
read ISNAME
if [ $ISNAME = "root" ];
then
    echo "Nice to meet you, root."
    echo "Login as root" | mail -s "Login for root" root
    echo "Next for the Security Question! "  
fi

echo "What is the codename?"
if [ $codename = "Cole Server" ];
then
    echo "Hello, Welcome"
else
    echo "Away from me, Not Root"
    echo "Unauthorized" | mail -s "INTRUDER ALERT" root
    pkill -KILL -u $ISNAME;
fi

*****UPDATE*******

Alright, So I got that part fixed. Now the script has the following.

echo “What is the codename?” read CODEAME if [ $CODENAME = “Cole Server” ]; then

When I run the script, I get the error “./txtscript: line 14: [: =: unary operator expected”

Line 14 is: if [ $CODENAME = “Cole Server” ];

In general, I have not even found a good website/article that really explains the unary operator expected error, so I have no idea what it means. Any assistance is greatly appreciated.

Advertisement

Answer

Well, the problem is that $codename is not filled in.

So line 13 is running:

if [ = "Cole Server" ];  # note: the $codename was expanded to emptiness

What you want to write in your script is:

if [ "$codename" = "Cole Server"];

This will evaluate as

if [ "" = "Cole Server"];

Which is at least syntactically valid. You still need to actually assign to codename somewhere, which you’re currently not doing.

However, I think this whole approach is probably wrong-headed. You are giving them an interactive bash session, and there are all sorts of things they can do there – like press Ctrl+Z to suspend it.

This is fine as a fun little game, but if you are trying to do serious security, I worry (:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement