|
Home > Archive > Unix Shell > February 2007 > How to convert a shell string (separated by spaces) to a shell array?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to convert a shell string (separated by spaces) to a shell array?
|
|
| melroysoares@hotmail.com 2007-02-23, 1:21 am |
| Let's say I have a shell string a = " this is a shell string" . How do
I convert it
into an array (in C-shell) so that each word in the string is an
element.
In other words, let "b" be the new string. then b[1] = "this". b[2] =
"is"
b[3] = "a" and so on. could someone suggest a simple way to create an
array from this string according to this in C-shell.
Thanks
| |
| Chris F.A. Johnson 2007-02-23, 1:21 am |
| On 2007-02-23, melroysoares@hotmail.com wrote:
> Let's say I have a shell string a = " this is a shell string" . How do
> I convert it
> into an array (in C-shell) so that each word in the string is an
> element.
> In other words, let "b" be the new string. then b[1] = "this". b[2] =
> "is"
> b[3] = "a" and so on. could someone suggest a simple way to create an
> array from this string according to this in C-shell.
In bash or ksh93:
a=" this is a shell string"
set -f
array=( $a )
See these aticles for reasons against using csh for scripts:
<http://www.grymoire.com/Unix/CshTop10.txt>
<http://www.grymoire.com/Unix/Csh.html#uh-0>
<http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Stephane CHAZELAS 2007-02-23, 1:18 pm |
| 2007-02-22, 21:07(-05), Chris F.A. Johnson:
> On 2007-02-23, melroysoares@hotmail.com wrote:
>
> In bash or ksh93:
>
> a=" this is a shell string"
> set -f
> array=( $a )
[...]
It will also split on <Tabs> and <NLs> (or whatever $IFS
contains).
zsh:
array=($=a)
(uses $IFS which also include $'\0' by default).
(zsh is not affected by the bug/misfeature that makes set -f
necessary in bash/ksh)
Or you can do:
array=($(s: a})
to specify the separator.
In POSIX shells,
set -f
IFS=' '
set -- $a
elements available as "$1", "$2"...
--
Stéphane
| |
| Chris F.A. Johnson 2007-02-23, 7:17 pm |
| On 2007-02-23, Stephane CHAZELAS wrote:
>
> (zsh is not affected by the bug/misfeature that makes set -f
> necessary in bash/ksh)
I do wish you would stop this nonsense. It is not a misfeature; it
is one of the strengths of the shell.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Stephane CHAZELAS 2007-02-23, 7:17 pm |
| 2007-02-23, 16:29(-05), Chris F.A. Johnson:
> On 2007-02-23, Stephane CHAZELAS wrote:
>
> I do wish you would stop this nonsense. It is not a misfeature; it
> is one of the strengths of the shell.
Please explain what's the point of globbing being performed
upon variable expansion.
--
Stéphane
| |
| Chris F.A. Johnson 2007-02-23, 7:17 pm |
| On 2007-02-23, Stephane CHAZELAS wrote:
> 2007-02-23, 16:29(-05), Chris F.A. Johnson:
>
> Please explain what's the point of globbing being performed
> upon variable expansion.
To expand wildcards, of course. I use it frequently.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Stephane CHAZELAS 2007-02-23, 7:17 pm |
| 2007-02-23, 17:01(-05), Chris F.A. Johnson:
[...]
>
> To expand wildcards, of course. I use it frequently.
In all the languages but sh, to expand wildcards, you use a
globbing operator/function. Doing it as part of variable
expansion *by default!* is just nonsense to me.
cmd glob($var)
(as in perl)
or:
cmd $~var
(as in zsh)
that is explicitely requesting the globbing make sense to me.
For me,
cmd $var
should pass the content of $var as an argument to cmd, passing
it the list of files that match the content of $var taken as a
pattern is counter-intuitive. I call that a bug or misfeature.
Are you really going to tell me it's not what you think?
--
Stéphane
| |
| Chris F.A. Johnson 2007-02-25, 7:16 pm |
| On 2007-02-23, Stephane CHAZELAS wrote:
> 2007-02-23, 17:01(-05), Chris F.A. Johnson:
> [...]
>
> In all the languages but sh, to expand wildcards, you use a
> globbing operator/function. Doing it as part of variable
> expansion *by default!* is just nonsense to me.
If you don't like it, turn it off. I have no problem with it, and
rarely turn it off.
....
> Are you really going to tell me it's not what you think?
Definitely. Other languages do whatever they like; the shell is
different. So what?
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Stephane CHAZELAS 2007-02-27, 7:21 am |
| 2007-02-26, 03:03(+00), Bruce Barnett:
> melroysoares@hotmail.com writes:
>
>
> Besides the other comments, in csh, you use
>
> set a = "this is a shell string"
> set b = ( $a )
> echo $b[1] $b[2]
>
> outputs "this is"
[...]
csh is a little better than sh:
% set a = "it's another one, isn't it?"
% set b = ( $a )
set: No match.
csh lets you beware that "?" is a globbing pattern. Both csh and
sh would have happily expanded "it?" into the list of files that
match that pattern. But at least csh lets you that there's a
problem with your command line if there's no matching file.
Here, you obviously don't want "it?" to match files, so you need
to disable filename expansion. csh would have problems with
quotes as well.
There's a ":x" to /quote/ word individually after word splitting
has been performed:
% set b = ( $a:x )
% echo $b[1]:q
it's
%
--
Stéphane
|
|
|
|
|