-->

Previous | Table of Contents | Next

Page 423


if [ -w file1 ] then

   echo "file1 has write permission"

else

   echo "file1 does not have write permission"

fi



if [ -x dir1 ] then

   echo "dir1 has execute permission"

else

   echo "dir1 does not have execute permission"

fi

If you execute the file compare3, you will get the following results:


dir1 is a directory

file1 is a regular file

file1 has read permission

file1 does not have write permission

dir1 has execute permission

Logical Operators

Logical operators are used to compare expressions using the rules of logic; the characters represent NOT, AND, and OR:

! To negate a logical expression
-a To logically AND two logical expressions
-o To logically OR two logical expressions
tcsh

As stated earlier, the comparisons are different under tcsh than they are under pdksh and bash. This section explains the same concepts as the section "pdksh and bash" but uses the syntax necessary for the tcsh shell environment.

String Comparison

Operators that can be used to compare two string expressions are as follows:

== To compare if two strings are equal
!= To compare if two strings are not equal

The following examples compare two strings, string1 and string2, in the shell program compare1:


set string1 = "abc"

set string2 = "abd"



if  (string1 == string2)  then

   echo "string1 equal to string2"

else

   echo "string1 not equal to string2"

endif

Page 424




if  (string2 != string1)  then

   echo "string2 not equal to string1"

else

   echo "string2 equal to string1"

endif



If you execute compare1, you will get the following results:


string1 not equal to string2

string2 not equal to string1

Number Comparison

These operators can be used to compare two numbers:

>= To compare if one number is greater than or equal to the other number
<= To compare if one number is less than or equal to the other number
> To compare if one number is greater than the other number
< To compare if one number is less than the other number

The next examples compare two numbers, number1 and number2, in a shell program called compare2:


set number1 = 5

set number2 = 10

set number3 = 5



if  (number1 > number2)  then

   echo "number1 is greater than number2"

else

   echo "number1 is not greater than number2"

endif



if  (number1 >= number3) then

   echo "number1 is greater than or equal to number3"

else

   echo "number1 is not greater than or equal to number3"

endif



if  (number1 < number2)  then

   echo "number1 is less than number2"

else

   echo "number1 is not less than number2"

endif



if  (number1 <= number3) then

   echo "number1 is less than or equal to number3"

else

   echo "number1 is not less than or equal to number3"

endif

Executing the shell program compare2, you will get the following results:


number1 is not greater than number2

number1 is greater than or equal to number3

number1 is less than number2

number1 is less than or equal to number3

Page 425

File Operators

These operators can be used as file comparison operators:

-d To ascertain if a file is a directory
-e To ascertain if a file exists
-f To ascertain if a file is a regular file
-o To ascertain if a user is the owner of a file
-r To ascertain if read permission is set for a file
-w To ascertain if write permission is set for a file
-x To ascertain if execute permission is set for a file
-z To ascertain if a file size is zero

The following examples are based on a shell program called compare3 that contains a file called file1 and a subdirectory dir1 under the current directory. Assume that file1 has a permission of r-x (read and execute permission) and dir1 has a permission of rwx (read, write, and execute permission).

The following is the code for the compare3 shell program:


if  (-d dir1) then

   echo "dir1 is a directory"

else

   echo "dir1 is not a directory"

endif



if (-f dir1)  then

   echo "file1 is a regular file"

else

   echo "file1 is not a regular file"

endif



if (-r file1) then

   echo "file1 has read permission"

else

   echo "file1 does not have read permission"

endif



if (-w file1) then

   echo "file1 has write permission"

else

   echo "file1 does not have write permission"

endif



if (-x dir1) then

   echo "dir1 has execute permission"

else

   echo "dir1 does not have execute permission"

endif



if (_z file1) then

   echo "file1 has zero length"

Page 426


else

   echo "file1 has greater than zero length"

endif

If you execute the file compare3, you will get the following results:


dir1 is a directory

file1 is a regular file

file1 has read permission

file1 does not have write permission

dir1 has execute permission

file1 has greater than zero length

Logical Operators

Logical operators are used with conditional statements. These operators are used to perform logical ANDs and ORs, and the third operator is used to negate a logical expression:

! To negate a logical expression
&& To logically AND two logical expressions
|| To logically OR two logical expressions

Iteration Statements

The iteration statements are used to repeat a series of commands contained within the iteration statement to be executed multiple times.

The for Statement

There are a number of formats of the for statement. The first format is as follows:


for curvar in list

do

    statements

done

This form should be used if you want to execute statements once for each value in list; for each iteration, the current value of the list is assigned to vcurvar. list can be a variable containing a number of items or a list of values separated by spaces. This format of the for statement is used by pdksh and bash.

The second format is as follows:


for curvar

do

    statements

done

In this form, the statements are executed once for each of the positional parameters passed to the shell program. For each iteration, the current value of the positional parameter is assigned to the variable curvar.

Previous | Table of Contents | Next