-->

Previous | Table of Contents | Next

Page 419

Command Environment


var=\$test              pdksh and bash







set var = \$test         tcsh



The backslash (\) before the dollar sign ($) signals to the shell to interpret the $ as any other ordinary character and not to associate any special meaning to it.

Backtick

You can use the backtick (`) character to signal the shell to execute the string delimited by the backtick. This can be used in shell programs when you want the result of execution of a command to be stored in a variable. For example, if you want to count the number of lines in a file called test.txt in the current directory and store the result in a variable called var, then you can use the following command:

Command Environment


var=`wc _l test.txt`               pdksh and bash







set var = `wc _l test.txt`         tcsh



Comparison of Expressions

The logical comparison of two operators (numeric or string) is done slightly differently, depending on which shell you are in. In pdksh and bash, a command called test can be used to achieve comparisons of expressions. In tcsh, you can write an expression to accomplish the same thing.

pdksh and bash

This section covers comparisons using the pdksh or bash shells. Later in the chapter, the section "tcsh" contains a similar discussion for the tcsh shell.

The syntax of the test command is as follows: test expression




or



[ expression ]

Both these forms of test commands are processed the same way by pdksh and bash. The test commands support the following types of comparisons:

Page 420

String Comparison

The following operators can be used to compare two string expressions:

= To compare if two strings are equal
!= To compare if two strings are not equal
-n To evaluate if the string length is greater than zero
-z To evaluate if the string length is equal to zero

Next are some examples comparing two strings, string1 and string2, in a shell program called compare1:


string1="abc"

string2="abd"

if [ string1 = string2 ] then

   echo "string1 equal to string2"

else

   echo "string1 not equal to string2"

fi



if [ string2 != string1 ] then

   echo "string2 not equal to string1"

else

   echo "string2 equal to string2"

fi



if [ string1 ] then

   echo "string1 is not empty"

else

   echo "string1 is empty"

fi



if [ -n string2 ] then

   echo "string2 has a length greater than zero"

else

   echo "string2 has length equal to zero"

fi



if [ -z string ]

   echo "string1 has a length equal to zero"

else

  echo "string1 has a length greater than zero"

fi

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


string1 not equal to string2

string2 not equal to string1

string1 is not empty

string2 has a length greater than zero

string1 has a length greater than zero

Page 421

If two strings are not equal in size, the system will pad the shorter string with trailing spaces for comparison. That is, if string1 has value of abc and that of string2 is ab, then for comparison purposes, string2 will be padded with trailing spaces—that is, it will have a value of ab .

Number Comparison

The following operators can be used to compare two numbers:

-eq To compare if two numbers are equal
-ge To compare if one number is greater than or equal to the other number
-le To compare if one number is less than or equal to the other number
-ne To compare if two numbers are not equal
-gt To compare if one number is greater than the other number
-lt To compare if one number is less than the other number

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


number1=5

number2=10

number3=5



if [ number1 _eq number3 ] then

   echo "number1 is equal to number3"

else

   echo "number1 is not equal to number3"

fi



if [ number1 _ne number2 ] then

   echo "number1 is not equal to number2"

else

   echo "number1 is equal to number2"

fi



if [ number1 _gt number2 ] then

   echo "number1 is greater than number2"

else

   echo "number1 is not greater than number2"

fi



if [ number1 _ge number3 ] then

   echo "number1 is greater than or equal to number3"

else

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

fi



if [ number1 _lt number2 ] then

   echo "number1 is less than number2"

Page 422


else

   echo "number1 is not less than number2"

fi



if [ number1 _le number3 ] then

   echo "number1 is less than or equal to number3"

else

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

fi

When you execute the shell program compare2, you will get the following results:


number1 is equal to number3

number1 is not equal to number2

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

File Operators

These operators can be used as file comparison operators:

-d To ascertain if a file is a directory
-f To ascertain if a file is a regular file
-r To ascertain if read permission is set for a file
-s To ascertain if the name of a file has a length greater than zero
-w To ascertain if write permission is set for a file
-x To ascertain if execute permission is set for a file

Assume that in a shell program called compare3, there is 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 code for compare3 would look like this:


if [ -d dir1 ] then

   echo "dir1 is a directory"

else

   echo "dir1 is not a directory"

fi



if [ -f dir1 ] then

   echo "file1 is a regular file"

else

   echo "file1 is not a regular file"

fi



if [ -r file1 ] then

   echo "file1 has read permission"

else

   echo "file1 does not have read permission"

fi

Previous | Table of Contents | Next