Help with strings in Bourne shell

Michael J. Chinni, SMCAR-CCS-E mchinni at pica.army.mil
Thu Apr 27 00:30:17 AEST 1989


Jim Rudolf <rudolf at oce.orst.edu> writes:
> If I have a Bourne script called 'foo' and I call it with the arguments:
>    foo "color = red" "size = big"
> then from within foo they will be read as:
>    $1 = "color = red"
>    $2 = "size = big"
> However, I want to read from stdin (or maybe a redirected pipe), and I
> can't get it to work no matter what strange combination of quotes I use!
> I would like to do something like:
>    read args
>    for i in $args
>    .
>    .
> so I can process each string in turn.  Why can't I get this to work?
> Do command line arguments get treated differently than arguments read
> from within the script?  Any suggestions would be greatly appreciated.
As to how to do this, others have given answers. As to why this doesn't work,
read on.

The problem is with the "for" loop. The "for" construct uses a list of arguments
which are words separated by tabs/spaces. Therefore, "for" will split your
arguments into their individual words.  Given your command line arguments:
	echo $1
	echo $2
will produce
	color = red
	size = big

I know of no way to do what you want using a "for" loop.

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
			    Michael J. Chinni
      Chief Scientist, Simulation Techniques and Workplace Automation Team
	US Army Armament Research, Development, and Engineering Center
 User to skeleton sitting at cobweb    () Picatinny Arsenal, New Jersey  
   and dust covered terminal and desk  () ARPA: mchinni at pica.army.mil
    "System been down long?"           () UUCP: ...!uunet!pica.army.mil!mchinni
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



More information about the Comp.unix.questions mailing list