Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Log out from terminal OR logout from system ? Which distribution you are using ? – SHW Nov 23 '16 at 7:33
1  
How are you invoking this script? From ~/.bash_profile? You might be able to just kill $PPID. – icarus Nov 23 '16 at 7:36
    
What was the problem with the logout command? – Tobias Nov 23 '16 at 9:40
    
I want it to log out of the system. kill $PPID did not work. Sorry about the late reply guys. – Alex Graalum Nov 29 '16 at 22:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.