CShell foreach loop

CShell foreach loop

am 12.10.2005 07:10:35 von a

echo `$CMD f=$f $fileName`
The f and fileName goes in pairs.
f=1 fName="f1"
f=2 fName="f2"
foreach #loop the f and fName pair#
echo `$CMD f=$f $fileName`
end
How to write this loop?
Thanx

Re: CShell foreach loop

am 12.10.2005 14:45:38 von Bruce Barnett

"a" writes:

> echo `$CMD f=$f $fileName`
> The f and fileName goes in pairs.
> f=1 fName="f1"
> f=2 fName="f2"
> foreach #loop the f and fName pair#
> echo `$CMD f=$f $fileName`
> end
> How to write this loop?
> Thanx
>
>
Which shell are you using?
If you are using csh (not really recommended for scripting)
you need to use
set fName = "f1"
and not
fName="f1"

and what is fName vs fileName?

And example would help.



--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

Re: CShell foreach loop

am 12.10.2005 15:40:03 von Ed Morton

a wrote:

> echo `$CMD f=$f $fileName`
> The f and fileName goes in pairs.
> f=1 fName="f1"
> f=2 fName="f2"
> foreach #loop the f and fName pair#
> echo `$CMD f=$f $fileName`
> end
> How to write this loop?
> Thanx
>
>

You REALLY need to take a look at the FAQ
(http://home.comcast.net/~j.p.h/cus-faq.html), especially question 17
(http://home.comcast.net/~j.p.h/cus-faq-2.html#17).

Ed.

Re: CShell foreach loop

am 12.10.2005 18:53:19 von Janis Papanagnou

a wrote:
> echo `$CMD f=$f $fileName`
> The f and fileName goes in pairs.
> f=1 fName="f1"
> f=2 fName="f2"
> foreach #loop the f and fName pair#
> echo `$CMD f=$f $fileName`
> end
> How to write this loop?
> Thanx

Whatever shell, loop only over the index and construct the filename
by concatenating the index to the filename prefix within the loop.

Janis

switching shells

am 12.10.2005 21:35:14 von MD Websunlimited

>Ed Morton wrote:
>
> You REALLY need to take a look at the FAQ
> (http://home.comcast.net/~j.p.h/cus-faq.html), especially question 17
> (http://home.comcast.net/~j.p.h/cus-faq-2.html#17).
>

As someone who:
1) has used cshell way too long for scripts
2) is thinking about switching to a different shell
3) and needs to use arrays fairly often in scripts

I have a question. Can /sh handle arrays, or if I'm going to have arrays
should I use bash. Without knowing anything about any shells except
tcsh/csh that I've been using (and will continue to use for my
interactive shell) I'd like to know what shell is recommended for me. I
suppose I'm looking for a good compromise between portability and
breadth of features (such as arrays). Any suggestions or web pages that
compare/contrast all the shells. Thanks.

Ben

Re: switching shells

am 12.10.2005 21:46:27 von Ed Morton

Ben wrote:

> >Ed Morton wrote:
>
>>
>> You REALLY need to take a look at the FAQ
>> (http://home.comcast.net/~j.p.h/cus-faq.html), especially question 17
>> (http://home.comcast.net/~j.p.h/cus-faq-2.html#17).
>>
>
> As someone who:
> 1) has used cshell way too long for scripts
> 2) is thinking about switching to a different shell
> 3) and needs to use arrays fairly often in scripts
>
> I have a question. Can /sh handle arrays, or if I'm going to have arrays
> should I use bash. Without knowing anything about any shells except
> tcsh/csh that I've been using (and will continue to use for my
> interactive shell) I'd like to know what shell is recommended for me. I
> suppose I'm looking for a good compromise between portability and
> breadth of features (such as arrays). Any suggestions or web pages that
> compare/contrast all the shells. Thanks.


Basic borune shell doesn't have arrays, but ksh88, ksh93, bash, etc...
all do. e.g. in ksh88:

$ cat tstarrays.ksh
set -A array a b c d e

echo "array size = \"${#array[*]}\""

echo "array contents = \"${array[@]}\""

echo "array elements, accessed directly one at a time:"
for element in "${array[@]}"
do
echo "\t\"$element\""
done

echo "array contents, indexed one at a time (indices start at 0):"
(( i = 0 ))
(( size = ${#array[*]} ))
while (( $i < $size ))
do
element=${array[$i]}
echo "\tarray[$i] = \"$element\""
(( i = $i + 1 ))
done

$ tstarrays.ksh
array size = "5"
array contents = "a b c d e"
array elements, accessed directly one at a time:
"a"
"b"
"c"
"d"
"e"
array contents, indexed one at a time (indices start at 0):
array[0] = "a"
array[1] = "b"
array[2] = "c"
array[3] = "d"
array[4] = "e"

I'd go with ksh88, since it's the most basic but usefull Bourne-like
shell so you get most of the benefits of the others plus portability.

Regards,

Ed.

Re: switching shells

am 12.10.2005 22:09:53 von joe

Ben writes:

> I have a question. Can /sh handle arrays, or if I'm going to have
> arrays should I use bash. Without knowing anything about any shells
> except tcsh/csh that I've been using (and will continue to use for
> my interactive shell) I'd like to know what shell is recommended for
> me. I suppose I'm looking for a good compromise between portability
> and breadth of features (such as arrays). Any suggestions or web
> pages that compare/contrast all the shells. Thanks.

In addition to what Ed said, here's a FAQ that gives some differences
between other shells. It's not complete and is fairly old (1997),
but it will give you the general idea.

Also interesting reading ...

http://www.faqs.org/faqs/unix-faq/shell/shell-differences/

http://humorix.org/articles/2002/09/nullify/

Joe
--
Gort, klatu barada nikto

Re: switching shells

am 12.10.2005 23:01:04 von MD Websunlimited

joe@invalid.address wrote:

> Also interesting reading ...
>
> http://www.faqs.org/faqs/unix-faq/shell/shell-differences/

Wow...that WAS an interesting (and informative) read. Thanks.

Re: switching shells

am 13.10.2005 00:13:16 von Enrique Perez-Terron

On Wed, 12 Oct 2005 21:35:14 +0200, Ben wrote:

> >Ed Morton wrote:
>>
>> You REALLY need to take a look at the FAQ
>> (http://home.comcast.net/~j.p.h/cus-faq.html), especially question 17
>> (http://home.comcast.net/~j.p.h/cus-faq-2.html#17).
>>
>
> As someone who:
> 1) has used cshell way too long for scripts
> 2) is thinking about switching to a different shell
> 3) and needs to use arrays fairly often in scripts
>
> I have a question. Can /sh handle arrays, or if I'm going to have arrays
> should I use bash. Without knowing anything about any shells except
> tcsh/csh that I've been using (and will continue to use for my
> interactive shell) I'd like to know what shell is recommended for me. I
> suppose I'm looking for a good compromise between portability and
> breadth of features (such as arrays). Any suggestions or web pages that
> compare/contrast all the shells. Thanks.

If you are going to continue using tcsh for interactive use, why do the
scripting in a shell? Why not use e.g., ruby? Ruby is available on most
platforms, and your scripts will be portable to those platforms.

It is not harder to learn than any Bourne-derivative, and saves you
endless clutter: ${#arghhhh[*]} is seven punctuation characters just
to get the length of that array. Compare to "ahhhh.length". A cleaner
language saves you hours of confusion over trivial mistakes.

-Enrique

Re: switching shells

am 13.10.2005 00:27:19 von cfajohnson

On 2005-10-12, Enrique Perez-Terron wrote:
> On Wed, 12 Oct 2005 21:35:14 +0200, Ben wrote:
>> As someone who:
>> 1) has used cshell way too long for scripts
>> 2) is thinking about switching to a different shell
>> 3) and needs to use arrays fairly often in scripts
>>
>> I have a question. Can /sh handle arrays, or if I'm going to have arrays
>> should I use bash. Without knowing anything about any shells except
>> tcsh/csh that I've been using (and will continue to use for my
>> interactive shell) I'd like to know what shell is recommended for me. I
>> suppose I'm looking for a good compromise between portability and
>> breadth of features (such as arrays). Any suggestions or web pages that
>> compare/contrast all the shells. Thanks.
>
> If you are going to continue using tcsh for interactive use, why do the
> scripting in a shell? Why not use e.g., ruby? Ruby is available on most
> platforms, and your scripts will be portable to those platforms.

The advantage to using the same shell for scripting as for
interactive use is that you can try snippets (or even multi-line
scripts) directly at the command line.

Why learn two langauges when one will do? For more serious
programming, if you can't do it in the shell, use a real
programming language such as C.

> It is not harder to learn than any Bourne-derivative, and saves you
> endless clutter: ${#arghhhh[*]} is seven punctuation characters just
> to get the length of that array. Compare to "ahhhh.length". A cleaner
> language saves you hours of confusion over trivial mistakes.

That's still seven characters.

--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

Re: switching shells

am 13.10.2005 02:39:30 von Bruce Barnett

Ed Morton writes:

> Basic borune shell doesn't have arrays, but ksh88, ksh93, bash,
> etc... all do. e.g. in ksh88:

Well, one might say the Bourne shell has one array. You use $1 $2 $3,
shift, etc.

Ugly, as you have to save your arguments.

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

Re: switching shells

am 13.10.2005 09:34:09 von Stephane CHAZELAS

2005-10-12, 15:35(-04), Ben:
> >Ed Morton wrote:
>>
>> You REALLY need to take a look at the FAQ
>> (http://home.comcast.net/~j.p.h/cus-faq.html), especially question 17
>> (http://home.comcast.net/~j.p.h/cus-faq-2.html#17).
>>
>
> As someone who:
> 1) has used cshell way too long for scripts
> 2) is thinking about switching to a different shell
> 3) and needs to use arrays fairly often in scripts
>
> I have a question. Can /sh handle arrays, or if I'm going to have arrays
> should I use bash. Without knowing anything about any shells except
> tcsh/csh that I've been using (and will continue to use for my
> interactive shell) I'd like to know what shell is recommended for me. I
> suppose I'm looking for a good compromise between portability and
> breadth of features (such as arrays). Any suggestions or web pages that
> compare/contrast all the shells. Thanks.
[...]

Note that the obvious choice when coming from the csh world is
zsh, as it has all the features of tcsh plus the better design
of the Bourne-likes. Contrary to bash, it also fixed some design
errors in the Bourne like shells, has many useful features of
its own (more than any other shell) and is also more
user-friendly. See www.zsh.org for more details.

As for portability, for a user shell, that's not the main point
to look at. To write script, don't think in shell, think in
language. The obvious choice is the POSIX or Unix language as it
is specified by the Single Unix Specification
(http://www.opengroup.org/onlinepubs/009695399/toc.htm).

zsh has a POSIX conformant mode that can be used to run POSIX
conpliant scripts (when called as sh or when you issue "emulate
sh", just as bash or ksh). But, in any case, it's better to use
you system's sh to run POSIX scripts (though you can install zsh
as your system's sh).

As for arrays, the POSIX language has a limited support for
arrays, they are the function positionnal parameters ($1, $2...
$@), but chances are that if you need arrays in a script, then
you need a real programming language, not a shell. Then consider
languages such as perl/python/ruby on the interpreted side.

Contrary to bash, zsh has support for real arrays plus support
for associative arrays (bash has some kind of associative array
support whose keys are limited to positive integers that it
calls arrays).

Please note that the zsh language is to be used at your prompt or to
write extensions to zsh for interactive use (a big part of zsh
features is written in zsh, the rest in C, just like a big part
of emacs is written in emacs lisp). Just like I wouldn't write
an emacs lisp "script", except to add a feature to emacs, I
wouldn't consider writing zsh (nor bash nor ksh93 nor tcsh)
scripts that I would share with anyone (unless they are intended
as zsh extensions). Instead, I would write a POSIX script that I
know every POSIX conformant shell can interpret.

(POSIX means portable operating system interface, Unixish).

--
Stéphane