Shell How to execute logout in a script

Hello,

I want to write a logout script, but when it executes, an error is reported:
logout: not login shell: use `exit'



Here is my script

Code:
#!/usr/bin/bash

do_logout() {
  echo "y. Logout"
  echo "n. Back to Previous Menu"
  echo -n "Enter option [ y/n ]: "
  read input
  confirm=$(echo $input | tr '[A-Z]' '[a-z]')
  if [ y == $confirm ]; then
    logout
  elif [ n == $confirm ]; then
    menu
  else
    do_logout
  fi
}
 
You can't. logout isn't even POSIX btw. When your login shell exits, the logout is implicit. For bash's logout builtin, as far as I understand, the only difference to the standard exit is that it checks whether it IS the login shell and refuses to exit otherwise.

If you want to end a session from a script, you must make sure this script is run by the login shell. When you manually run it, you can achieve that with the . builtin, which will source the script and run it in your current shell instead of launching a new shell as given in the shebang.
 
Addendum: This does look like you want the whole session for some specific user to be "driven" by a script you write. You can certainly do that, but then, check what kind of "startup file" the shell you intend to use executes and put your complete script there.
 
fstat /dev/tty|tail +2|cut -f 3 -w|sort -urn|xargs kill -1 2>/dev/null
this well send SIGHUP to all processes at your current tty
wont work in xterm (most likely) -- it wont end your xdm session
 
covacat I would like to discourage such hacks. As you already mentioned, they're not reliable, especially if the session in question is an X session. Instead, I would like to see the actual requirements first -- so far, it seems like a wish to "script" a whole "console session", which could be done in a sane way.

edit, addendum: this shell snippet would already break as soon as PIDs are truely randomized ... fstat can't tell you the session leader, all it can tell you is the processes that currently have the controlling tty opened. So, again: don't do this.
 
well it signals all of them. i sorted the in them hope to reduce "No such pid" messages but in the end just sent stderr to null
it still sucks (looks like even mc creates its own pty so it wont work from mc)
 
Several others already said: You need to explain why you want to create a logout script. What is the use case? Have you considered alternative ways of accomplishing the same thing? Some shells have automated things that can be executed on logout.

I want to write a logout script, but when it executes, an error is reported:

I think you need to first learn about how Unix and shells work. When you run a command from the shell prompt, it is usually executed in a separate process. If your command is a shell script, the way it works is that a new shell is started, that shell executes your script, and when the script finishes, that shell exits. Which also implies that if you write "logout" or "exit" into a shell script, it will exit the sub-shell, not the one that you are logged in as.

There are a few exceptions to this theory that "commands are executed by starting a new executable or sub-shell", and those are called built-in commands. In that category are things like exit (duh), logout, and a whole lot of others. If you read the documentation for your shell, the built-in commands will be explained. The fact that some commands run in the context of the shell, but most are in a separate process explains why certain shell functions are hard to replace by scripts; that in particular includes setting shell variable or environment variables, changing the working directories, and logging out.

So please tell us: Why and what do you want to do.
 
Back
Top