Merge pull request #4801 from vali19th/master

[tcsh/en] fix: tcsh.html.markdown
This commit is contained in:
Marcel Ribeiro-Dantas 2023-11-29 00:01:49 -03:00 committed by GitHub
commit 3ddb3bb03f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,23 +28,23 @@ Some more files:
```tcsh ```tcsh
#!/bin/tcsh #!/bin/tcsh
# First line of the script is shebang which tells the system how to execute the # The first line of the script is a shebang which tells the system how to execute
# script: http://en.wikipedia.org/wiki/Shebang_(Unix) # the script: http://en.wikipedia.org/wiki/Shebang_(Unix)
# TCSH emulates the shebang on systems which don't understand it. # TCSH emulates the shebang on systems that don't understand it.
# In most cases you'll use `#!/bin/tcsh -f', because `-f' option does not load # In most cases you'll use `#!/bin/tcsh -f`, because `-f` option does not load
# any resource or start-up files, or perform any command hashing, and thus # any resource or start-up files, or perform any command hashing, and thus
# starts faster. # starts faster.
# --- the echo command -------------------------------------------------------- # --- the echo command --------------------------------------------------------
# The `echo' writes each word to the shell's standard output, separated by # The `echo` writes each word to the shell's standard output, separated by
# spaces and terminated with a newline. The echo_style shell variable may be # spaces and terminated with a newline. The echo_style shell variable may be
# set to emulate (or not) the flags and escape sequences. # set to emulate (or not) the flags and escape sequences.
# Display the value of echo_style # Display the value of echo_style
echo $echo_style echo $echo_style
# Enable `echo' to support backslashed characters and `-n' option (no new line) # Enable `echo` to support backslashed characters and `-n` option (no new line)
# This is the default for tcsh, but your distro may change it. Slackware has # This is the default for tcsh, but your distro may change it. Slackware has
# done so. # done so.
set echo_style = both set echo_style = both
@ -65,17 +65,17 @@ echo 'two\nlines'
# --- Basic Syntax ------------------------------------------------------------ # --- Basic Syntax ------------------------------------------------------------
# A special character (including a blank or tab) may be prevented from having # A special character (including a blank or tab) may be prevented from having
# its special meaning by preceding it with a backslash `\'. # its special meaning by preceding it with a backslash `\`.
# this will display the last history commands # This will display the last history commands
echo !! echo !!
# this will not # This will not
echo \!\! echo \!\!
# Single quotes prevents expanding special characters too, but some # Single quotes prevent expanding special characters too, but some
# characters like `!' and backslash have higher priority # characters like `!` and backslash have higher priority
# `$' (variable value) will not expands # `$` (variable value) will not expand
echo '$1 tip' echo '$1 tip'
# `!' (history) will expands # `!` (history) will expand
echo '!!' echo '!!'
# Strings enclosed by back-quotes will be executed and replaced by the result. # Strings enclosed by back-quotes will be executed and replaced by the result.
@ -85,16 +85,16 @@ echo `ls`
echo 'first line'; echo 'second line' echo 'first line'; echo 'second line'
# There is also conditional execution # There is also conditional execution
echo "Always executed" || echo "Only executed if first command fails" echo "Always executed" || echo "Only executed if the first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail" echo "Always executed" && echo "Only executed if the first command does NOT fail"
# Parenthesised commands are always executed in a subshell, # Parenthesised commands are always executed in a subshell,
# example: create a project and then informs you that it finished while # example: creates a project and then informs you that it finished while
# it does the installation. # it does the installation.
make && ( espeak "BOSS, compilation finished"; make install ) make && ( espeak "BOSS, compilation finished"; make install )
# prints the home directory but leaving you where you were # prints the home directory but leaves you where you were
(cd; pwd); pwd (cd; pwd); pwd
# Read tcsh man-page documentation # Read tcsh man-page documentation
@ -103,10 +103,10 @@ man tcsh
# --- Variables --------------------------------------------------------------- # --- Variables ---------------------------------------------------------------
# The shell maintains a list of variables, each of which has as value a list of # The shell maintains a list of variables, each of which has as value a list of
# zero or more words. The values of shell variables can be displayed and # zero or more words. The values of shell variables can be displayed and
# changed with the `set' and `unset' commands. # changed with the `set` and `unset` commands.
# The system maintains its own list of ``environment'' variables. # The system maintains its own list of "environment" variables.
# These can be displayed and changed with `printenv', `setenv' and `unsetenv'. # These can be displayed and changed with `printenv`, `setenv`, and `unsetenv`.
# The syntax of setenv is similar to POSIX sh. # The syntax of `setenv` is similar to POSIX sh.
# Assign a value or nothing will create a variable # Assign a value or nothing will create a variable
# Assign nothing # Assign nothing
@ -122,7 +122,7 @@ set var = `ls`
# Remove a variable # Remove a variable
unset var unset var
# Prints 1 (true) if the variable `var' exists otherwise prints 0 (false) # Prints 1 (true) if the variable `var` exists otherwise prints 0 (false)
echo $?var echo $?var
# Print all variables and their values # Print all variables and their values
set set
@ -130,10 +130,10 @@ set
# Prints the contents of 'var' # Prints the contents of 'var'
echo $var; echo $var;
echo "$var"; echo "$var";
# Prints the string `$var' # Prints the string `$var`
echo \$var echo \$var
echo '$var' echo '$var'
# braces can be used to separate variable from the rest when its needed # Braces can be used to separate variables from the rest when it is needed
set num = 12; echo "There ${num}th element" set num = 12; echo "There ${num}th element"
# Prints the number of characters of the value: 6 # Prints the number of characters of the value: 6
@ -147,7 +147,7 @@ echo $var
echo $var[*] echo $var[*]
# Print the count of elements: 5 # Print the count of elements: 5
echo $#var echo $#var
# Print indexed element; prints the second element: two # Print the indexed element; This prints the second element: two
echo $var[2] echo $var[2]
# Print range of elements; prints 2nd up to 3rd: two, three # Print range of elements; prints 2nd up to 3rd: two, three
echo $var[2-3] echo $var[2-3]
@ -165,8 +165,8 @@ echo $var[-3]
# $! the PID of the last background process started by this shell # $! the PID of the last background process started by this shell
# $$ script's PID # $$ script's PID
# $path, $PATH the list of directories that will search for executable to run # $path, $PATH the list of directories that will search for an executable to run
# $home, $HOME user's home directory, also the `~' can be used instead # $home, $HOME user's home directory, also the `~` can be used instead
# $uid user's login ID # $uid user's login ID
# $user user's login name # $user user's login name
# $gid the user's group ID # $gid the user's group ID
@ -174,24 +174,24 @@ echo $var[-3]
# $cwd, $PWD the Current/Print Working Directory # $cwd, $PWD the Current/Print Working Directory
# $owd the previous working directory # $owd the previous working directory
# $tcsh tcsh version # $tcsh tcsh version
# $tty the current tty; ttyN for linux console, pts/N for terminal # $tty the current tty; ttyN for Linux console, pts/N for terminal
# emulators under X # emulators under X
# $term the terminal type # $term the terminal type
# $verbose if set, causes the words of each command to be printed. # $verbose if set, causes the words of each command to be printed.
# can be set by the `-v' command line option too. # can be set by the `-v` command line option too.
# $loginsh if set, it is a login shell # $loginsh if set, it is a login shell
# TIP: $?0 is always false in interactive shells # TIP: $?0 is always false in interactive shells
# TIP: $?prompt is always false in non-interactive shells # TIP: $?prompt is always false in non-interactive shells
# TIP: if `$?tcsh' is unset; you run the original `csh' or something else; # TIP: if `$?tcsh` is unset; you run the original `csh` or something else;
# try `echo $shell' # try `echo $shell`
# TIP: $verbose this is useful to debugging scripts # TIP: `$verbose` is useful for debugging scripts
# NOTE: $PWD and $PATH are synchronised with $cwd and $pwd automatically. # NOTE: `$PWD` and `$PATH` are synchronised with `$cwd` and `$pwd` automatically.
# --- Variable modifiers ------------------------------------------------------ # --- Variable modifiers ------------------------------------------------------
# Syntax: ${var}:m[:mN] # Syntax: ${var}:m[:mN]
# Where <m> is: # Where <m> is:
# h : the directory t : the filenane r : remove extension e : the extension # h : the directory t : the filename r : remove extension e : the extension
# u : uppercase the first lowercase letter # u : uppercase the first lowercase letter
# l : lowercase the first uppercase letter # l : lowercase the first uppercase letter
# p : print but do not execute it (hist) # p : print but do not execute it (hist)
@ -199,8 +199,8 @@ echo $var[-3]
# x : like q, but break into words at white spaces # x : like q, but break into words at white spaces
# g : apply the following modifier once to each word # g : apply the following modifier once to each word
# a : apply the following modifier as many times as possible to single word # a : apply the following modifier as many times as possible to single word
# s/l/r/ : search for `l' and replace with `r', not regex; the `&' in the r is # s/l/r/ : search for `l` and replace with `r`, not regex; the `&` in the `r` is
# replaced by l # replaced by `l`
# & : Repeat the previous substitution # & : Repeat the previous substitution
# start with this file # start with this file
@ -232,7 +232,7 @@ echo 'this string' >> file.txt
echo 'this string' >>& file.txt echo 'this string' >>& file.txt
# Redirect the standard input from file.txt # Redirect the standard input from file.txt
cat < file.txt cat < file.txt
# Input from keyboard; this stores the input line to variable `x' # Input from keyboard; this stores the input line to variable `x`
set x = $< set x = $<
# Document here; # Document here;
cat << LABEL cat << LABEL
@ -243,7 +243,7 @@ LABEL
(grep 'AGP' /usr/src/linux/Documentation/* > output-file.txt) >& error-file.txt (grep 'AGP' /usr/src/linux/Documentation/* > output-file.txt) >& error-file.txt
# example: read a name from standard input and display a greetings message # example: read a name from standard input and display a greetings message
echo -n "Enter your name? " echo -n "Enter your name: "
set name = $< set name = $<
echo "Greetings $name" echo "Greetings $name"
@ -266,15 +266,15 @@ if ( $name != $user ) echo "Your name isn't your username"
# NOTE: if $name is empty, tcsh sees the above condition as: # NOTE: if $name is empty, tcsh sees the above condition as:
# if ( != $user ) ... # if ( != $user ) ...
# which is invalid syntax # which is invalid syntax
# so the "safe" way to use potentially empty variables in tcsh is: # The "safe" way to use potentially empty variables in tcsh is:
# if ( "$name" != $user ) ... # if ( "$name" != $user ) ...
# which, when $name is empty, is seen by tcsh as: # which, when $name is empty, is seen by tcsh as:
# if ( "" != $user ) ... # if ( "" != $user ) ...
# which works as expected # which works as expected
# There is also conditional execution # There is also conditional execution
echo "Always executed" || echo "Only executed if first command fails" echo "Always executed" || echo "Only executed if the first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail" echo "Always executed" && echo "Only executed if the first command does NOT fail"
# To use && and || with if statements, you don't need multiple pairs of # To use && and || with if statements, you don't need multiple pairs of
# square brackets: # square brackets:
@ -286,14 +286,14 @@ if ( "$name" == "Daniya" || "$name" == "Zach" ) then
echo "This will run if $name is Daniya OR Zach." echo "This will run if $name is Daniya OR Zach."
endif endif
# String matching operators ( `=~' and `!~' ) # String matching operators ( `=~` and `!~` )
# The == != =~ and !~ operators compare their arguments as strings; # The == != =~ and !~ operators compare their arguments as strings;
# all others operate on numbers. The operators =~ and !~ are like != # all others operate on numbers. The operators =~ and !~ are like !=
# and == except that the right hand side is a glob-pattern against which # and == except that the right hand side is a glob-pattern against which
# the left hand operand is matched. # the left-hand operand is matched.
if ( $user =~ ni[ck]* ) echo "Greetings Mr. Nicholas." if ( $user =~ ni[ck]* ) echo "Greetings Mr. Nicholas."
if ( $user !~ ni[ck]* ) echo "Hey, get out of Nicholas PC." if ( $user !~ ni[ck]* ) echo "Hey, get out of Nicholas' PC."
# Arithmetic expressions are denoted with the following format: # Arithmetic expressions are denoted with the following format:
@ result = 10 + 5 @ result = 10 + 5
@ -302,16 +302,16 @@ echo $result
# Arithmetic Operators # Arithmetic Operators
# +, -, *, /, % # +, -, *, /, %
# #
# Arithmetic Operators which must be parenthesised # Arithmetic Operators which must be parenthesized
# !, ~, |, &, ^, ~, <<, >>, # !, ~, |, &, ^, ~, <<, >>,
# Compare and logical operators # Compare and logical operators
# #
# All operators are same as in C. # All operators are the same as in C.
# It is non so well documented that numeric expressions require spaces # It is non so well documented that numeric expressions require spaces
# in-between; Also, `@' has its own parser, it seems that work well when the # in-between; Also, `@` has its own parser, it seems that it works well when
# expression is parenthesised otherwise the primary parser seems it is active. # the expression is parenthesized, otherwise the primary parser seems to be
# Parenthesis require spaces around, this is documented. # active. Parentheses require spaces around, this is documented.
# wrong # wrong
@ x = $y+1 @ x = $y+1
@ -330,32 +330,32 @@ echo $result
# C's operators ++ and -- are supported if there is not assignment # C's operators ++ and -- are supported if there is not assignment
@ result ++ @ result ++
# None shell created to do mathematics; # No shell was created to do mathematics;
# Except for the basic operations, use an external command with backslashes. # Except for the basic operations, use an external command with backslashes.
# #
# I suggest the calc as the best option. # I suggest the calc as the best option.
# (http://www.isthe.com/chongo/tech/comp/calc/) # (http://www.isthe.com/chongo/tech/comp/calc/)
# #
# The standard Unix's bc as second option # The standard Unix's bc as the second option
# (https://www.gnu.org/software/bc/manual/html_mono/bc.html) # (https://www.gnu.org/software/bc/manual/html_mono/bc.html)
# #
# The standard Unix's AWK as third option # The standard Unix's AWK as the third option
# (https://www.gnu.org/software/gawk/manual/gawk.html) # (https://www.gnu.org/software/gawk/manual/gawk.html)
# You can also use `perl', `php' or even several BASICs, but prefer the # You can also use `Perl`, `PHP`, `python`, or even several BASICs, but prefer
# above utilities for faster load-and-run results. # the above utilities for faster load-and-run results.
# real example: (that I answer in StackExchange) # real example: (that I answer in StackExchange)
# REQ: x := 1001b OR 0110b # REQ: x := 1001b OR 0110b
# in `tcsh' expression (by using octal) # in `tcsh` expression (by using octal)
@ x = ( 011 | 06 ); echo $x @ x = ( 011 | 06 ); echo $x
# the same by using `calc' (and using binary as the original req) # the same by using `calc` (and using binary as the original req)
set x = `calc '0b1001 | 0b110'`; echo $x set x = `calc '0b1001 | 0b110'`; echo $x
# --- File Inquiry Operators -------------------------------------------------- # --- File Inquiry Operators --------------------------------------------------
# NOTE: The builtin `filetest' command do the same thing. # NOTE: The built-in `filetest` command does the same thing.
#### Boolean operators #### Boolean operators
# -r read access -w write access -x execute access -e existence # -r read access -w write access -x execute access -e existence
@ -366,23 +366,23 @@ set x = `calc '0b1001 | 0b110'`; echo $x
# -b block device -c char device # -b block device -c char device
# -t file (digit) is an open file descriptor for a terminal device # -t file (digit) is an open file descriptor for a terminal device
# if the file `README' exists, displays a message # If the file `README` exists, display a message
if ( -e README ) echo "I have already README file" if ( -e README ) echo "I have already README file"
# if the `less' program is installed, use this instead of `more' # If the `less` program is installed, use it instead of `more`
if ( -e `where less` ) then if ( -e `where less` ) then
alias more 'less' alias more 'less'
endif endif
#### Non-boolean operators #### Non-boolean operators
# -Z returns the file size in bytes # -Z returns the file size in bytes
# -M returns the modification time (mtime) -M: returns mtime string # -M returns the modification time (mtime) -M: returns mtime string
# -A returns the lass access time (atime) -A: returns atime string # -A returns the last access time (atime) -A: returns atime string
# -U returns the owners user ID -U: returns the owners user-name # -U returns the owner's user ID -U: returns the owner's user name
# -G returns the group ID -G: returns the group-name # -G returns the owner's group ID -G: returns the owner's group name
# -P returns the permissions as octal number -Pmode returns perm. AND mode # -P returns the permissions as octal number -Pmode returns perm. AND mode
# this will display the date as Unix-time integer: 1498511486 # this will display the date as a Unix-time integer: 1498511486
filetest -M README.md filetest -M README.md
# This will display "Tue Jun 27 00:11:26 2017" # This will display "Tue Jun 27 00:11:26 2017"
@ -390,14 +390,14 @@ filetest -M: README.md
# --- Basic Commands ---------------------------------------------------------- # --- Basic Commands ----------------------------------------------------------
# Navigate though file system with `chdir' (cd) # Navigate through the filesystem with `chdir` (cd)
cd path # change working directory cd path # change working directory
cd # change to home directory cd # change to the home directory
cd - # change to previous directory cd - # change to the previous directory
cd .. # go up one directory cd .. # go up one directory
# Examples: # Examples:
cd ~/Downloads # go to my `Downloads' directory cd ~/Downloads # go to my `Downloads` directory
# Use `mkdir` to create new directories. # Use `mkdir` to create new directories.
mkdir newdir mkdir newdir
@ -413,7 +413,7 @@ where csh
# --- Pipe-lines -------------------------------------------------------------- # --- Pipe-lines --------------------------------------------------------------
# A pipeline is a sequence of processes chained together by their standard # A pipeline is a sequence of processes chained together by their standard
# streams, so that the output of each process (stdout) feeds directly as input # streams, so that the output of each process (stdout) feeds directly as input
# (stdin) to the next one. This `pipes' are created with the `|' special # (stdin) to the next one. These `pipes` are created with the `|` special
# character and it is one of the most powerful characteristics of Unix. # character and it is one of the most powerful characteristics of Unix.
# example: # example:
@ -422,18 +422,18 @@ ls -l | grep key | less
# input (stdin) of the process for "grep key"; and likewise for the process # input (stdin) of the process for "grep key"; and likewise for the process
# for "less". # for "less".
# the `ls', the `grep' and the `less' are programs of Unix and they have their # the `ls`, the `grep`, and the `less` are Unix programs and they have their
# own man-page. The `pipe' mechanism is part of the kernel but the syntax # own man-page. The `pipe` mechanism is part of the kernel but the syntax
# and the control is job of the shell, the tcsh in our case. # and the control is the shell's job, the tcsh in our case.
# NOTE: `pipe' mechanism has Windows too, but it is buggy and I sign it for all # NOTE: Windows has the `pipe` mechanism too, but it is buggy and I signed it
# versions until Windows XP SP3 API32 which was the last one that I worked on. # for all versions until Windows XP SP3 API32 which was the last one that I
# Microsoft still denied it but is well known bug since it is a common method # worked on. Microsoft denied it, but it is a well-known bug since it is a
# for inter-process communication. For small I/O it will work well. # common method for inter-process communication. For small I/O it will work well.
# tcsh, along with grep, gcc and perl is one of the first Unix programs that # tcsh, along with grep, GCC, and Perl is one of the first Unix programs that
# ported to DOS (with EMX DOS extender) and later to Windows (1998). # ported to DOS (with EMX DOS extender) and later to Windows (1998).
# example: this will convert tcsh to PostScript and will show it with okular # example: this will convert tcsh to PostScript and will show it with Okular
zcat /usr/man/man1/tcsh.1.gz | groff -Tps -man | okular - zcat /usr/man/man1/tcsh.1.gz | groff -Tps -man | okular -
# a better version # a better version
@ -451,10 +451,10 @@ set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz");
set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz"); set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz");
zcat `eval $loc` | groff -Tps -man | ps2pdf - ${page}.pdf && okular tcsh.pdf zcat `eval $loc` | groff -Tps -man | ps2pdf - ${page}.pdf && okular tcsh.pdf
# NOTE: `okular' is the default application of KDE environment and it shows # NOTE: `okular` is the default application of the KDE environment and it shows
# postcript and pdf files. You can replace it with your lovely pdf viewer. # postcript and pdf files. You can replace it with your lovely PDF viewer.
# zcat, locate, groff, are common programs in all Unices. `ps2pdf' program # `zcat`, `locate`, `groff`, are common programs in all Unixes. The `ps2pdf`
# is part of `ghostscript' package that is widely used. # program is part of the `ghostscript` package that is widely used.
# --- Control Flow ------------------------------------------------------------ # --- Control Flow ------------------------------------------------------------
@ -468,8 +468,8 @@ set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz");
# ...] # ...]
# endif # endif
# #
# If the specified expr is true then the commands to the first else are # If the specified `expr` is true then the commands to the first else are
# executed; otherwise if expr2 is true then the commands to the second else # executed; otherwise if `expr2` is true then the commands to the second else
# are executed, etc. # are executed, etc.
# Any number of else-if pairs are possible; only one endif is needed. # Any number of else-if pairs are possible; only one endif is needed.
# #
@ -477,24 +477,24 @@ set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz");
# #
# if ( expr ) command # if ( expr ) command
# #
# If `expr' evaluates true, then command is executed. # If `expr` evaluates to true, then the command is executed.
# `command' must be a simple command, not an alias, a pipeline, a command list # `command` must be a simple command, not an alias, a pipeline, a command list
# or a parenthesized command list. With few words, avoid to use it. #, or a parenthesized command list. With a few words, avoid using it.
# #
# BUG: Input/output redirection occurs even if expr is false and command is # BUG: Input/output redirection occurs even if expr is false and the command
# thus not executed. # is thus not executed.
# #
# check if we are in non-interactive shell and quit if true # check if we are in a non-interactive shell and quit if true
if ( $?USER == 0 || $?prompt == 0 ) exit if ( $?USER == 0 || $?prompt == 0 ) exit
# check if we are a login shell # check if we are a login shell
if ( $?loginsh ) then if ( $?loginsh ) then
# check if you are on linux console (not X's terminal) # check if you are on linux console (not X's terminal)
if ( $tty =~ tty* ) then if ( $tty =~ tty* ) then
# enable keypad application keys (man console_codes) # enable keypad application keys (man console_codes)
echo '\033=' echo '\033='
endif endif
endif endif
#### SWITCH-ENDSW #### SWITCH-ENDSW
@ -509,37 +509,37 @@ endif
# #
# tcsh uses a case statement that works similarly to switch in C. # tcsh uses a case statement that works similarly to switch in C.
# Each case label is successively matched, against the specified string which # Each case label is successively matched, against the specified string which
# is first command and filename expanded. The file metacharacters `*', `?' # is first command and filename expanded. The file metacharacters `*`, `?`
# and `[...]' may be used in the case labels. If none of the labels match the # and `[...]` may be used in the case labels. If none of the labels match the
# execution begins after the default label if its defined. # execution begins after the default label if it's defined.
# The command `breaksw' causes execution to continue after the endsw. Otherwise # The command `breaksw` causes execution to continue after the endsw. Otherwise,
# control may fall through case labels and default labels as in C. # control may fall through case labels and default labels as in C.
switch ( $var ) switch ( $var )
case *.[1-9]: case *.[1-9]:
case *.[1-9].gz: case *.[1-9].gz:
echo "$var is a man-page." echo "$var is a man-page."
breaksw breaksw
case *gz: case *gz:
echo "$var is gzipped" echo "$var is gzipped"
breaksw breaksw
default: default:
file $var file $var
endsw endsw
#### FOREACH-END #### FOREACH-END
# Syntax: # Syntax:
# foreach name ( wordlist ) # foreach name ( wordlist )
# ... # ...
# [break | continue] # [break | continue]
# end # end
# #
# Successively sets the variable `name' to each member of `wordlist' and # Successively sets the variable `name` to each member of `wordlist` and
# executes the sequence of commands between this command and the matching # executes the sequence of commands between this command and the matching
# `end' keyword. The `continue' keyword jump to the next element back to # `end` keyword. The `continue` keyword jumps to the next element back to
# top; and the `break' keyword terminates the loop. # top, and the `break` keyword terminates the loop.
# #
# BUG: `foreach' doesn't ignore here documents when looking for its end. # BUG: `foreach` doesn't ignore here documents when looking for its end.
# example: counting 1 to 10 # example: counting 1 to 10
foreach i ( `seq 1 10` ) foreach i ( `seq 1 10` )
@ -548,12 +548,12 @@ end
# example: type all files in the list # example: type all files in the list
foreach f ( a.txt b.txt c.txt ) foreach f ( a.txt b.txt c.txt )
cat $f cat $f
end end
# example: convert wma to ogg # example: convert wma to ogg
foreach f ( *.wma ) foreach f ( *.wma )
ffmpeg -i "$f" "$f:r".ogg ffmpeg -i "$f" "$f:r".ogg
end end
#### WHILE-END #### WHILE-END
@ -562,22 +562,22 @@ end
# [break | continue] # [break | continue]
# end # end
# #
# Executes the commands between the `while' and the matching `end' while `expr' # Executes the commands between the `while` and the matching `end` while `expr`
# evaluates non-zero. `break' and `continue' may be used to terminate or # evaluates non-zero. `break` and `continue` may be used to terminate or
# continue the loop prematurely. # continue the loop prematurely.
# count from 1 to 10 # count from 1 to 10
set num = 1 set num = 1
while ( $num <= 10 ) while ( $num <= 10 )
echo $num echo $num
@ num ++ @ num ++
end end
# print all directories of CWD # print all directories of CWD
set lst = ( * ) set lst = ( * )
while ( $#lst ) while ( $#lst )
if ( -d $lst[1] ) echo $lst[1] is directory if ( -d $lst[1] ) echo $lst[1] is directory
shift lst shift lst
end end
# separate command-line arguments to options or parameters # separate command-line arguments to options or parameters
@ -585,12 +585,12 @@ set options
set params set params
set lst = ( $* ) set lst = ( $* )
while ( $#lst ) while ( $#lst )
if ( "$lst[1]" =~ '-*' ) then if ( "$lst[1]" =~ '-*' ) then
set options = ( $options $lst[1] ) set options = ( $options $lst[1] )
else else
set params = ( $params $lst[1] ) set params = ( $params $lst[1] )
endif endif
shift lst shift lst
end end
echo 'options =' $options echo 'options =' $options
echo 'parameters =' $params echo 'parameters =' $params
@ -599,16 +599,16 @@ echo 'parameters =' $params
# Syntax: repeat count command # Syntax: repeat count command
# #
# The specified command, which is subject to the same restrictions as the # The specified command, which is subject to the same restrictions as the
# command in the one line if statement above, is executed count times. # command in the one line `if` statement above, is executed count times.
# I/O redirections occur exactly once, even if count is 0. # I/O redirections occur exactly once, even if `count` is 0.
# #
# TIP: in most cases prefer `while' # TIP: in most cases prefer `while`
repeat 3 echo "ding dong" repeat 3 echo "ding dong"
# --- Functions --------------------------------------------------------------- # --- Functions ---------------------------------------------------------------
# tcsh has no functions but its expression syntax is advanced enough to use # tcsh has no functions but its expression syntax is advanced enough to use
# `alias' as functions. Another method is recursion # `alias` as functions. Another method is recursion
# Alias argument selectors; the ability to define an alias to take arguments # Alias argument selectors; the ability to define an alias to take arguments
# supplied to it and apply them to the commands that it refers to. # supplied to it and apply them to the commands that it refers to.
@ -637,48 +637,48 @@ alias cd 'cd \!* && ls'
#!/bin/tcsh -f #!/bin/tcsh -f
set todo = option1 set todo = option1
if ( $#argv > 0 ) then if ( $#argv > 0 ) then
set todo = $argv[1] set todo = $argv[1]
endif endif
switch ( $todo ) switch ( $todo )
case option1: case option1:
# ... # ...
$0 results $0 results
breaksw breaksw
case option2: case option2:
# ... # ...
$0 results $0 results
breaksw breaksw
case results: case results:
echo "print the results here" echo "print the results here"
# ... # ...
breaksw breaksw
default: default:
echo "Unknown option: $todo" echo "Unknown option: $todo"
# exit 0 # exit 0
endsw endsw
# --- Recursion method --- end --- # --- Recursion method --- end ---
# --- examples ---------------------------------------------------------------- # --- examples ----------------------------------------------------------------
# this script prints available power-states if no argument is set; # this script prints available power-states if no argument is set;
# otherwise it set the state of the $argv[1] # otherwise it sets the state of the $argv[1]
# --- power-state script --- begin -------------------------------------------- # --- power-state script --- begin --------------------------------------------
#!/bin/tcsh -f #!/bin/tcsh -f
# get parameter ("help" for none) # get parameter ("help" for none)
set todo = help set todo = help
if ( $#argv > 0 ) then if ( $#argv > 0 ) then
set todo = $argv[1] set todo = $argv[1]
endif endif
# available options # available options
set opts = `cat /sys/power/state` set opts = `cat /sys/power/state`
# is known? # is known?
foreach o ( $opts ) foreach o ( $opts )
if ( $todo == $o ) then if ( $todo == $o ) then
# found; execute it # found; execute it
echo -n $todo > /sys/power/state echo -n $todo > /sys/power/state
break break
endif endif
end end
# print help and exit # print help and exit
echo "usage: $0 [option]" echo "usage: $0 [option]"
@ -691,19 +691,19 @@ echo "available options on kernel: $opts"
set secret=`shuf -i1-100 -n1` set secret=`shuf -i1-100 -n1`
echo "I have a secret number from 1 up to 100" echo "I have a secret number from 1 up to 100"
while ( 1 ) while ( 1 )
echo -n "Guess: " echo -n "Guess: "
set guess = $< set guess = $<
if ( $secret == $guess ) then if ( $secret == $guess ) then
echo "You found it" echo "You found it"
exit 1 exit 1
else else
if ( $secret > $guess ) then if ( $secret > $guess ) then
echo "its greater" echo "its greater"
else if ( $secret < $guess ) then else if ( $secret < $guess ) then
echo "its lesser" echo "its lesser"
endif endif
endif endif
endif endif
end end
# --- secretnum.csh --- end --------------------------------------------------- # --- secretnum.csh --- end ---------------------------------------------------
@ -711,36 +711,36 @@ end
# Appendices # Appendices
#### About [T]CSH: #### About [T]CSH:
# * CSH is notorious about its bugs; # * CSH is notorious for its bugs;
# * It was also famous about its advanced interactive mode. # * It is also famous for its advanced interactive mode.
# * TCSH is famous that have the most advanced completion subsystem. # * TCSH is famous for having the most advanced completion subsystem.
# * TCSH is famous that have the most advanced aliases subsystem; aliases # * TCSH is famous for having the most advanced aliases subsystem; aliases
# can take parameters and often used as functions! # can take parameters and often be used as functions!
# * TCSH is well known that preferred by people (me too) because of better # * TCSH is well known and preferred by people (me too) because of better
# syntax. All shells are using Thomson's syntax with exception of [t]csh, # syntax. All shells are using Thomson's syntax with the exception of
# fish and plan9's shells (rc, ex). # [t]csh, fish, and plan9's shells (rc, ex).
# * It is smaller and consume far less memory than bash, zsh even mksh! # * It is smaller and consumes far less memory than bash, zsh, and even mksh!
# (memusage reports) # (memusage reports)
# * TCSH still has bugs; less but have; if you write readable clean code you'll # * TCSH still has bugs; fewer, but it does; if you write readable clean code
# find none; well almost none... This has to do with the implementation of # you'll find none; well almost none... This has to do with the implementation
# csh; that no means the other shells has good implementation. # of csh; that doesn't mean the other shells have a good implementation.
# * no one well known shell is capable for regular programming; if your script # * no well-known shell is capable of regular programming; if your script
# getting big, use a programming language, or at least PHP or Perl (good # is getting big, use a programming language, like Python, PHP, or Perl (good
# script languages). # scripting languages).
# #
# Advises: # Advice:
# 1. Do not use redirection in single-line if (it is well documented bug) # 1. Do not use redirection in single-line IFs (it is well documented bug)
# In most cases avoid to use single-line IFs. # In most cases avoid using single-line IFs.
# 2. Do not mess up with other shells code, c-shell is not compatible with # 2. Do not mess up with other shells' code, c-shell is not compatible with
# other shells and has different abilities and priorities. # other shells and has different abilities and priorities.
# 3. Use spaces as you'll use them to write readable code in any language. # 3. Use spaces as you'll use them to write readable code in any language.
# A bug of csh was `set x=1' worked, `set x = 1' worked, `set x =1' did not! # A bug of csh was `set x=1` and `set x = 1` worked, but `set x =1` did not!
# 4. It is well documented that numeric expressions require spaces in-between; # 4. It is well documented that numeric expressions require spaces in between;
# also parenthesise all bit-wise and unary operators. # also parenthesize all bit-wise and unary operators.
# 5. Do not write a huge weird expression with several quotes, backslashes etc # 5. Do not write a huge weird expression with several quotes, backslashes, etc
# It is bad practice for generic programming, it is dangerous in any shell. # It is bad practice for generic programming, it is dangerous in any shell.
# 6. Help tcsh, report the bug here <https://bugs.gw.com/> # 6. Help tcsh, report the bug here <https://bugs.gw.com/>
# 7. Read the man page, `tcsh' has huge number of options, and variables. # 7. Read the man page, `tcsh` has a huge number of options and variables.
# #
# I suggest the following options enabled by default # I suggest the following options enabled by default
# -------------------------------------------------- # --------------------------------------------------
@ -770,14 +770,14 @@ end
# unset time # unset time
# unset tperiod # unset tperiod
# #
# NOTE: If the `backslash_quote' is set, it may create compatibility issues # NOTE: If the `backslash_quote` is set, it may create compatibility issues
# with other tcsh scripts which was written without it. # with other tcsh scripts that were written without it.
# #
# NOTE: The same for `parseoctal', but it is better to fix the problematic # NOTE: The same for `parseoctal`, but it is better to fix the problematic
# scripts. # scripts.
# #
# NOTE: **for beginners only** # NOTE: **for beginners only**
# This enable automatically rescan `path' directories if need to. (like bash) # This enables automatic rescanning of `path` directories if needed. (like bash)
# set autorehash # set autorehash
#### common aliases #### common aliases