Grep Operator in Linux
Grep Operator in Linux
Escaped Characters: Any of the special characters can be matched as a regular character by escaping them with a
‘\’.
Example: “\$\*” will match the lines that contain the string “$*”
Character Range: A set of characters enclosed in a ‘[‘ and ‘]’ pair specify a range of characters to be matched.
“[aeiou]”
“[0-9]”
Repetition Modifier: A ‘*’ after a character or group of characters is used to allow matching zero or more instances of
the preceding pattern.
The grep command supports a number of options for additional controls on the matching:
•-i: performs a case-insensitive search.
•-n: displays the lines containing the pattern along with the line numbers.
•-v: displays the lines not containing the specified pattern.
•-c: displays the count of the matching patterns.
EXAMPLE:
• Match all lines that start with ‘hello’. E.g: “hello there”
$ grep “^hello” file1
• Match all lines that end with ‘done’. E.g: “well done”
$ grep “done$” file1
• Match all lines that contain any of the letters ‘a’, ‘b’, ‘c’, ‘d’ or ‘e’.
$ grep “[a-e]” file1
• Match all lines that start with a digit following zero or more spaces. E.g: “ 1.” or “2.”
$ grep “ *[0-9]” file1
• Match all lines that contain the word hello in upper-case or lower-case
$ grep -i “hello”
Chown Command In Linux (File Ownership)
The chown command changes user ownership of a file, directory, or link in Linux. Every file is connected with an
owner user or group. It is vital to establish file and folder permissions appropriately.
-ne Checks if the value of two operands are equal or not; if values are
not equal, then the condition becomes true. [ $a -ne $b ] is true.
-gt Checks if the value of left operand is greater than the value of right
operand; if yes, then the condition becomes true. [ $a -gt $b ] is not true.
-lt Checks if the value of left operand is less than the value of right
operand; if yes, then the condition becomes true. [ $a -lt $b ] is true.
-ge Checks if the value of left operand is greater than or equal to the
value of right operand; if yes, then the condition becomes true. [ $a -ge $b ] is not true.
-le Checks if the value of left operand is less than or equal to the
value of right operand; if yes, then the condition becomes true. [ $a -le $b ] is true.
It is very important to understand that all the conditional expressions should be placed inside square braces with
spaces around them. For example, [ $a <= $b ] is correct whereas, [$a <= $b] is incorrect.
NOTE:
The if...else statements
If else statements are useful decision-making statements which
can be used to select an option from a given set of options.
Unix Shell supports following forms of if…else statement −
•if...fi statement
•if...else...fi statement
•if...elif...else...fi statement
#!/bin/sh if...then...else...fi statement is a decision-making statement
a=10
if [ $a -lt $b ]
b=20 then
echo "$a -lt $b: a is less than b"
if [ $a -eq $b ] else
then echo "$a -lt $b: a is not less than b"
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b“
if [ $a -ge $b ]
fi
then
echo "$a -ge $b: a is greater or equal to b"
if [ $a -ne $b ] else
then echo "$a -ge $b: a is not greater or equal to b"
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b" if [ $a -le $b ]
then
if [ $a -gt $b ] echo "$a -le $b: a is less or equal to b"
then else
echo "$a -gt $b: a is greater than b" echo "$a -le $b: a is not less or equal to b"
else
echo "$a -gt $b: a is not greater than b"
There are various operators supported by each shell. We will discuss in detail about Bourne shell (default
shell) in this chapter.
We will now discuss the following operators −
•Arithmetic Operators
•Relational Operators
•Boolean Operators
•String Operators
•File Test Operators
#!/bin/sh val=`expr 2 + 2`
echo "Total value : $val"
-R makes every single file on the system under / (root) have rwxrwxrwx permissions.