I went about making a small script that requires an extra passcode when logging into my account on the system.
#!/bin/bash
clear
unset password
prompt="Enter Passcode: "
while IFS= read -p "$prompt" -r -s- n 1 char
do
if [[ $char == $'\0' ]]
then
break
fi
prompt='*'
password+="$char"
done
if [ $password -ne "####" ]
then
echo
echo "Logged out"
echo
read -n 1 -s -p "Press any key to continue"
clear
else
echo
echo "Login successful"
echo
read -n 1 -s -p "Press any key to continue"
clear
fi
This first segment of the code prompts the user for a passcode, which they will enter. As the passcode is entered, an asterisk (*) will be placed instead of whatever character was entered.
Enter Passcode: ****
Depending on whether the user entered the correct passcode, the script is supposed to do one of two things. Either notify the user they entered the correct passcode and close the script, continuing with the login, or notify the user they got the passcode wrong and force a logout from the system.
I have everything working except for the logout. I have attempted to use the logout and exit command, by themselves or in some combination. The best option that I could find was very unclean.
pkill -KILL -u user.name
Is there any other way that I could get my shell script to log out from the terminal?