Author |
Message |
Semegne Tafess #1 / 18
|
 This for loop in UNIX
I wrote a C program but I can not write it in UNIX in C shell #include <stdio.h> main() { int i, a = 65; for (i = 0; i < 5; i++) { printf("%c\n", a); a += 1; }
A B C D E --------- How can I decide the range? Thank you ST
|
Mon, 13 Oct 2003 12:25:31 GMT |
|
 |
Ronald Fische #2 / 18
|
 This for loop in UNIX
As there is no shell arithmetic in csh, you have to revert to while(...)-loops and use the expr command to increment the counter. See also http://www.primate.wisc.edu/software/csh-tcsh-book/csh-whynot Ronald -- Do NOT reply to the address given in the From: header. If you want to reply by mail, use the following address (after deleting the XXX): Ronald Otto Valentin Fischer <rovf...@earthling.net> (Tired of getting spam after posting a message? http://www.deadspam.com)
|
Mon, 13 Oct 2003 13:39:10 GMT |
|
 |
Keith Thompso #3 / 18
|
 This for loop in UNIX
Yes, csh has shell arithmetic using the "@" command. If you just want to display numbers from 65 to 69, you can do this: @ a = 65 while ( $a <= 69 ) echo $a @ a ++ end Converting the number 65 to the character 'A' is another issue. I don't think there's a builtin way to do this in csh, or in any other shell I'm familiar with.
A trifle overstated IMHO, but good reading. My personal feeling is that any script too complex for csh is better done in Perl. YMMV, of course. -- Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst> Cxiuj via bazo apartenas ni.
|
Mon, 13 Oct 2003 15:29:39 GMT |
|
 |
Paul D. Smit #4 / 18
|
 This for loop in UNIX
%% Keith Thompson <k...@cts.com> writes: >> See also http://www.primate.wisc.edu/software/csh-tcsh-book/csh-whynot kt> A trifle overstated IMHO, but good reading. My personal feeling kt> is that any script too complex for csh is better done in Perl. kt> YMMV, of course. Really? So, you think this: mycommand 2>/tmp/mycommand.errors is better done in Perl? ;) -- ------------------------------------------------------------------------------- Paul D. Smith <psm...@baynetworks.com> HASMAT--HA Software Methods & Tools "Please remain calm...I may be mad, but I am a professional." --Mad Scientist ------------------------------------------------------------------------------- These are my opinions---Nortel Networks takes no responsibility for them.
|
Mon, 13 Oct 2003 22:38:12 GMT |
|
 |
Bill Marcu #5 / 18
|
 This for loop in UNIX
Some shells have printf. I don't know if csh does, or if it's an external command. Of course, you could pipe it to awk '{printf "%c\n",$1}'
RL (ridante laute)
|
Tue, 14 Oct 2003 02:42:14 GMT |
|
 |
Tapani Tarvaine #6 / 18
|
 This for loop in UNIX
printf is an external command, but it treats %c argument as a string (picks its first letter), so it won't help here: % printf "%c\n" 65 6
Yes, that works, and is probably the easiest portable way. With ksh it can be done without external commands like this: typeset -i8 x=65 eval "print '\\0${x#*#}'" I have no idea how to do it with csh (other than by brute force). -- Tapani Tarvainen
|
Tue, 14 Oct 2003 13:38:58 GMT |
|
 |
Ronald Fische #7 / 18
|
 This for loop in UNIX
"Paul D. Smith" <psm...@baynetworks.com> writes:
But it isn't csh either (it is Posix shell syntax). BTW: If you find an easy way in csh to redirect stderr and stdout to different files, let me know. The point is not that some individual command is easier in csh than in Perl, but the combination of the commands, i.e. programming logic, is usually easier in Perl than in most shells, except for trivial examples where you only want to execute, say, a few commands in sequence, maybe passing parameters. But if you start to enhance your script with, say, command line switches, it is far more convenient doing it in Perl. Ronald -- Do NOT reply to the address given in the From: header. If you want to reply by mail, use the following address (after deleting the XXX): Ronald Otto Valentin Fischer <rovf...@earthling.net> (Tired of getting spam after posting a message? http://www.deadspam.com)
|
Tue, 14 Oct 2003 13:58:22 GMT |
|
 |
Ronald Fische #8 / 18
|
 This for loop in UNIX
It is an external command. See man printf -- Do NOT reply to the address given in the From: header. If you want to reply by mail, use the following address (after deleting the XXX): Ronald Otto Valentin Fischer <rovf...@earthling.net> (Tired of getting spam after posting a message? http://www.deadspam.com)
|
Tue, 14 Oct 2003 13:59:32 GMT |
|
 |
Ronald Fische #9 / 18
|
 This for loop in UNIX
Sorry Keith, you are perfectly right! I turned away from csh programming so early that I forgot about its arithmetic capability ;-) Ronald -- Do NOT reply to the address given in the From: header. If you want to reply by mail, use the following address (after deleting the XXX): Ronald Otto Valentin Fischer <rovf...@earthling.net> (Tired of getting spam after posting a message? http://www.deadspam.com)
|
Tue, 14 Oct 2003 14:00:59 GMT |
|
 |
Paul D. Smit #10 / 18
|
 This for loop in UNIX
%% Ronald Fischer <ronald.fisc...@deadspam.com> writes: ronny> "Paul D. Smith" <psm...@baynetworks.com> writes: >> Really? So, you think this: >> >> mycommand 2>/tmp/mycommand.errors >> >> is better done in Perl? ronny> But it isn't csh either (it is Posix shell syntax). I know. But it's too complex to write in csh so I wrote it in Bourne :). ronny> BTW: If you find an easy way in csh to redirect stderr and ronny> stdout to different files, let me know. Yes, this is entirely my point. You said that "any script too complex for csh is better done in Perl". I'm just pointing out even something as completely trivial as redirecting stderr is "too complex for csh", so you must feel that any command line or script that requires redirection of stderr "is better done in Perl". I dunno 'bout anyone else, but I find that _very_ hard to accept ;). PS. There are at least two ways I know of to do it in csh; one will only work sending stdout to the terminal and is marginally obscure, and the other will allow you to pipe or redirect stdout separately _but_ is *seriously* obscure and requires the mkfifo command... -- ------------------------------------------------------------------------------- Paul D. Smith <psm...@baynetworks.com> HASMAT--HA Software Methods & Tools "Please remain calm...I may be mad, but I am a professional." --Mad Scientist ------------------------------------------------------------------------------- These are my opinions---Nortel Networks takes no responsibility for them.
|
Tue, 14 Oct 2003 14:52:35 GMT |
|
 |
Keith Thompso #11 / 18
|
 This for loop in UNIX
"Paul D. Smith" <psm...@baynetworks.com> writes:
Um, no, not really. My statement apparently wasn't quite vague enough. BTW, the above can be done pretty easily in csh: sh -c "mycommand 2>/tmp/mycommand.errors" 8-)} More seriously, since I use tcsh for interactive work, I tend to use csh for some *small* scripts, particularly one-shot scripts that I won't be maintaining. My personal experience has been that *most* csh scripts that become too complex are better done in Perl than in sh -- but that's largely because I'm more familiar with Perl than with sh. I've gotten more familiar with sh recently, and I do find myself using it for some scripts. If I'm still overlooking anything, please squint and re-read the above until it makes sense. 8-)} -- Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst> Cxiuj via bazo apartenas ni.
|
Tue, 14 Oct 2003 16:59:34 GMT |
|
 |
Keith Thompso #12 / 18
|
 This for loop in UNIX
[...]
But then you might as well do the whole thing in awk (or Perl).
Ok, I'll explain mine if you'll explain yours. -- Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst> Cxiuj via bazo apartenas ni.
|
Tue, 14 Oct 2003 17:03:19 GMT |
|
 |
Keith Thompso #13 / 18
|
 This for loop in UNIX
[...]
This seems to work: % cat foo #!/bin/sh echo This is stdout echo This is stderr 1>&2 % ( ./foo > foo.out ) >& foo.err % cat foo.out This is stdout % cat foo.err This is stderr % exit -- Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst> Cxiuj via bazo apartenas ni.
|
Tue, 14 Oct 2003 17:06:33 GMT |
|
 |
Benjamin.Altma #14 / 18
|
 This for loop in UNIX
How about an easy way to redirect standard out to standard error in csh? Or what about if you only want stdout and not stderr and stdout is not a terminal? These were just 2 of the problems mentioned in an article entitled "C Shell Programming Considered Harmful" from the unix cd bookshelf.
|
Tue, 14 Oct 2003 21:39:19 GMT |
|
 |
Keith Thompso #15 / 18
|
 This for loop in UNIX
Most shell scripts do most of their work by invoking external programs. /bin/sh is almost universally available, and can be invoked from a csh script as easily as any other utility. sh -c 'foo 1>&2' sh -c 'foo 2>/dev/null' Ok, I'm not seriously suggesting that this is a good idea in a script; if you need this kind of capability, it's probably better to write the whole script using sh. But if you use csh or tcsh *interactively* (and IMHO there are valid reasons for doing so), "sh -c '...'" is a handy idiom for those cases where you want finer control over I/O redirection than csh provides natively. -- Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst> Cxiuj via bazo apartenas ni.
|
Wed, 15 Oct 2003 10:34:34 GMT |
|
 |
|