Professional Documents
Culture Documents
Conditional Statements Programming
Conditional Statements Programming
Conditional Statements Programming
programming
1.if statement
2.if-else statement
3.if..elif..else..fi statement (Else If ladder)
4.if..then..else..if..then..fi..fi..(Nested if)
5.switch statement
Their description with syntax is as follows:
1. if statement
This block will process if specified condition is true.
Syntax:
if [ expression ]
then
statement
fi
2. if-else statement
If specified condition is not true in if part then else part will be execute.
Syntax
if [ expression ]
then
statement1
else
statement2
fi
if [ expression1 ]
then
statement1
statement2
.
elif [ expression2 ]
then
statement3
statement4
.
else
statement5
fi
4. if..then..else..if..then..fi..fi..(Nested if)
Nested if-else block can be used when, one condition is satisfies then it
again checks another condition. In the syntax, if expression1 is false then
it processes else part, and again expression2 will be check.
Syntax:
if [ expression1 ]
then
statement1
statement2
.
else
if [ expression2 ]
then
statement3
.
fi
fi
5. switch statement
case statement works as a switch statement if specified value match with the
pattern then it will execute a block of that particular pattern
When a match is found all of the associated statements until the double
semicolon (;;) is executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax:
case in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
Esac
Tcsh
#!/bin/tcsh
# First line of the script is shebang which tells the system how to execute
the
# script: http://en.wikipedia.org/wiki/Shebang_(Unix)
# TCSH emulates the shebang on systems which don't understand it.
# 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
# starts faster.
# 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
# done so.
set echo_style = both
# example: create a project and then informs you that it finished while
# it does the installation.
make && ( espeak "BOSS, compilation finished"; make install )
# prints the home directory but leaving you where you were
(cd; pwd); pwd
# Remove a variable
unset var
# Prints 1 (true) if the variable `var' exists otherwise prints 0 (false)
echo $?var
# Print all variables and their values
set
### LISTs
# Assign a list of values
set var = ( one two three four five )
# Print all the elements: one two three four five
echo $var
echo $var[*]
# Print the count of elements: 5
echo $#var
# Print indexed element; prints the second element: two
echo $var[2]
# Print range of elements; prints 2nd up to 3rd: two, three
echo $var[2-3]
# Prints all elements starting from the 3rd: three four five
echo $var[3-]
# Prints print all up to 3rd element: one two three
echo $var[-3]
# $path, $PATH the list of directories that will search for executable to
run
# $home, $HOME user's home directory, also the `~' can be used instead
# $uid user's login ID
# $user user's login name
# $gid the user's group ID
# $group the user's group-name
# $cwd, $PWD the Current/Print Working Directory
# $owd the previous working directory
# $tcsh tcsh version
# $tty the current tty; ttyN for linux console, pts/N for terminal
# emulators under X
# $term the terminal type
# $verbose if set, causes the words of each command to be printed.
# can be set by the `-v' command line option too.
# $loginsh if set, it is a login shell
# example: read a name from standard input and display a greetings message
echo -n "Enter your name? "
set name = $<
echo "Greetings $name"
# Operators:
# == equal != not equal ! not
# > greater than < less than >= greater or equal <= less or equal
# && logical AND || logical OR
# single-line form
if ( $name != $user ) echo "Your name isn't your username"
# To use && and || with if statements, you don't need multiple pairs of
# square brackets:
if ( "$name" == "Steve" && "$age" == 15 ) then
echo "This will run if $name is Steve AND $age is 15."
endif
# Arithmetic Operators
# +, -, *, /, %
#
# Arithmetic Operators which must be parenthesised
# !, ~, |, &, ^, ~, <<, >>,
# Compare and logical operators
#
# All operators are same as in C.
# wrong
@ x = $y+1
@ x = 0644 & 022; echo $x
@ x = (0644 & 022) +1; echo $x
@ x = (0644 & 022)+ 1; echo $x
@ x = ( ~077 ); echo $x
# correct
@ x = $y + 1
@ x = ( 0644 & 022 ) + 1; echo $x
@ x = ( ~ 077 ); echo $x
@ x = ( ~ 077 | 022 ); echo $x
@ x = ( ! 0 ); echo $x
# You can also use `perl', `php' or even several BASICs, but prefer the
# above utilities for faster load-and-run results.
# the same by using `calc' (and using binary as the original req)
set x = `calc '0b1001 | 0b110'`; echo $x
# Examples:
cd ~/Downloads # go to my `Downloads' directory
# example:
ls -l | grep key | less
# "ls -l" produces a process, the output (stdout) of which is piped to the
# input (stdin) of the process for "grep key"; and likewise for the process
# for "less".
# the `ls', the `grep' and the `less' are programs of Unix and they have
their
# 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.
# NOTE: `pipe' mechanism has Windows too, but it is buggy and I sign it for
all
# versions until Windows XP SP3 API32 which was the last one that I worked
on.
# Microsoft still denied it but is well known bug since it is a 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
# 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
zcat /usr/man/man1/tcsh.1.gz | groff -Tps -man | okular -
# a better version
zcat `locate -b -n 1 '\tcsh.1.gz'` | groff -Tps -man | okular -
# even better
set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz");
zcat `eval $loc` | groff -Tps -man | okular -
#### IF-THEN-ELSE-ENDIF
# Syntax:
# if ( expr ) then
# ...
# [else if ( expr2 ) then
# ...]
# [else
# ...]
# endif
#
# 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
# are executed, etc.
# Any number of else-if pairs are possible; only one endif is needed.
#
# Single-line form:
#
# if ( expr ) command
#
# If `expr' evaluates true, then command is executed.
# `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.
#
# BUG: Input/output redirection occurs even if expr is false and command is
# thus not executed.
#
#### SWITCH-ENDSW
# Syntax:
# switch ( expr )
# case pattern:
# ...
# [breaksw]
# [default:
# ...]
# endsw
#
# tcsh uses a case statement that works similarly to switch in C.
# Each case label is successively matched, against the specified string which
# is first command and filename expanded. The file metacharacters `*', `?'
# and `[...]' may be used in the case labels. If none of the labels match the
# execution begins after the default label if its defined.
# The command `breaksw' causes execution to continue after the endsw.
Otherwise
# control may fall through case labels and default labels as in C.
switch ( $var )
case *.[1-9]:
case *.[1-9].gz:
echo "$var is a man-page."
breaksw
case *gz:
echo "$var is gzipped"
breaksw
default:
file $var
endsw
#### FOREACH-END
# Syntax:
# foreach name ( wordlist )
# ...
# [break | continue]
# end
#
# Successively sets the variable `name' to each member of `wordlist' and
# executes the sequence of commands between this command and the matching
# `end' keyword. The `continue' keyword jump to the next element back to
# top; and the `break' keyword terminates the loop.
#
# BUG: `foreach' doesn't ignore here documents when looking for its end.
# example: counting 1 to 10
foreach i ( `seq 1 10` )
echo $i
end
#### WHILE-END
# while ( expr )
# ...
# [break | continue]
# end
#
# Executes the commands between the `while' and the matching `end' while
`expr'
# evaluates non-zero. `break' and `continue' may be used to terminate or
# continue the loop prematurely.
# count from 1 to 10
set num = 1
while ( $num <= 10 )
echo $num
@ num ++
end
#### REPEAT
# Syntax: repeat count command
#
# The specified command, which is subject to the same restrictions as the
# command in the one line if statement above, is executed count times.
# I/O redirections occur exactly once, even if count is 0.
#
# TIP: in most cases prefer `while'
# Alias the cd command so that when you change directories, the contents
# are immediately displayed.
alias cd 'cd \!* && ls'
switch ( $todo )
case option1:
# ...
$0 results
breaksw
case option2:
# ...
$0 results
breaksw
case results:
echo "print the results here"
# ...
breaksw
default:
echo "Unknown option: $todo"
# exit 0
endsw
# --- Recursion method --- end ---
# ---------------------------------------------------------------------------
--
# Appendices