comp.unix.shell FAQ - Answers to Frequently Asked Questions

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Joe Halp » Wed, 31 Mar 2004 05:12:58



The FAQ has been posted here, and is also available at
http://home.comcast.net/~j.p.h/

Joe

 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Joe Halp » Wed, 31 Mar 2004 05:12:57


Archive-name: unix-faq/shell/sh
Posting-Frequency: monthly
Version: $Id: cus-faq.html,v 1.4 2004/02/21 16:54:22 jhalpin Exp jhalpin $
Maintainer: Joe Halpin

This FAQ list contains the answers to some Frequently Asked Questions
often seen in comp.unix.shell. It spells "unix" in lower case letters
to avoid arguments about whether or not Linux, FreeBSD, etc are
unix. That's not the point of this FAQ, and I'm ignoring the issue.

This document as a whole is Copyright (c) 2003 Joe Halpin. It may be
copied freely. Exceptions are noted in individual answers.

Suggestions, complaints, et al, should be sent to the maintainer at
j....@comcast.net or posted to comp.unix.shell

There are two levels of questions about shells.

One is the use of the shell itself as an interface to the operating
system. For example, "how do I run a program in the background, and go
on with other things?". Or "how do I setup environmental variables
when I log in?".

The other level is how to write shell scripts. This often involves
having the shell execute unix utilities to perform part of the work
the shell script needs to accomplish, and requires knowledge of these
utilities, which isn't nominally in the scope of shell
programming. However, unless the question involves something other
than standard unix utilities, it should be included in this FAQ.

Standard unix utilities are defined by either POSIX or the Single Unix
Specification. These are now joined and are normally abbreviated as
"POSIX/SUS". This specification can be found at

http://www.opengroup.org/onlinepubs/007904975/toc.htm

The man pages found on that web page define standard behavior for any
given utility (or the shell itself). However, you should also check
the man page on your system for any utility or shell you need to
use. There isn't always a perfect correspondence between the standard
and a particular implementation (in fact, I'm not sure there's any
case in which they perfectly correspond).

There is also an Austin Group FAQ, which describes the standardization
effort in more detail ls
at

http://www.opengroup.org/austin/faq.html

Other good web sites that provide information about shells and shell
programming (including OS utilities) include:

http://www.shelldorado.com/
http://cfaj.freeshell.org/shell/
http://www.faqs.org/faqs/by-newsgroup/comp/comp.unix.shell.html

This FAQ is available at
http://home.comcast.net/~j.p.h/
http://www.newsville.com/cgi-bin/getfaq?file=comp.unix.shell/comp.uni...

---------------------------
The predictable legal stuff

---------------------------
The answers given in this FAQ list are provided with the best
intentions, but they may not be accurate for any particular
shell/os. They may be completely wrong for any shell/os. If you don't
test the answers, that's a bug in your procedures.

There are no guarantees for the answers or recommendations given in
this document. In fact, I don't even claim to have tested any or all
of them myself. Many of the answers here have been contributed by one
or more regular participants in the newsgroup, who I believe to be
competent (certainly more competent than I am), but THERE ARE NO
GUARANTEES.

Did I really need to make that all uppercase? Hopefully not, but there
are a lot of lawyers around with too much time on their hands, so I
want to make it clear that THERE ARE NO GUARANTEES
about the accuracy of answers in this FAQ list. This is, hopefully, an
aid to people trying to learn shell programming, but it is NOT a
supported product. You have to figure out for yourself whether or not
the answers here work for what you're trying to do.

Under no circumstances will the maintainer of this FAQ list, or any
contributors to it, be held liable for any mistakes in this
document. If the answers work for you, well and good. If not, please
tell me and I'll modify them appropriately so that this will be more
useful.

If you don't agree to that, don't read any farther than this. Reading
beyond this point indicates your agreement.

If you do test the answers and find a problem, please send email to
the maintainer (see above), so it can be corrected, or (preferably)
post a question to the newsgroup so it can be discussed and corrected
if there's a problem.

A number of people have contributed to this FAQ, knowingly or
unknowingly. Some of the answers were taken from previous postings in
the group, and other people contributed questions and answers
directly to the maintainer, which you are welcome to do as well.

Among the contributors is Heiner Steven, who also provided the
momentum to get this FAQ list started. He maintains a web site about
shell programming that has a lot of good stuff in it.

http://www.shelldorado.com/

======================================================================

CONTENTS:

0   COPYING

0a. Glossary
    Google
    POSIX/SUS ("the standard")
    UUOC
    dotfile
    portable
    race condition
    shebang
    shells
    top-posting

0b. Notes about using echo
1.  How can I send e-mails with attached files?
2.  How can I generate random numbers in shell scripts?
3.  How can I automatically transfer files using FTP with error checking?
4.  How can I remove whitespace characters within file names?
5.  How can I automate a telnet session?
6.  How do I do date arithmetic?
7.  Why did someone tell me to RTFM?
9.  How do I create a lock file?
10. How can I convert DOS text files to unix, and vice versa?
11. How can a shell prompt be set up to change the title of xterm?
12. How do I batch an FTP download/upload?
13. How do I get the exit code of cmd1 in cmd1|cmd2
14. Why do I get "script.sh: not found"
15. Why doesn't echo do what I want?
16. How do I loop through files with spaces in their name?
17. how do I change my login shell?
18. When should I use a shell instead of perl/python/ruby/tcl...
19. Why shouldn't I use csh?
20. How do I reverse a file?
21. How do I remove last n lines?
22. how do I get file size, or file modification time?
23. How do I get a process id given a process name? Or, how do I find out if a process is still running, given a process ID?
24. How do I get a script to update my current environment?
25. how do I rename *.foo to *.bar?
26. How do I use shell variables in awk scripts
27. How do I input the user with a timeout?
28. How do I get one character input from the user?
29. why isn't my .profile read?
30. why do I get "[5" not found in "[$1 -eq 2]"?
31. How do I exactly display the content of $var (with a \n appended).
32. How do I exactly display the content of $var (without a \n appended).
33. How do I split a pathname into the directory and file?
34. How do I make an alias take an argument?

Appendix A: Some example scripts
Appendix B: References. These correspond with numbers in square
            brackets (e.g. [1]) which may appear in the text.

======================================================================

ANSWERS

0  COPYING

   Some contributors may copyright their submissions and license them
   differently than this document.

   [1] Chris F.A. Johnson. Examples marked with COPYING[1] were
       contributed by Chris F.A. Johnson. He has copyrighted these
       examples, and licensed them under the GNU General Public
       License (GPL). Copying them directly into another script will
       cause that script to also come under the GPL. For details see

       http://www.fsf.org/licenses/licenses.html

0a.Glossary

   -------------------------------
   Google

      Google is one of the search engines on the Internet. It took
      over dejanews some years ago, and now is the standard reference
      when directing someone to a past thread one some topic. This is
      a very good place to start when researching a question about
      shell programming (and just about anything else).

        http://groups.google.com/advanced_group_search

   -------------------------------
   POSIX/SUS ("the standard")

      POSIX (Portable Operating System Interface) and SUS (Single Unix
      Specification) have been joined into one standard. This is what
      people usually mean when they refer to "the standard" in
      discussions about unix. When people in this group refer to the
      POSIX shell, they are talking about the shell prescribed by this
      specification. You can find this standard at

        http://www.opengroup.org/onlinepubs/007904975/toc.htm

   -------------------------------
   UUOC

      This is short for "Useless use of cat". It's used to point out
      that some example script has used cat when it could have used
      redirection instead. It's more efficient to redirect input than
      it is to spawn a process to run cat. For example

        $ cat file | tr -d 'xyz'

      runs two processes, one for cat and one for tr. This is less
      efficient than

        $ tr -d 'xyz' < file

      In general, "cat file | somecommand" can be more efficiently
      replaced by "somecommand < file"

      or (especially for multi-file input)

        $ somecommand file [file ...]

      but check the man page for "somecommand" to find out if it will
      accept this syntax.

      For more details about this, as well as other things like it, see
        http://rhols66.adsl.netsonic.fi/era/unix/award.html

   -------------------------------
   dotfile

      This refers to a file which starts with '.' (a dot). These files
      are not shown in directory listings without the -a (or -A in
      newer versions of ls - check the man page on your system) option
      to ls. Often they are configuration files, subdirectories used
      by applications to store configuration files, NFS swap files, et
      al.

   -------------------------------
   portable

      The word "portable" means different things to different people,
      in different situations, which is to say, there isn't one
      definition of "portable".

      At one extreme, a portable script is one which will
...

read more »

 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Faux_Pseud » Wed, 31 Mar 2004 08:28:44


_.-In comp.unix.shell, Joe Halpin wrote the following -._

Quote:>     Then just use the -f option on the ncftp program:
>     ncftpput -f /home/username/somefile somewebsite.com /pics *jpg

>     ncftp can be found at

I just noticed that this section (which I wrote) is missing its url.

It should be appended with:
<ulr:http://freshmeat.net/projects/ncftp/>

--
  .-'))  http://asciipr0n.com/fp   ('-.  | It's a damn poor mind that
 ' ..-  .:" )               ( ":.  -.. ' | can only think of one way to
  ((,,_;'.;'   UIN=66618055  ';. ';_,,)) | spell a word.
        ((_.YIM=Faux_Pseudo :._))        | - Andrew Jackson

 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by j.. » Wed, 31 Mar 2004 10:41:23



> _.-In comp.unix.shell, Joe Halpin wrote the following -._
> >     Then just use the -f option on the ncftp program:
> >     ncftpput -f /home/username/somefile somewebsite.com /pics *jpg

> >     ncftp can be found at

> I just noticed that this section (which I wrote) is missing its url.

> It should be appended with:
> <ulr:http://freshmeat.net/projects/ncftp/>

That's very odd. If you view the page source it's there. I've been
using Open Office to convert from HTML to plain text. I'll have to
look into this. Thanks for pointing it out.

Joe
--
If people don't want to come out to the ballpark, nobody's going
to stop them.
  - Yogi Berra

 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Joe Halp » Wed, 31 Mar 2004 11:12:38


The FAQ has been posted here, and is also available at
http://home.comcast.net/~j.p.h/

Joe

 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Joe Halp » Wed, 31 Mar 2004 11:12:38


Archive-name: unix-faq/shell/sh
Posting-Frequency: monthly
Version: $Id: cus-faq.html,v 1.4 2004/02/21 16:54:22 jhalpin Exp jhalpin $
Maintainer: Joe Halpin

This FAQ list contains the answers to some Frequently Asked Questions
often seen in comp.unix.shell. It spells "unix" in lower case letters
to avoid arguments about whether or not Linux, FreeBSD, etc are
unix. That's not the point of this FAQ, and I'm ignoring the issue.

This document as a whole is Copyright (c) 2003 Joe Halpin. It may be
copied freely. Exceptions are noted in individual answers.

Suggestions, complaints, et al, should be sent to the maintainer at
j....@comcast.net or posted to comp.unix.shell

There are two levels of questions about shells.

One is the use of the shell itself as an interface to the operating
system. For example, "how do I run a program in the background, and go
on with other things?". Or "how do I setup environmental variables
when I log in?".

The other level is how to write shell scripts. This often involves
having the shell execute unix utilities to perform part of the work
the shell script needs to accomplish, and requires knowledge of these
utilities, which isn't nominally in the scope of shell
programming. However, unless the question involves something other
than standard unix utilities, it should be included in this FAQ.

Standard unix utilities are defined by either POSIX or the Single Unix
Specification. These are now joined and are normally abbreviated as
"POSIX/SUS". This specification can be found at

http://www.opengroup.org/onlinepubs/007904975/toc.htm

The man pages found on that web page define standard behavior for any
given utility (or the shell itself). However, you should also check
the man page on your system for any utility or shell you need to
use. There isn't always a perfect correspondence between the standard
and a particular implementation (in fact, I'm not sure there's any
case in which they perfectly correspond).

There is also an Austin Group FAQ, which describes the standardization
effort in more detail ls
at

http://www.opengroup.org/austin/faq.html

Other good web sites that provide information about shells and shell
programming (including OS utilities) include:

http://www.shelldorado.com/
http://cfaj.freeshell.org/shell/
http://www.faqs.org/faqs/by-newsgroup/comp/comp.unix.shell.html

This FAQ is available at
http://home.comcast.net/~j.p.h/
http://www.newsville.com/cgi-bin/getfaq?file=comp.unix.shell/comp.uni...

---------------------------
The predictable legal stuff

---------------------------
The answers given in this FAQ list are provided with the best
intentions, but they may not be accurate for any particular
shell/os. They may be completely wrong for any shell/os. If you don't
test the answers, that's a bug in your procedures.

There are no guarantees for the answers or recommendations given in
this document. In fact, I don't even claim to have tested any or all
of them myself. Many of the answers here have been contributed by one
or more regular participants in the newsgroup, who I believe to be
competent (certainly more competent than I am), but THERE ARE NO
GUARANTEES.

Did I really need to make that all uppercase? Hopefully not, but there
are a lot of lawyers around with too much time on their hands, so I
want to make it clear that THERE ARE NO GUARANTEES
about the accuracy of answers in this FAQ list. This is, hopefully, an
aid to people trying to learn shell programming, but it is NOT a
supported product. You have to figure out for yourself whether or not
the answers here work for what you're trying to do.

Under no circumstances will the maintainer of this FAQ list, or any
contributors to it, be held liable for any mistakes in this
document. If the answers work for you, well and good. If not, please
tell me and I'll modify them appropriately so that this will be more
useful.

If you don't agree to that, don't read any farther than this. Reading
beyond this point indicates your agreement.

If you do test the answers and find a problem, please send email to
the maintainer (see above), so it can be corrected, or (preferably)
post a question to the newsgroup so it can be discussed and corrected
if there's a problem.

A number of people have contributed to this FAQ, knowingly or
unknowingly. Some of the answers were taken from previous postings in
the group, and other people contributed questions and answers
directly to the maintainer, which you are welcome to do as well.

Among the contributors is Heiner Steven, who also provided the
momentum to get this FAQ list started. He maintains a web site about
shell programming that has a lot of good stuff in it.

http://www.shelldorado.com/

======================================================================

CONTENTS:

0   COPYING

0a. Glossary
    Google
    POSIX/SUS ("the standard")
    UUOC
    dotfile
    portable
    race condition
    shebang
    shells
    top-posting

0b. Notes about using echo
1.  How can I send e-mails with attached files?
2.  How can I generate random numbers in shell scripts?
3.  How can I automatically transfer files using FTP with error checking?
4.  How can I remove whitespace characters within file names?
5.  How can I automate a telnet session?
6.  How do I do date arithmetic?
7.  Why did someone tell me to RTFM?
9.  How do I create a lock file?
10. How can I convert DOS text files to unix, and vice versa?
11. How can a shell prompt be set up to change the title of xterm?
12. How do I batch an FTP download/upload?
13. How do I get the exit code of cmd1 in cmd1|cmd2
14. Why do I get "script.sh: not found"
15. Why doesn't echo do what I want?
16. How do I loop through files with spaces in their name?
17. how do I change my login shell?
18. When should I use a shell instead of perl/python/ruby/tcl...
19. Why shouldn't I use csh?
20. How do I reverse a file?
21. How do I remove last n lines?
22. how do I get file size, or file modification time?
23. How do I get a process id given a process name? Or, how do I find out if a process is still running, given a process ID?
24. How do I get a script to update my current environment?
25. how do I rename *.foo to *.bar?
26. How do I use shell variables in awk scripts
27. How do I input the user with a timeout?
28. How do I get one character input from the user?
29. why isn't my .profile read?
30. why do I get "[5" not found in "[$1 -eq 2]"?
31. How do I exactly display the content of $var (with a \n appended).
32. How do I exactly display the content of $var (without a \n appended).
33. How do I split a pathname into the directory and file?
34. How do I make an alias take an argument?
35. How do I deal with a file whose name begins with a weird character

Appendix A: Some example scripts
Appendix B: References. These correspond with numbers in square
            brackets (e.g. [1]) which may appear in the text.

======================================================================

ANSWERS

0  COPYING

   Some contributors may copyright their submissions and license them
   differently than this document.

   [1] Chris F.A. Johnson. Examples marked with COPYING[1] were
       contributed by Chris F.A. Johnson. He has copyrighted these
       examples, and licensed them under the GNU General Public
       License (GPL). Copying them directly into another script will
       cause that script to also come under the GPL. For details see

       http://www.fsf.org/licenses/licenses.html

0a.Glossary

   -------------------------------
   Google

      Google is one of the search engines on the Internet. It took
      over dejanews some years ago, and now is the standard reference
      when directing someone to a past thread one some topic. This is
      a very good place to start when researching a question about
      shell programming (and just about anything else).

        http://groups.google.com/advanced_group_search

   -------------------------------
   POSIX/SUS ("the standard")

      POSIX (Portable Operating System Interface) and SUS (Single Unix
      Specification) have been joined into one standard. This is what
      people usually mean when they refer to "the standard" in
      discussions about unix. When people in this group refer to the
      POSIX shell, they are talking about the shell prescribed by this
      specification. You can find this standard at

        http://www.opengroup.org/onlinepubs/007904975/toc.htm

   -------------------------------
   UUOC

      This is short for "Useless use of cat". It's used to point out
      that some example script has used cat when it could have used
      redirection instead. It's more efficient to redirect input than
      it is to spawn a process to run cat. For example

        $ cat file | tr -d 'xyz'

      runs two processes, one for cat and one for tr. This is less
      efficient than

        $ tr -d 'xyz' < file

      In general, "cat file | somecommand" can be more efficiently
      replaced by "somecommand < file"

      or (especially for multi-file input)

        $ somecommand file [file ...]

      but check the man page for "somecommand" to find out if it will
      accept this syntax.

      For more details about this, as well as other things like it, see
        http://rhols66.adsl.netsonic.fi/era/unix/award.html

   -------------------------------
   dotfile

      This refers to a file which starts with '.' (a dot). These files
      are not shown in directory listings without the -a (or -A in
      newer versions of ls - check the man page on your system) option
      to ls. Often they are configuration files, subdirectories used
      by applications to store configuration files, NFS swap files, et
      al.

   -------------------------------
   portable

      The word "portable" means different things to different people,
      in different situations, which is to say, there isn't one
      definition of
...

read more »

 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by j.. » Wed, 31 Mar 2004 11:16:26



> _.-In comp.unix.shell, Joe Halpin wrote the following -._
> >     Then just use the -f option on the ncftp program:
> >     ncftpput -f /home/username/somefile somewebsite.com /pics *jpg

> >     ncftp can be found at

> I just noticed that this section (which I wrote) is missing its url.

> It should be appended with:
> <ulr:http://freshmeat.net/projects/ncftp/>

<sigh>

I maintain the FAQ in html format using emacs, and I fat-fingered
something along the way. I just reposted it, and it should be ok now.

Joe
--
If people don't want to come out to the ballpark, nobody's going
to stop them.
  - Yogi Berra

 
 
 

comp.unix.shell FAQ - Answers to Frequently Asked Questions

Post by Alan Conno » Wed, 31 Mar 2004 18:49:56


The FAQ is looking really sharp.

Thanks for the good work, Joe.

AC