Linux shell if Parameters

Source: Internet
Author: User

In linux shell if parameter shell programming, the if statement is used to determine the parameter www.2cto.com-B. When the file exists and is a block file, true-c is returned when the file exists and is a character file. -d. When pathname exists and is a directory, true-e is returned. When the file or directory specified by pathname exists, true-f is returned. If the file exists and is a regular file, true-g is returned. if the file or directory specified by pathname exists and the SGID bit is set, the returned result is true-h. If the file exists and is a symbolic link file, the returned result is true, this option is invalid in some old systems.-k. When a file or directory specified by pathname exists and a "Sticky" bit is set, true-p is returned when the file exists and is a command pipeline. true: www.2cto.com-r: returns true-s when the file or directory specified by pathname exists and is readable. If the file size is greater than 0, true-u is returned. When the file size is greater than 0, files or directories specified by pathname are returned. exist and If the SUID bit is set, true is returned. If the file or directory specified by pathname exists and is executable, true is returned. A directory must be executable for its content access. -O returns true if a file or directory specified by pathname exists and the user specified by the valid user ID of the current process is owned by it. Compare characters in UNIX Shell:-eq equals-ne is not equal to-gt is greater than-lt is less than-le is less than or equal to-ge is greater than or equal to-z empty string = two characters are equal! = Two character unequal-n non-empty string ------------------------------------------------------------------------- for more details: Operator Description Example file comparison operator-e filename if filename exists, it is true [-e/var/log/syslog]-d filename. If filename is a directory, it is true [-d/tmp/mydir]-f filename. If filename is a regular file, it is true [-f/usr/bin/grep]-L filename. If filename is a symbolic link, it is true [-L/usr/bin/grep]-r filename. If filename is readable, it is true [-r/var/log/syslog]-w filename if f Ilename can be written, it is true [-w/var/mytmp.txt]-x filename if filename can be executed, true: [-L/usr/bin/grep] filename1-nt filename2. If filename1 is newer than filename2, if [/tmp/install/etc/services-nt/etc/services] filename1-ot filename2 is earlier than filename2, it is a true [/boot/bzImage-ot arch/i386/boot/bzImage] string comparison operator (please note the use of quotation marks, this is a good way to prevent space from disturbing the code) -z string: If the string length is zero, it is true [-z $ myvar]-n string. If the string length is not zero, it is true. [-N $ myvar] string1 = string2 if string1 is the same as string2, it is true [$ myvar = one two three] string1! = String2 if string1 is different from string2, it is true [$ myvar! = One two three] arithmetic comparison operator num1-eq num2 equals to [3-eq $ mynum] num1-ne num2 not equal to [3-ne $ mynum] num1-lt num2 less than [3 -lt $ mynum] num1-le num2 is less than or equal to [3-le $ mynum] num1-gt num2 is greater than [3-gt $ mynum] num1-ge num2 is greater than or equal to [3 -ge $ mynum] script example: www.2cto.com #! /Bin/bash # This script prints a message about your weight if you give it your # weight in kilos and hight in centimeters. if [! $ #== 2]; then echo "Usage: $0 weight_in_kilos length_in_centimeters "exit fi weight =" $1 "height =" $2 "idealweight = $ [$ height-110] if [$ weight-le $ idealweight]; then echo "You shoshould eat a bit more fat. "else echo" You shoshould eat a bit more fruit. "fi # weight. sh 70 150 You shoshould eat a bit more fruit. # weight. sh 70 150 33 Usage :. /weight. sh weight_in_kilos length_in_centimeters location parameter $1, $2, ..., $ N, $ # represents the number of parameters in the command line, $0 represents the Script Name, the first parameter represents $1, the second parameter represents $2, and so on, the total number of parameters exists in $ #. The preceding example shows how to change the script. If the number of parameters is less than or more than two, a message is printed. Run the command and check the status. Www.2cto.com # bash-x tijian. sh 60 170 + weight = 60 + height = 170 + idealweight = 60 + '['60-le 60']' + echo 'You shoshould eat a bit more fat. 'You should eat a bit more fat. -x is used to check the execution of the script.
 

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.