[comp.unix.wizards] Proposed monthly Frequently Axed Questions posting, round II

sahayman@iuvax.cs.indiana.edu (Steve Hayman) (06/08/89)

Thanks again to everyone who made suggestions about this document.  I
appreciate your input, both positive and negative, and will continue to
welcome suggestions or corrections.  I wish I could have incorporated
every suggestion, but somewhere you have to draw the line between not
enough and too much detail.

Just to make it clear, my main concern in posting this article is to
help people out, to get them answers without their having to post
articles.  I don't want anybody flaming anybody for not having read
this document, and I don't want some sort of rule (real or implied)
that you have to read this before posting a question.  With any luck,
people will scan this at least once and then later when they have a
question, even if they don't remember the answer, they'll think "Wasn't
there something about this in 'Frequently Asked Questions'?" and go and
check it out.

I plan to start posting this on July 1 and every first of the month
thereafter.  For now I would like to leave it as a single article
rather than splitting the Q/A and guidelines.  Would posting a 
separate "diff" when things change be useful?


Here's summary of the major differences between this posting and the one of
a week ago:

 - added some new frequently asked questions and answers

 	How do I rename "*.foo" to "*.bar"?
 	Why do I get [some strange error message] when I "rsh host command" ?
 	What does {awk,grep,fgrep,egrep,biff,cat} stand for?

	  (Note - should anything be added to that last list?
	   The meaning of "UNIX" is covered in the news.announce.newusers
	   article and there's probably too much overlap between this
	   and that already)

  
  - modified the suggestion that "most sites have at least a few Unix experts";
    "many" is probably more accurate.    (I got an interesting note from
    a fellow whose business is selling turnkey Xenix systems to churches and
    he says he really had to laugh at the remark about most sites having
    an expert.)
  
  - suggested that specific C questions should go to comp.lang.c

  - suggested need for an accurate "Subject: " line, e.g. not "Subject: Help"

  - deleted some well-intentioned suggestions in some of the answers in
    the interests of keeping this to a manageable size.  (For instance
    I cut down on the amount of stuff about "find" in the section
    on deleting oddly-named files, and although the suggestion to make
    sure your shell scripts can handle files named "?" or "embedded blank"
    is a good one, it is only one of several things you need to do
    in a good shell script, and that would really be a good subject
    for another article.)

  - Various changes to the frequently questioned answers.


Anyways, here's the latest version.  Comments still welcome, this is
probably the last revision I'll post before I start doing the
real thing.  Thanks for your support, and see you at Usenix.

..Steve

=== cut here ===
Subject: Welcome to comp.unix.questions and comp.unix.wizards  [Monthly posting]
Newsgroups: comp.unix.questions,comp.unix.wizards
Followup-To: comp.unix.questions


Comp.unix.questions and comp.unix.wizards are two of the most popular
and highest volume newsgroups on Usenet.  This article is a monthly
attempt to remind potential posters about what is appropriate for each
of the two newsgroups.  If you would like to make any suggestions about
the content of this article, please contact its maintainer at
sahayman@iuvax.cs.indiana.edu  or  iuvax!sahayman .

Later on in this article, you'll see the answers to some Frequently
Asked Questions.  Please don't ask these questions again, they've
been answered plenty of times already.  Thank you.  This article
includes answers to:


	How do I remove a file whose name begins with a "-" ?
	How do I remove a file with funny characters in the filename ?
	How do I get a recursive directory listing?
	How do I get the current directory into my prompt?
	How do I read characters from a terminal without requiring the user
	    to hit RETURN?
	How do I read characters from the terminal in a shell script?
	How do I check to see if there are characters to be read without
	    actually reading?
	How do I find the name of an open file?
	How do I rename "*.foo" to "*.bar"?
	Why do I get [some strange error message] when I "rsh host command" ?
	What does {awk,grep,fgrep,egrep,biff,cat} stand for?

Also, if you have not already read the overall Usenet introductory material
posted to "news.announce.newusers", please do.  Much of this article
overlaps with the common sense guidelines posted there.


	     Should I Post My Unix Question to the Net?

Often the answer is "No, you can get an answer a lot faster without
posting a question." Before you post, you should try -
    
    o Reading the manual for your system.  Some day you may encounter
      the phrase "RTFM", which stands for "Read the Fine Manual",
      (except 'F' doesn't really stand for "Fine").  If you ask
      someone a question and they tell you to RTFM, it's an
      indication that you haven't done your homework.   For instance,
      if you are having trouble removing a file whose name begins
      with a "-", check the man page for "rm".  It might tell
      you what you need to know.

      When people use terminology like "read(2)", they are referring
      to the "read" man page in section 2 of the manual (which you 
      would see by using "man 2 read").

    o Finding a knowledgeable user at your site.  Many sites have
      at least a few Unix experts who will be happy to help you
      figure out how to remove a file whose name begins with "-".
      Many larger sites, particularly universities, may even have
      paid consultants whose job is to help you with Unix problems.
      Check with them first.
    
    o Find a good introductory book on Unix.  There are plenty of
      such books available, and you will save yourself a lot
      of trouble by having one handy and consulting it frequently.

Please remember that comp.unix.questions and comp.unix.wizards are
read by over 50,000 people around the world, and that posting a question
to either of these groups will cost a lot of time and money by the
time your article is distributed to Japan, Australia, Norway, Israel,
and all corners of North America.


    Should I Post to "comp.unix.questions" or "comp.unix.wizards" ?

Comp.unix.wizards is intended for advanced discussion of Unix features - the
sort of topics the average user never thinks about.  Simple questions
about using normal commands should never go there.  Unfortunately, it's
often hard to tell whether your question is simple until you know
what the answer is.  A good rule of thumb is -

    Don't post to comp.unix.wizards unless *you* *yourself* are
    a Unix wizard.

Don't post to comp.unix.wizards just because you want to get the
attention of a unix wizard.  Many unix wizards read comp.unix.questions
also and will be happy to help you out if they see your question there.

Specific questions about the "C" language can go to comp.lang.c .

	      What Information Should I Include?

It's hard to include too much information.  There are hundreds of
different Unix systems out there, and they all have less in common
than you might think.  If you have a problem and are posting an
article, please be sure to mention:

    o A descriptive subject line.  Many people will decide whether
      to read your article solely on the basis of the subject line,
      so it should be a good statement of your problem.

      NOT GOOD                          GOOD

      "Help"                            "How do I sort a file by line length?"
      "Csh question"			"csh core dumps when I use '$<'"


    o What computer you are using, and what specific version
      of the operating system it uses.  For instance,

	    SunOS 4.0.1, Sun 3/50
	    4.3BSD-tahoe, Vax 11/780
	    SVR3.2, 3b2

    o If possible, the *exact* text of any error message you
      may have encountered.

      WRONG				RIGHT

      "I can't print this file"     "When I type 'lpr Filename', I get
				      lpr: Filename: File too ugly to print
				     What does this mean?  It isn't in
				     the man page.  This is using
				     Mueslix 9.3 on a Fax 68086502"

It's a good idea to post unrelated questions in separate articles,
so that people can keep different discussions separate.   It's also 
a *very* good idea to include a line or two like this:

    "Please mail your answers to me and I'll summarize what I get
     and post the results to comp.unix.questions."

This prevents many identical responses from different users to the
same question from clogging up the newsgroup.  And make sure
you really summarize what you get - don't just concatenate
all the mail you've received.

It's also a good idea to read comp.unix.questions for at least a couple
of weeks after you post your article to see what followup articles
are posted.


                Should I Post an Answer to a Question?

It's very tempting to post an answer to a question you read on the net,
especially when you think "Aha, finally - a question I can answer!"
Consider though that when a simple question is asked, such as the
sort about to be answered below, many other people around the
world already know the answer and may be posting their own reply.
In order to avoid dozens of replies to simple questions, please
wait a day or so and see if anyone else has already answered
the question.  If you have something special to contribute, please
do so, but make sure you're not duplicating something someone else has
already done.


                    What About Posting Source Code?

Posting small amounts of example code is fine (use comp.sources.unix to
distribute complete programs) - but please make sure that your code
runs (or at least compiles) properly.  Don't just type it in while
editing your posting and hope it will work, no matter how sure you are
that it will.  We all make mistakes.


                        What About Those People
       Who Continue to Ask Stupid or Frequently Asked Questions
                      In Spite of This Document?


Just send them a polite mail message, possibly referring them to this document.
There is no need to flame them on the net - it's busy enough as it is.



                      FREQUENTLY ASKED QUESTIONS

While these are all legitimate questions, they seem to crop up in
comp.unix.questions on an annual basis, usually followed by plenty
of replies (only some of which are correct) and then a period of
griping about how the same questions keep coming up.  You may also like
to read the monthly article "Answers to Frequently Asked Questions"
in the newsgroup "news.announce.newusers", which will tell you what
"UNIX" stands for.

With the variety of Unix systems in the world, it's hard to guarantee
that these answers will work everywhere.  Read your local manual pages
before trying anything suggested here.  If you have suggestions or
corrections for any of these answers, please send them to to
sahayman@iuvax.cs.indiana.edu or iuvax!sahayman.

1)  How do I remove a file whose name begins with a "-" ?

    Figure out some way to name the file so that it doesn't
    begin with a dash.  The simplest answer is to use   

	    rm ./-filename

    (assuming "-filename" is in the current directory, of course.)
    This method of avoiding the interpretation of the "-" works
    with other commands too.

    Many commands, particularly those that have been written to use
    the "getopt(3)" argument parsing routine, accept a "--" argument
    which means "this is the last option, anything after this is not
    an option", so your version of rm might handle "rm -- -filename".
    Some versions of rm that don't use getopt() treat a single "-"
    in the same way, so you can also try "rm - -filename".
    
2)  How do I remove a file with funny characters in the filename ?

    The  classic answers are

	rm -i some*pattern*that*matches*only*the*file*you*want

	which asks you whether you want to remove each file matching
	the indicated pattern;  depending on your shell, this may
	not work if the filename has a character with the 8th bit set
	(the shell may strip that off);
    
    and

	rm -ri .

	which asks you whether to remove each file in the directory,
	answer "y" to the problem file and "n" to everything else.,
	and which, unfortunately, doesn't work with many versions of rm;
	(always take a deep breath and think about what you're doing
	and double check what you typed when you use rm's "-r" flag)

    and

	find . -type f ... -ok rm '{}' \;

    where "..." is a group of predicates that uniquely identify the
    file.  One possibility is to figure out the inode number
    of the problem file (use "ls -i .") and then use

	find . -inum 12345 -ok rm '{}' \;
    
    or
	find . -inum 12345 -ok mv '{}' new-file-name \;
	
	
    "-ok" is a safety check - it will prompt you for confirmation of the
    command it's about to execute.  You can use "-exec" instead to avoid
    the prompting, if you want to live dangerously.

    If none of these work, find your system manager.

3)  How do I get a recursive directory listing?

    One of the following may do what you want:

	ls -R 			(not all versions of "ls" have -R)
	find . -print		(should work everywhere)
    
    If you're looking for a wildcard pattern that will match
    all ".c" files in this directory and below, you won't find one,
    but you can use

	% some-command `find . -name '*.c' -print`

    "find" is a powerful program.  Learn about it.

4)  How do I get the current directory into my prompt?

    It depends which shell you are using.  It's easy with some shells,
    hard or impossible with others.
	
    C Shell (csh):
	Put this in your .cshrc - customize the prompt variable
	the way you want.

	    alias cd 'chdir \!* && set prompt="${cwd}% "'
	
	If you use pushd and popd, you'll also need

	    alias pushd 'pushd \!* && set prompt="${cwd%} "'
	    alias popd  'popd  \!* && set prompt="${cwd%} "'

	Some C shells don't keep a $cwd variable - you can use
	`pwd` instead.

	If you just want the last component of the current directory
	in your prompt ("mail% " instead of "/usr/spool/mail% ")
	you can do
	    alias cd 'chdir \!* && set prompt="$cwd:t% "'



    Bourne Shell (sh):

	If you have a newer version of the Bourne Shell (SVR2 or newer)
	you can use a shell function to make your own command, "xcd" say:

	    xcd { cd $* ; PS1="`pwd` $ "; }

	If you have an older Bourne shell, it's complicated but not impossible.
	Here's one way.  Add this to your .profile file:

		LOGIN_SHELL=$$ export LOGIN_SHELL
		CMDFILE=/tmp/cd.$$ export CMDFILE
		PROMPTSIG=16 export PROMPTSIG
		trap '. $CMDFILE' $PROMPTSIG

	and then put this executable script (without the indentation!),
	let's call it "xcd", somewhere in your PATH 

		: xcd directory - change directory and set prompt
		: by signalling the login shell to read a command file
		cat >${CMDFILE?"not set"} <<EOF
		cd $1
		PS1="\`pwd\`$ "
		EOF
		kill -$PROMPTSIG ${LOGIN_SHELL?"not set"}

	Now change directories with "xcd /some/dir".


    Korn Shell (ksh):

	Put this in your .profile file:
		PS1='$PWD $ '
	
	If you just want the last component of the directory, use
		PS1='${PWD##*/} $ '


5)  How do I read characters from a terminal without requiring the user
    to hit RETURN?

	Check out cbreak mode in BSD, ~ICANON mode in SysV. 

	If you don't want to tackle setting the terminal parameters
	yourself (using the "ioctl(2)" system call) you can
	let the stty program do the work - but this is inefficient,
	and you should change the code to do it right some time:

	main()
	{
		int c;

		printf("Hit any character to continue\n");
		system("/bin/stty cbreak");
		c = getchar();
		system("/bin/stty -cbreak");
		printf("Thank you for typing %c.\n", c);

		exit(0);
	}


6)  How do I read characters from the terminal in a shell script?

	In sh, use read.  It is most common to use a loop like

		while read line
		do
			...
		done

	In csh, use $<.

7)  How do I check to see if there are characters to be read without
    actually reading?

	Certain versions of UNIX provide ways to check whether characters
	are currently available to be read from a file descriptor.  In BSD,
	you can use select(2).  You can also use the FIONREAD ioctl (see
	tty(4)), which returns the number of characters waiting to be read,
	but only works on terminals and pipes.  In System V Release 3, you
	can use poll(2), but that only works on streams.

	There is no way to check whether characters are available to be
	read from a FILE pointer.  (Well, there is no *good* way.  You could
	poke around inside stdio data structures to see if the input buffer
	is nonempty but this is a bad idea, forget about it.)

	Sometimes people ask this question with the intention of writing
		if (characters available from fd)
			read(fd, buf, sizeof buf);
	in order to get the effect of a nonblocking read.  This is not the
	best way to do this, because it is possible that characters will
	be available when you test for availability, but will no longer
	be available when you call read.  Instead, set the O_NDELAY flag
	(which is also called FNDELAY under BSD) using the F_SETFL option
	of fcntl(2).  Older systems (Version 7, 4.1 BSD) don't have O_NDELAY;
	on these systems the closest you can get to a nonblocking read is
	to use alarm(2) to time out the read.


8)  How do I find the name of an open file?

	In general, this is too difficult.  The file descriptor may
	be attached to a pipe or pty, in which case it has no name.
	It may be attached to a file that has been removed.  It may
	have multiple names, due to either hard or symbolic links.

	If you really need to do this, and be sure you think long
	and hard about it and have decided that you have no choice,
	you can use find with the -inum and possibly -xdev option,
	or you can use ncheck, or you can recreate the functionality
	of one of these within your program.  Just realize that
	searching a 600 megabyte filesystem for a file that may not
	even exist is going to take some time.


9) How do I rename "*.foo" to "*.bar"?
	
	"mv *.foo *.bar" doesn't work, because of the way wildcards are
	expanded.  Depending on your shell, you can do it with a loop
	like this if you have "basename":

	C Shell:
	    foreach f ( *.foo )
		set base=`basename $f .foo`
		mv $f $base.bar
	    end

	Bourne Shell:
	    for f in *.foo; do
		base=`basename $f .foo`
		mv $f $base.bar
	    done
	
	If you don't have "basename" or want to do something like
	renaming foo.* to bar.*, you can use something like "sed" to
	strip apart the original file name in other ways, but
	the general looping idea is the same. 


10) Why do I get [some strange error message] when I "rsh host command" ?

    If your remote account uses the C shell, the remote host will
    fire up a C shell to execute 'command' for you, and that shell
    will read your remote .cshrc file.  Perhaps your .cshrc contains
    a "stty", "biff" or some other command that isn't appropriate
    for a non-interactive shell.  The unexpected output or error
    message from these commands can screw up your rsh in odd ways.

    Fortunately, the fix is simple.  There are, quite possibly, a whole
    *bunch* of operations in your ".cshrc" (e.g., "set history=N") that are
    simply not worth doing except in in interactive shells.  What you do is
    surround them in your ".cshrc" with:

	    if ( $?prompt ) then
		    operations....
	    endif

    and, since in a non-interactive shell "prompt" won't be set, the
    operations in question will only be done in interactive shells.

    You may also wish to move some commands to your .login file; if
    those commands only need to be done when a login session starts up
    (checking for new mail, unread news and so on) it's better
    to have them in the .login file.

11) What does {awk,grep,fgrep,egrep,biff,cat} stand for?

    grep == "Global Regular Expression Print"

	grep comes from the ed command to print all lines matching a
	certain pattern

		    g/re/p

	where "re" is a "regular expression".
    
    fgrep = "Fixed Grep".

	fgrep searches for fixed strings only.  The "f" does not
	stand for "fast".

    egrep = "Extended Grep"

	egrep uses fancier regular expressions than grep.

    awk = "Aho Weinberger and Kernighan"

	This language was named by its authors, Al Aho, Peter Weinberger and
	Brian Kernighan.

    cat = "catenate"

	catenate is an obscure word meaning "to connect in a series".

    biff = "biff"

	This command, which turns on asynchronous mail notification,
	was allegedly named after someone's dog that barked whenever
	the postman arrived.  Or so the story goes.
    

-- 
Steve Hayman    Workstation Manager    Computer Science Department   Indiana U.
sahayman@iuvax.cs.indiana.edu     iuvax!sahayman                 (812) 855-6984