Aerosol Posted December 8, 2014 Report Posted December 8, 2014 In computer programing, an operator is a symbol or function representing a mathematical operation. Common examples are mathematical arithmetic operations, e.g. ">" for "greater than", or "<" for "les than". Programming languages typically support a set of built-in operators (e.g. +, -, *). As in programing languages, there are various operators supported by each unix shell. Bourne Shell operators: Arithmetic OperatorsRelational OperatorsBoolean OperatorsString OperatorsFile Test Operators LinuxBox#vi my_script.sh #!/bin/sh # summation of 2 numbers val=`expr 3 + 2` echo "Total value: $val !" LinuxBox# LinuxBox# ./my_script.sh Total value: 5 !One thing to notice is that Bourne shell originally didn't have any mechanism to perform simple arithmetic but it uses external programs. Common example of performing arithmetic is done by using simple "expr" program. Simple example of summation of two numbers: Also notice that there must be spaces between operators and expressions - for example "2+2" is not correct, where as it should be written as "2 + 2". Also, completed expression should be enclosed between `` signs, called inverted commas.) Arithmetic Operators:+ Addition - Adds values on either side of the operator - Subtraction - Subtracts right hand operand from left hand operand * Multiplication - Multiplies values on either side of the operator / Division - Divides left hand operand by right hand operand% Modulus - Divides left hand operand by right hand operand and returns remainder= Assignment - Assign right operand in left operand == Equality - Compares two numbers, if both are same then returns true!= Not Equality - Compares two numbers, if both are different then returns true Important thing to note here is that all the conditional expressions should be put inside square braces with one spaces around them. For example [ $a == $b ] is correct where as [$a==$b] is incorrect. Relational Operators:Relational operators are specific to numeric values. These operators would not work for string values unless their value is numeric. For example, operators would work to check a relation between 5 and 10 as well as in between "5" and "10" but not in between "five" and "ten". -eq - Checks if the value of two operands are equal or not-ne - Checks if the value of two operands are not equal-gt - Checks if the value of left operand is greater than the value of right operand-lt - Checks if the value of left operand is less than the value of right operand-ge - Checks if the value of left operand is greater than or equal to the value of right operand-le - Checks if the value of left operand is less than or equal to the value of right operand Important thing to note here is that all the conditional expressions should be put inside square braces with one spaces around them, For example [ $A <= $A ] is correct where as [$A <= $B] is not correct. Boolean OperatorsBoolean algebra is the subarea of algebra in which the values of the variables are the truth values true and false. For example, boolean operator for logical negation "!" could be combined with Relational Operator which checks equalty, which would in the end make them to check for "NOT equality". ! - This is logical negation. This inverts a true condition into false and vice versa-o - This is logical OR. If one of the operands is true then condition would be true-a - This is logical AND. If both the operands are true then condition would be true otherwise it would be falseString OperatorsString operators are operators intended for use with variables that hold string vaules. = Checks if the value of two operands are equal or not, if yes then condition becomes true!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true-z Checks if the given string operand size is zero. If it is zero length then it returns true-n Checks if the given string operand size is non-zero. If it is non-zero length then it returns truestr Check if str is not the empty string. If it is empty then it returns false File Test OperatorsFile Test Operators are operators to test various properties associated with a Unix files. It is assumed that variable "file" holds an file name (full path to specific file). -b file Checks if file is a block special file if yes then condition becomes true-c file Checks if file is a character special file if yes then condition becomes true-d file Check if file is a directory if yes then condition becomes true-f file Check if file is an ordinary file as opposed to a directory or special file if yes then condition becomes true-g file Checks if file has its set group ID (SGID) bit set if yes then condition becomes true-k file Checks if file has its sticky bit set if yes then condition becomes true-p file Checks if file is a named pipe if yes then condition becomes true-t file Checks if file descriptor is open and associated with a terminal if yes then condition becomes true-u file Checks if file has its set user id (SUID) bit set if yes then condition becomes true-r file Checks if file is readable if yes then condition becomes true-w file Check if file is writable if yes then condition becomes true-x file Check if file is execute if yes then condition becomes true-s file Check if file has size greater than 0 if yes then condition becomes true-e file Check if file exists. Is true even if file is a directory but existsNext, see Shell scripting tutorial and examples of usage of shell operators. Source Quote
Dubfx Posted December 8, 2014 Report Posted December 8, 2014 Am mai intalnit distributii fara expr, o alternativa este "let":ex: let i=1+1*2;echo $i; Quote