home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gossip.pyramid.com!pyramid!oracle!unrepliable!bounce
- From: plobo@dvlseq.us.oracle.com (Patrick Lobo)
- Newsgroups: comp.unix.shell
- Subject: Re: How to do $#a in Bourne shell?
- Message-ID: <1992Sep10.190928.7699@oracle.us.oracle.com>
- Date: 10 Sep 92 19:09:28 GMT
- References: <peter.716085132@merlin>
- Sender: usenet@oracle.us.oracle.com (Oracle News Poster)
- Organization: Oracle World HQ, Redwood Shores, California
- Lines: 54
- Nntp-Posting-Host: dvlseq
- X-Disclaimer: This message was written by an unauthenticated user
- at Oracle Corporation. The opinions expressed are those
- of the user and not necessarily those of Oracle.
-
- In article <peter.716085132@merlin> peter@merlin.acadiau.ca (Peter Steele) writes:
- >If I have a list in Bourne shell, how can I determine how many
- >elements are in, short of write a for loop and counting them?
- >Bourne shell doesn't seem to have C shell's $#variable construct.
- >It doesn't seem to support array indexing either, although I'm just
- >learning it and I might be missing something....
- >--
-
-
- Here is a Bourne Shell script that attempts to explain how to do this:
- Basically, you use the set command.
-
-
- ========================= cut here =====================
- #!/bin/sh
-
- echo
- echo "Initially, this is the scenario"
-
- echo "These are the command line arguments: $@"
- echo "This is the number of command line arguments: $#"
-
- echo
- echo "------------------------"
- echo
-
- echo "FIRST SAVE THE COMMAND LINE ARGUMENTS"
- args=$@
-
-
- echo "THEN COUNT THE ELEMENTS IN THE LIST"
-
- list="a b c d e f g h i j k l m n"
- set $list # list now replaces the command line args
- echo "This is the list: $*"
- list_len=$#
- echo "This is the length of the list $list_len"
-
-
- echo
- echo "------------------------"
- echo
-
- echo "FINALLY, RESTORE THE COMMAND LINE ARGUMENTS"
-
- set $args
-
- echo "These are the original command line arguments: $@"
- echo "This is the original number of command line arguments: $#"
-
- ========================= end of script =====================
-
-
- Patrick Lobo
-