home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / shell / 3891 < prev    next >
Encoding:
Internet Message Format  |  1992-09-10  |  2.0 KB

  1. Path: sparky!uunet!gossip.pyramid.com!pyramid!oracle!unrepliable!bounce
  2. From: plobo@dvlseq.us.oracle.com (Patrick Lobo)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: How to do $#a in Bourne shell?
  5. Message-ID: <1992Sep10.190928.7699@oracle.us.oracle.com>
  6. Date: 10 Sep 92 19:09:28 GMT
  7. References: <peter.716085132@merlin>
  8. Sender: usenet@oracle.us.oracle.com (Oracle News Poster)
  9. Organization: Oracle World HQ, Redwood Shores, California
  10. Lines: 54
  11. Nntp-Posting-Host: dvlseq
  12. X-Disclaimer: This message was written by an unauthenticated user
  13.               at Oracle Corporation.  The opinions expressed are those
  14.               of the user and not necessarily those of Oracle.
  15.  
  16. In article <peter.716085132@merlin> peter@merlin.acadiau.ca (Peter Steele) writes:
  17. >If I have a list in Bourne shell, how can I determine how many
  18. >elements are in, short of write a for loop and counting them?
  19. >Bourne shell doesn't seem to have C shell's $#variable construct.
  20. >It doesn't seem to support array indexing either, although I'm just
  21. >learning it and I might be missing something....
  22. >--
  23.  
  24.  
  25. Here is a Bourne Shell script that attempts to explain how to do this:
  26. Basically, you use the set command.
  27.  
  28.  
  29. ========================= cut here =====================
  30. #!/bin/sh
  31.  
  32. echo
  33. echo "Initially, this is the scenario"
  34.  
  35. echo "These are the command line arguments: $@"
  36. echo "This is the number of command line arguments: $#"
  37.  
  38. echo 
  39. echo "------------------------"
  40. echo 
  41.  
  42. echo "FIRST SAVE THE COMMAND LINE ARGUMENTS"
  43. args=$@
  44.  
  45.  
  46. echo "THEN COUNT THE ELEMENTS IN THE LIST"
  47.  
  48. list="a b c d e f g h i j k l m n"
  49. set $list                      # list now replaces the command line args
  50. echo "This is the list: $*"
  51. list_len=$#
  52. echo "This is the length of the list $list_len"
  53.  
  54.  
  55. echo 
  56. echo "------------------------"
  57. echo 
  58.  
  59. echo "FINALLY, RESTORE THE COMMAND LINE ARGUMENTS"
  60.  
  61. set $args
  62.  
  63. echo "These are the original command line arguments: $@"
  64. echo "This is the original number of command line arguments: $#"
  65.  
  66. ========================= end of script =====================
  67.  
  68.  
  69. Patrick Lobo
  70.