Differences between revisions 2 and 3
Revision 2 as of 2020-05-27 21:32:57
Size: 5802
Editor: Burathar
Comment:
Revision 3 as of 2020-05-27 21:58:05
Size: 7036
Editor: Burathar
Comment:
Deletions are marked like this. Additions are marked like this.
Line 51: Line 51:

== Integer Comparison Operators ==
||'''Operator'''||'''C-style variant'''||'''Meaning'''||'''Usage'''||'''Usage C-Style'''||
||<:>`-eq` ||<:> none || Is equal to || `if [ "$a" -eq "$b" ]` || - ||
||<:>`-ne` ||<:> none || Is not equal to || `if [ "$a" -ne "$b" ]` || - ||
||<:>`-gt` ||<:> `>` || Is greater than || `if [ "$a" -gt "$b" ]` || `(("$a" > "$b"))` ||
||<:>`-ge` ||<:> `>=` || Is greater or equal to || `if [ "$a" -ge "$b" ]` || `(("$a" >= "$b"))` ||
||<:>`-lt` ||<:> `<` || Is less than || `if [ "$a" -lt "$b" ]` || `(("$a" < "$b"))` ||
||<:>`-le` ||<:> `<=` || Is less than or equal to || `if [ "$a" -le "$b" ]` || `(("$a" <= "$b"))` ||

== String Comparison Operators ==
||'''Operator'''||'''Meaning'''||'''Usage'''||
||<:>`=` || Is equal to || `if [ "$a" = "$b" ]` ||
||<:>`==` || Is equal to<<FootNote(The == comparison operator behaves differently within a double-brackets test than within single brackets. See [[https://tldp.org/LDP/abs/html/comparison-ops.html | this documentation ]])>> || `if [ "$a" == "$b" ]` ||
||<:>`!=` || Is not equal to || `if [ "$a" != "$b" ]` ||
TODO: Continue here https://tldp.org/LDP/abs/html/comparison-ops.html
||<:>`=` || Is equal to || `if [ "$a" = "$b" ]` ||

Description

This is a reference for bash scripting. For more general instructions take a look here.

Operators

Usage

Bash operators are mainly used in if statements, to check against a variable or a hard coded object. Example:

   1 #!/bin/bash 
   2 name=Foo  
   3 
   4 if [ -e $name ] 
   5 then 
   6     echo File Exist 
   7 else
   8     echo File doesnot exist 
   9 fi 

File Test Operators

Operator

Checks for

Returns

(-a)

File exits DEPRECATED

True if it exits, otherwise false

-b

File is a block device

True if the file is a block device, otherwise false

-c

File is a character device

True if it is a character device, otherwise false

-d

File is a directory

True if it is a directory, otherwise false

-e

File exits

True if it exits, otherwise false

-ef

Files are hard links to the same file

True if files before and after operator are hard links to the same file, otherwise false

-f

File is regular file

True if regular, false if directory or device file

-g

Set-group-id (sgid) flag set on directory1

True if flag is set, otherwise false

-G

Group-id of file is equal to the current user's

True group-id is equal to the user's, otherwise false

-h

File is a symbolic link

True if file is a symbolic link, otherwise false

-k

Sticky bit set2

True if bit is set, otherwise false

-L

File is a symbolic link

True if file is a symbolic link, otherwise false

-N

File is modified since last read

True file is modified since last read, otherwise false

-nt

File is newer than other file

True if file preceding operator is newer than file behind the operator, otherwise false

-ot

File is older than other file

True if file preceding operator is older than file behind the operator, otherwise false

-O

Current user is owner of file

True if user is owner, otherwise false

-p

File is a pipe

True if file is a pipe, otherwise false

-r

File has read permission (for the user running the test)

True if it has read access, otherwise false

-s

File Size

True if file > 0, otherwise false

-S

File is a socket

True if file is a socket, otherwise false

-t

File (descriptor) is associated with a terminal device3

True if file is associated with a terminal device, otherwise false

-u

Set-user-id (suid) flag set on file4

True if flag is set, otherwise false

-w

File has write permission (for the user running the test)

True if it has write access, otherwise false

-x

File has execute permission (for the user running the test)

True if it has execute access, otherwise false

!

NOT, reverses the tests above

True if condition absent, otherwise reverse of condition

Integer Comparison Operators

Operator

C-style variant

Meaning

Usage

Usage C-Style

-eq

none

Is equal to

if [ "$a" -eq "$b" ]

-

-ne

none

Is not equal to

if [ "$a" -ne "$b" ]

-

-gt

>

Is greater than

if [ "$a" -gt "$b" ]

(("$a" > "$b"))

-ge

>=

Is greater or equal to

if [ "$a" -ge "$b" ]

(("$a" >= "$b"))

-lt

<

Is less than

if [ "$a" -lt "$b" ]

(("$a" < "$b"))

-le

<=

Is less than or equal to

if [ "$a" -le "$b" ]

(("$a" <= "$b"))

String Comparison Operators

Operator

Meaning

Usage

=

Is equal to

if [ "$a" = "$b" ]

==

Is equal to5

if [ "$a" == "$b" ]

!=

Is not equal to

if [ "$a" != "$b" ]

TODO: Continue here https://tldp.org/LDP/abs/html/comparison-ops.html

=

Is equal to

if [ "$a" = "$b" ]

Arithmetic Operators

Operator

Name

Funtion

+

Plus

3 + 6 = 6

-

Minus

6 - 3 = 3

*

Multiply

3 * 3 = 9

/

Divide

9 / 3 = 3

**

Exponent

3 ** 2 = 9

%

Modulo

24 % 4 = 4

Each arithmetic operator can be followed with an equals sign(=), which writes the output of the operation into the first variable. An example:

   1 #!/bin/bash
   2 a=2
   3 $((a += 1)) 
   4 echo $a
   5 
   6 # returns: 3
   7 

Logical Operators

Operator

Name

Funtion

&&

AND

"true" && "false" = false

||

OR

"true" || "false" = true

!

NOT

! "true" = false

Usage:

   1 #!/bin/bash 
   2 if [ $condition1 ] && [ $condition2 ]; ...
   3 if [[ $condition1 && $condition2 ]]; ...
   4 if [ ! -f $FILENAME ]; ...

Bitwise Operators

Operator

Name

Funtion

&

AND

1010 & 1100 = 1000

|

OR

1010 | 1100 = 1110

^

XOR

1010 ^ 1100 = 0110

~

NOT/Complement

~1010 = 0101

<<

Left Shift

1100 << 1 = 1000

>>

Right Shift

1100 >> 1 = 0110

Bitwise operators are usually not used in if statements, but for number manipulation. An example:

   1 #!/bin/bash 
   2 
   3 a=$((2#1010))
   4 b=12
   5   
   6 bitwiseAND=$(( a&b )) 
   7 echo Bitwise AND of a and b is $bitwiseAND 
   8 
   9 # returns: Bitwise AND of a and b is 8
  10 

Each bitwise operator can be followed with an equals sign(=), which writes the output of the operation into the first variable. An example:

   1 #!/bin/bash
   2 a=2
   3 $((a <<= 1)) 
   4 echo $a
   5 
   6 # returns: 4
   7 
  1. If a directory has the sgid flag set, then a file created within that directory belongs to the group that owns the directory, not necessarily to the group of the user who created the file. This may be useful for a directory shared by a workgroup. (1)

  2. Commonly known as the sticky bit, the save-text-mode flag is a special type of file permission. If a file has this flag set, that file will be kept in cache memory, for quicker access. [3] If set on a directory, it restricts write permission. Setting the sticky bit adds a t to the permissions on the file or directory listing. This restricts altering or deleting specific files in that directory to the owner of those files. (2)

  3. This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal. (3)

  4. A binary owned by root with set-user-id flag set runs with root privileges, even when an ordinary user invokes it. (4)

  5. The == comparison operator behaves differently within a double-brackets test than within single brackets. See this documentation (5)

Howto/Bash (last edited 2022-02-15 16:24:53 by Burathar)