Boolean Comparisons

Boolean Comparisons
#!/bin/sh
# Take this file, and cut and paste it into a file called 
# boolean-comps.sh Then do: chmod +x boolean-comps.sh
# Then from the command line type: ./boolean-comps.sh

clear
echo
echo "The ! operator placed in front of any test expression "
echo "negates the result of the expression."
echo

var1=4 var2=9

echo var1=$var1 var2=$var2
echo
echo "[ ! "\"\$var2"\" -eq "\"\$var1"\" ]"
[ ! "$var2" -eq "$var1" ]
echo "exit status = $? because 4 is not equal to 9."
echo "although 4 does not equal 9, the ! character negates the expression."
echo
echo "In effect it is the same as:"
echo
echo [ "\"\$var1"\" -ne "\"\$var2"\" ]
echo
sleep 5
echo but not the same as:
echo
echo [ "\"\$var1" != "\"\$var2"\" ]
echo 
echo because this expression is for string operators.
echo
echo
echo "The -a operator performs a logical AND on two expressions as above."
echo
echo "It will return a true (0) value only if  and  are both true."
echo
echo "[ -f "\"myfile"\" -a -x "\"myfile"\" ]"
echo
echo "This command will return (0) true value if" 
echo
echo
var4=7
echo var4=$var4
echo [ "\"\$var4"\" -gt 4 -a "\"\$var4"\" -lt 10 ]
[ "$var4" -gt 4 -a "$var4" -lt 10 ]
echo "exit status = $? because var4 is 7 which is greater than 4 and"
echo "less than 10."
echo
sleep 5
echo
echo "The operator -a has a lower precedence than the integer comparison "
echo "operators.  That means that the intial comparison operators make"
echo "their comparison followed by the -a operator which follows up."
echo
echo
echo "The operator -o performs a logical OR on two expressions.  It will "
echo "return a true (0) value if either  or  is true."
echo

echo "[ -p "\"myfile"\" -a -d "\"myfile"\" ]"
echo
echo "This command will return a true (0) value if myfile is either a "
echo "pipe file or directory file." 
echo

echo "[ "\"\$month"\" -gt 12 -o "\"\$day"\" -gt 31 ]"
echo
echo "This command will return a 0 value if month contains an integer "
echo "greater than 12 or day contains an integer greater than 31."
echo
echo
echo "                      LOGICAL OPERATORS                         "
echo
echo
echo "&& and || enable you to execute a command depending on whether "
echo "the preceding command succeeds or fails."
echo
echo "In the construct  && , "
echo "is executed only if the status from  is zero."
echo

echo 
echo "[ -x "\"progfile"\" ] && progfile  "
echo
echo "This command first checks to see if progfile is executable "
echo "and if it is, progfile is then executed."
echo
echo
year=1999 day=30 month=11
echo "[ "\"\$month"\" -le 12 -a "\"\$day"\" -lt 32 ] ||"\"
echo "Invalid date \$year \$month \$day"
echo
echo "The test part of this command returns an exit status of zero if "
echo "month has an integer value of less than or equal to 12 and when "
echo "day is less than 32."
echo
echo "In other words it checks to see if the month and day values are legal."
echo "If month and day are invalid, the echo command is executed."
echo
echo "So the command after a || operator is executed only if the command"
echo "before it returns a non-zero (false) exit status.  The \ character "
echo "at the end of the line signals... continuation to the shell."
echo  
echo "Strictly speaking, the backslash character at the end of the line"
echo "suppresses the special meaning of newline and treats it as whitespace."
echo "It is used as a long command line to signify that what follows belongs "
echo "to the same line."
echo
echo "Very often in programming languages, the parts of compound comparisons"
echo "are enclosed in parentheses.  This ensures that no confusion arises "
echo "over the order in which operations are carried out."
echo
echo "To suppress the special meaning of parentheses the backslash character"
echo "must be used."
echo
echo "[ \( \"\$month"\" -le 12 \) -a \( \"\$day"\" -lt 32 \)] "
echo
echo
echo "                      ARITHMETIC OPERATORS "
echo
echo
echo "The operators are:"
echo "                   +   addition "
echo "                   -   subtraction "
echo "                   *   multiplication "
echo "                   /   division "
echo "                   %   modulus "
echo
echo "To do arithmatic the shell uses the expr command.  The command informs"
echo "the shell that what follows is to be treated as a mathematical "
echo "expression.  Omitting expr can produce unexpected results."
echo
echo
echo "Typing expr $x * $y will result in an error message."
echo
echo "The * here will be read as a metacharacter and requires a backslash "
echo " \ character."
echo
echo "x=4 y=5 z=6"
echo "expr \$x \* \$y \* \$z = 120"
echo
echo "The % modulus operator gives the number left over when one number is"
echo "divided by the other."
echo
echo "The output from the command expr 22 % 7 is 1.It only gives the remainder."
echo
echo "The output from the command expr 24 % 8 is 0."
echo
echo "Probably the most commonly used construct is the increment by one:"
echo
echo "                       count=\`expr \$count+1\`    "
echo
echo
echo "x=3"
x=3
echo "                       z=\`expr \$x + 4\`  " 
echo

  z=`expr $x + 4`   

echo "The result of this addition is: $z "
echo
x=5
echo "x=5"
e=`expr $x \* 5`
echo 			     
echo "	                     e=\`expr \$x \* 5\` "
echo
echo "The result of this multiplication is: $e "
echo
echo
echo "                        QUOATING SPECIAL CHARACTERS"
echo
echo "            Three distinct examples of how to use special characters."
echo
echo \$1 million dollars ... and that\'s a bargain!
echo "\$1 million dollars ... and that's a bargain!"
echo '$1 million dollars ... and that'\'s a bargain!
echo
echo Exercise \#1, \#2, \#3 are now complete - example

Learning Resources

Latest News

News

Links

 
Unix, Solaris, Java, Perl fundamentals, administration courses in London, Kent, South-East and UK
Unix, Solaris, Java, Perl fundamentals, administration courses in London, Kent, South-East and UK