home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3738 < prev    next >
Encoding:
Text File  |  1992-09-01  |  1.5 KB  |  65 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!caen!malgudi.oar.net!chemabs!lwv26
  3. From: lwv26@cas.org (Larry W. Virden)
  4. Subject: ksh: how to properly parse through arguments?
  5. Message-ID: <1992Sep1.194057.13966@cas.org>
  6. Followup-To: comp.unix.shell
  7. Sender: usenet@cas.org
  8. Reply-To: lvirden@cas.org (Larry W. Virden)
  9. Organization: Nedriv Software and Shoe Shiners, Uninc.
  10. References: <1992Aug28.051548.16935@ddsw1.mcs.com> <ahn.714982174@ac> <1992Sep1.190410.17063@prism.poly.edu>
  11. Date: Tue, 1 Sep 1992 19:36:05 GMT
  12. Lines: 51
  13.  
  14.  
  15. Okay.  I have some code which has to read thru its arguments.  These arguments
  16. may have quotes, definitely have spaces, etc.
  17.  
  18. I have to look thru them, acting on certain flags and then pass the rest
  19. off to another program.
  20.  
  21. I have code like:
  22.  
  23.  
  24. arg=""
  25. while [ "$#" -gt 0 ] ; do
  26.     case "$1" in
  27.  
  28.         -Q )
  29.             echo "time to go fishing"
  30.             shift
  31.             ;;
  32.         -X )
  33.             echo "Who knows?"
  34.             shift
  35.             ;;
  36.  
  37.         * )
  38.             arg="$arg $1"
  39.             shift
  40.             ;;
  41.     esac
  42. done
  43.  
  44. other_cmd $arg
  45. exit
  46.  
  47. The problem is that last line.  If I use that, then arguments which have
  48. embedded blanks turn into separate words - not good.
  49.  
  50. If I make the line:
  51.  
  52. other_cmd "$arg"
  53.  
  54. then all the flags become one argument - not good.
  55.  
  56. If I try to put quotes around the arg="$arg \"$1\"" they seem to disappear
  57. before other_cmd get the arguments.
  58.  
  59. What trick do I need here?
  60. -- 
  61. Larry W. Virden                 UUCP: osu-cis!chemabs!lvirden
  62. Same Mbox: BITNET: lvirden@cas  INET: lvirden@cas.org
  63. Personal: 674 Falls Place,   Reynoldsburg, OH 43068-1614
  64. America Online: lvirden@aol.com
  65.