Operators in the shell

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators tmp file

1. Arithmetic operators

2. Relational operators

3. Boolean operator

4. String operator

5. FILE-related operators

Arithmetic operators

The bash shell does not provide any mechanism to perform simple arithmetic operations, but we can use some other programs, such as expr

#!/bin/shval= ' Expr 2 + 2 ' echo "Total value: $val"

Note: There must be a space between the operator and the expression, and the entire expression should be enclosed in anti-quotation marks.

Other Available arithmetic operators:

+: ' Expr 10 + 20 '

-: ' Expr 10-20 '

*: ' Expr 10 \* 20 ' (need to escape multiplication sign)

/: ' Expr 10/2 '

%: ' Expr 10% 3 '

=: (Assignment) A = $b, assigning the value of B to a

= =: [$a = = $b] Determines whether two numbers are equal and returns true for equality

! =: [$a! = $b] Determines whether two number does not want to wait, does not want to wait to return true

Note: The variables and operators in all conditional expressions need to be separated by commas. As for why, personally, I think it is similar to $ $, $ ... $n, this method of parameter acquisition, if connected together, the system may be connected to the two as an operand or something.

Relational operators

-EQ: Determine whether two operands are equal, example: [$a-eq $b]

-ne: Determine whether two operators do not want to wait, for example: [$a-ne $b]

-GT: Determines whether the first operand is greater than the second operand, for example: [$a-gt $b]

-LT: Determines whether the first operand is less than the second operand, for example: [$a-lt $b]

-ge:[$a GE $b] to determine if the $a is greater than or equal to $b

-le: [$a le $b] to determine whether the $a is less than or equal to $b

logical operators

-A: With, [$a-lt 20-a $b-gt], $a less than 20 and $b greater than 100 returns true

-O: Or, [$a-lt 20-o $b-gt], $a less than 20 or $b greater than 100 returns true

!: No, [! false] Returns True

string comparison

=:[$a = $b], to determine whether the $a is equal to the $b, note that it is not "= ="

! =: [$a! = $b], judging whether $a and $b don't want to wait

-Z: Determines whether the string length is 0,[-Z $a] If $a length is 0 returns true

-N: Determines whether the string length is not 0,[-n $a] if $a length is not 0 returns true

str:[$a], if the string is full of spaces, it will return false, "" = False, the method of getting the length of the string: ${#str}

I do not know the specific use of-Z and-N, the above-Z and-N, when the string length is not 0-Z also returns TRUE.

File-related Judgments

-B: Determine if the file is a block special file, the hard disk partition is a block special file, such as: [-B/DEV/DISK1S1] will return true.

-C: Determine if the file is character special file, [-C $file]

-D: Determine if the directory is not

-F: Judging is not a normal file, but not a directory or special file

-G: Determine if the file set SGID (set GID)

-K: Determine if the file is set to Sbit (Sticky Bit)

-P: Determine if the file is a named pipe

-T: Determines whether the file descriptor is open and associates the terminal

-U: Determine if the file is set to SUID (set User ID)

-R: Determine if the file is readable

-W: Determine if the file is writable

-X: Determine if the file is executable

-S: Determine if the file size is greater than 0

-E: Determine if the file exists

Extended reading: 1, character special and block special file types

When a program reads or writes data from a file, the requests go to a kernel driver. If the file is a regular file, the data are handled by a filesystem driver and it's typically stored in zones on a disk or Other storage media, and the data, are read from a file are what's previously written in. There is other file types for which different things happen.

When data was read or written to a device file, the request was handled by the driver for that device. Each device file has a associated number which identifies the driver to use. What's the device does with the data is it own business.

Block devices (also called block special files) usually behave a lot like ordinary files:they is an array of bytes, a nd the value is read at a given location is the value of the last written there. Data from block device can is cached in memory and read back from cache; Writes can be buffered. Block devices is normally seekable (i.e. there-a notion of position inside the file which the application can change). The name "Block device" comes from the fact, the corresponding hardware typically reads and writes a whole block at a Time (e.g a sector on a hard disk).

Character devices (also called Character special files) behave like pipes, serial ports, etc. Writing or reading to them are an immediate action. What's the driver does with the data was its own business. Writing a byte to a character device might cause it to is displayed on screens, output on a serial port, converted into a s Ound, ... Reading a byte from a device might cause the serial port to wait for input, might return a random byte ( /dev/urandom ), ... The name "character device" comes from the fact, each character is handled individually.

The translation comes roughly:

Block special to read and write data is given to the device driver processing, how to deal with the device-driven thing. Block device data read and write cache, block device read and write data is block (such as: a sector on the disk).

Character files (character special) read and write data similar to pipelines, ports, read and write are processed immediately, the result of writing these files may be displayed to the screen, output to the port, converted to sound, etc., from these files can cause the port to wait for input, or return a random Byte (/dev/urandom), which is called character special because these files handle only a single character at a time.

2, SGID, SUID, Sticky Bit

SUID

SUID appears on the execution bit of the file owner's permissions, and a file with this permission will give the caller temporary permission to the owner of the file at the time it executes.

Such as:

Results:

-rwsr-xr-x 1 root root 42824 Sep  2012/usr/bin/passwd  

We can see the file owner's execution bit there is s instead of X, so say passwd This program has SUID permissions. Until we change the user's password, we use the passwd command, and we know that under Linux, the user's password is stored in the/etc/shadow file. First look at the permissions of the/etc/shadow file:

Ls-l/etc/shadow  

Results:

-rw-r-----1 root Shadow 1138 Dec 20:00/etc/shadow  

As a result of the above, we know that only root can write data to the shadow file, and other users don't even have access to the view. How do we usually change the password? Yes, it's about suid. When we use the passwd command, we get the owner of the passwd, root, and then write to the shadow file. The use of suid must satisfy a few points:

1.SUID is valid only for binary files

2. The caller has the right to execute the file

3. During execution, the caller will temporarily get the owner permission for the file

4. This permission is valid only in the course of program execution

SGID

Sgid is the abbreviation for set GID, which appears on the execution bit of the group permissions that the file belongs to, and it is valid for both normal binaries and directories. When it acts on a normal file, similar to suid, the user obtains the permissions of the group to which the file belongs when the file is executed. When Sgid acts on the table of contents, the meaning is very significant. When a user has write and execute permissions on a directory, the user can create a file in that directory, and if the directory is decorated with Sgid, the file created by that user in this directory belongs to the group to which the directory belongs.

  

Sbit

Sbit is the sticky bit, which appears on the execution bit of other user rights, and it can only be used to decorate a directory. When a directory has sbit permissions, any user who can create a file in this directory, the user in this directory created by the file, only the user himself and root can be deleted, no other users can. For example:

Ls-ld/tmp  

Results:

DRWXRWXRWT root root 12288 Dec 16:33/tmp  

You can see the last one is T, which indicates that the/tmp file is such a file.

So, how do you set the three permissions mentioned above? First of all, to introduce a bit of pre-knowledge, with numbers to express permissions:

4 means suid  2 means sgid  1 means sbit  

If two or three permissions exist at the same time, adding the value of the Write permission to the person is the desired result. It is advantageous for suid and sgid to exist simultaneously, then 6. Here are some examples of modifications:

chmod 4777 Test
To make the test file have suid permissions, you may already see that it is possible to add these special permission values in front of normal file permissions.

Operators in the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.