This and That

Here are some short topics that won't fill their own Tipsheet.

How Do You Log Out of Unix

Instructions on logging out of Unix may seem like overkill, but there are some options to consider.

A simple logout is accomplished by typing

exit

not logoff, logout, or variations on "log". However, you also can logout by typing a control-d, which is the "end-of-file" character. This can cause problems for some people, especially if they are prone to making typing mistakes. One mistake that has been known to occur is for a user to exit Pico with the traditional control-x, but to overreach the "x" and also hit the "d". If your key-stroke does "xd" when you press and "dx" when you lift your hand, while you keep the control key down, it will take you out of Pico and log you out at the same time. I repeat, this has been known to happen. There is a simple fix, though. Put this line in your ".profile" file

set -o ignoreeof

You will never log out by accident again.

If you really prefer to type "logout" or something similar, you can set up an alias:

alias logout="exit"

See Tipsheet Vol. 1 No. 3 for information on where to set up an alias in your login sequence.

Users of the "csh" have the default action that a file called ".logout" is executed when they logout. The "ksh" and "sh" don't look for a ".logout" file but it is possible to make them look for one. Add this alias:

alias logout='. $HOME/.sh_logout; exit'

Now when you type "logout", the file ".sh_logout" will be run. You can add clean-up commands, or end-of-the-day commands if you like, to this file. The first clean-up task you should do is clear the screen to protect your privacy. Just put the word "clear" on a line by itself in the file. Another clean-up task you should do is to remove "core" files. These get "dumped" in your disk area when a program crashes. These can be helpful when you are debugging a program, but most of the time they just use up space. To clean up "core" files when you logout you might put this in the file:

find ~ -name core -exec rm {} \;

which will start in your home directory and search all subdirectories recursively for files named "core" and delete them. You can use a similar technique to clear out temporary files if you have given them a unique name, such as by using the suffix ".tmp". Try this

find ~ -name \*.tmp -exec rm -i {} \;

The "-i" switch is there to protect you in case you put a space after the "*". If you are fairly certain you are looking only for "*.tmp" and not '*", then you can remove the "-i".

It is not recommended that you alias "exit" to itself plus the .sh_logout command. There are times when "exit" needs to be run and not mean "logout", such as with the "script" command, discussed below.

Here is an example that comes from the book "Unix Power Tools." It will print out your "todo" list for the next day, called "todo.tomorrow", if you have written one in your home directory.

if [ -f $HOME/todo.tomorrow ] 
then 
echo "======Stuff To Do Tomorrow======" 
cat $HOME/todo.tomorrow 
fi. 

Note that there should be a space after "[" and before "]".

How to Append Files in Unix

Unix does not have an "append" command, much to some people's dismay. That's because the ability to append is hidden in the use of the "cat" command and output redirection. Want to append one file to another, putting the output in a third file? Do this:

cat file1 file2 > file3

This will take "file1" and "file2" and put them into "file3" in the order specified. If, instead, you want to append the files in place, such as putting file2 onto the end of file1 directly, you would type:

cat file2 >> file1

The redirection symbol ">>" is purposefully different from the first example. The double greater-than symbol means "append to the end," so file2 will be added to the end of file1. Now this raises an interesting question. What happens if you make a mistake and only type ">" instead of ">>"? If you haven't taken any precautions, then in our example, file2 would overwrite file1. To prevent such a mishap you should add the following to your ".profile" file:

set -o noclobber

There's No Temp Disk!

Well there is, sort of. Users are allowed to put files in the file system that starts with /tmp. When you put a file there you can continue to use it, you can edit it, and you can delete it. There is one caveat, however. This file system isn't really on a disk. It is in memory. If the machine crashes, all is lost. Still, it provides, fast, and almost vast, temporary storage space.

Keeping A Record of All You Do

There may be times when you want a record of all the commands you type, and all the responses the computer gives back to you. Unix provides a command for this, called "script". So you could type:

script instructions.for.my.student
cd work-dir
spss -m program.spssx > program.out &
lp -d p3900 program.out
exit

and everything that occurred between the lines "script . . ." and "exit" will show up in the file "instructions.for.my.student". You may substitute the word "professor" for "student", if you like.

 

© 1998, The University of Alabama. The information included here is for the University of Alabama central computing facility as it was configured on the document date. It may or may not apply to other Unix systems.