Linux Shell bash if parameter __linux

Source: Internet
Author: User
Tags readable syslog

Use in shell programming to determine parameters within the IF statement

–b returns True when file exists and is a block file

-C Returns True when file exists and is a character file

-D returns True when pathname exists and is a directory

-E Returns True when pathname specified file or directory exists

-F Returns True when file exists and is a regular file

-G returns True when the file or directory specified by pathname exists and the Sgid bit is set

-H Returns True when file exists and is a symbolic link file, which is not valid on some old systems

-K returns True when a file or directory specified by pathname exists and a "sticky" bit is set

-P Returns True when file exists and is a command pipe

-R when the file or directory specified by pathname is present and returned as true when it is readable

-S returns True when file size is greater than 0 o'clock

-U returns True when the file or directory specified by pathname exists and the SUID bit is set

-W Returns True when the file or directory specified by pathname exists and is executable. A directory that is accessed for its content is necessarily executable.

-O Returns True if the file or directory specified by pathname exists and the user specified by the active user ID of the current process is owned by the person.

The UNIX Shell compares character writing:

-eq equals

-ne is not equal to

-GT Greater than

-lt less than

-le less than or equal

-ge is greater than or equal to

-Z Empty string

= Two characters equal

!= two-character range

-N Non-empty string

-------------------------------------------------------------------------

A more detailed description:

Operator Description Example

File comparison Operators

-e filename true if filename exists [-e/var/log/syslog]

-D filename True if filename is a directory [-d/tmp/mydir]

-F filename True if filename is a regular file [-f/usr/bin/grep]

-L filename True if filename is a symbolic link [-l/usr/bin/grep]

-R filename True if filename is readable [-r/var/log/syslog]

-W filename true if filename is writable [-w/var/mytmp.txt]

-X filename is true if filename is executable [-l/usr/bin/grep]

Filename1-nt filename2 is True if filename1 is newer than filename2 [/tmp/install/etc/services-nt/etc/services]

Filename1-ot filename2 is true if filename1 is older than filename2 [/boot/bzimage-ot Arch/i386/boot/bzimage]

String comparison operators (note the use of quotes, which is a good way to prevent spaces from disrupting code)

-Z String True if string length is zero [-Z $myvar]

-N String True [-N $myvar] If string length is Non-zero

string1 = string2 true if string1 is the same as string2 [$myvar = one Two three]

String1!= string2 True if string1 is different from string2 [$myvar!= one Two three]

Arithmetic comparison operators

Num1-eq num2 equals [3-eq $mynum]

Num1-ne num2 Not equal to [3-ne $mynum]

Num1-lt num2 less than [3-lt $mynum]

Num1-le num2 less than or equal to [3-le $mynum]

NUM1-GT num2 is greater than [3-GT $mynum]

Num1-ge num2 greater than or equal to [3-ge $mynum]

Script Example:

#!/bin/bash

# This script prints a message about the your weight if you give it your

# Weight in kilos and hight in centimeters.

if [! $# = 2]; Then

echo "Usage: $ Weight_in_kilos length_in_centimeters"

Exit

Fi

Weight= "$"

Height= "$"

idealweight=$[$height-110]

If [$weight-le $idealweight]; Then

echo "You are should eat a bit more fat."

Else

echo "You are should eat a bit more fruit."

Fi

# weight.sh 70 150

You are should eat a bit more fruit.

# weight.sh 70 150 33

Usage:./weight.sh Weight_in_kilos Length_in_centimeters

Position parameters $,..., $N, $ #代表了命令行的参数数量, and $ $ represents the name of the script,

The first argument represents $, the second parameter represents $, and so on, the total number of parameters is $ #中, and the example above shows how to change the script and print out a message if there are fewer than 2 arguments or more.

Execute and view the situation.

# bash-x tijian.sh 60 170

+ weight=60

+ height=170

+ idealweight=60

+ ' [' 60-le 60 '] '

+ echo ' You should eat a bit more fat. '

You are should eat a bit more fat.

Where-X is used to check the execution of the script.

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.