Movatterモバイル変換


[0]ホーム

URL:


Jump to content
Rosetta Code
Search

Category:C Shell

Help
From Rosetta Code
Language
C Shell
Thisprogramming language may be used to instruct a computer to perform a task.
Execution method:Interpreted
Lang tag(s):csh
See Also:


Listed below are all of the tasks on Rosetta Code which have been solved using C Shell.
C Shell is animplementation ofUNIX Shell.Other implementations of UNIX Shell.

csh was the shell that William Joy wrote forBSD.csh accepted the sameUnix commands as other shells, but had a very different syntax (for variable assignments, control flow, and such).csh is not compatible with theBourne Shell.

BSD keeps the C Shell at/bin/csh.Hashbang lines should use the -f option:

<lang csh>#!/bin/csh -f</lang>

Reputation

C Shell is obsolete. Most scriptwriters prefer a Bourne-compatible shell, and few users want to learn two flavors of shells. C Shell introduced tilde expansion (ls ~), job control, command history, and aliases, but POSIX shells now have all of those.

Csh Programming Considered Harmful andTop Ten Reasons not to use the C shell give multiple reasons to avoid C Shell.

tcsh is a later version that fixed many of the problems with csh. It is still actively, if intermittently, maintained and has a following such as on Solaris.

Syntax

The manual for csh(1) claims that C Shell has "a C-like syntax". Several other languages have a C-like syntax, includingJava andPike, and Unix utilitiesAWK andbc. C Shell is less likeC than those other languages.

This example prints aHailstone sequence from 13.

CC Shell
<lang c>#include <stdio.h>

intmain(){

 int n;
 n = 13; printf("%d\n", n); while (n != 1) {   if (n % 2)     n = 3 * n + 1;   else     n /= 2;
   printf("%d\n", n); }
 return 0;

}</lang>

<lang csh>




@ n = 13echo $nwhile ($n != 1)

 if ($n % 2) then   @ n = 3 * $n + 1 else   @ n /= 2 endif echo $n

end


</lang>

C Shell has no braces {} to group the commands. Strange keywords arethen,endif andend. Expressions have$n instead ofn. Assignments use@ n.

C Shell has "a C-like syntax" because C Shell is more like C thanBourne Shell.

Bourne ShellC Shell
<lang bash>n=13

echo $nwhile test $n -ne 1; do

 if expr $n % 2 >/dev/null; then   n=`expr 3 \* $n + 1` else   n=`expr $n / 2` fi echo $n

done</lang>

<lang csh>@ n = 13

echo $nwhile ($n != 1)

 if ($n % 2) then   @ n = 3 * $n + 1 else   @ n /= 2 endif echo $n

end</lang>

Bourne Shell requirestest orexpr to evaluate expressions. C Shell has built-in expressions, so the Hailstone sequence comes more easily. These expressions have a stupid quirk: all operators are right-associative, so10 - 3 - 2 acts like10 - (3 - 2). The fix is to use parentheses.

<lang csh>% @ n = 10 - 3 - 2% echo $n9% @ n = (10 - 3) - 2% echo $n5</lang>

Links

Subcategories

This category has the following 3 subcategories, out of 3 total.

Pages in category "C Shell"

The following 31 pages are in this category, out of 31 total.

Retrieved from "https://rosettacode.org/wiki/Category:C_Shell?oldid=197815"
Categories:
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

[8]ページ先頭

©2009-2026 Movatter.jp