Shell Programming Basics

Source: Internet
Author: User
Tags case statement

Receiving user parameters

#./example.sh One and three
The Shell scripting language includes variables for receiving user parameters, and based on the above command, the commonly used variables are as follows:

    • Use $ A To get the file name of the shell script, the above example.sh
    • Use $, $, $ ... corresponding to the N-position parameters, such as the One
    • With $ #对应着该脚本执行时带有的参数个数, the above $ #等于3
    • The $* corresponds to the parameters of all positions, the above $ #为one and the three
    • With $? Represents the execution return value of the last command, 0 indicates success, and a value other than 0 indicates a failure
      <br/> Determine the user's parameters
      1. File Test statement
      2. Logical Test Statement
      3. Integer value comparison statement
      4. string comparison statements

Note: Therefore, the syntax format of the test statement is: [conditional expression], the conditional expression has a space before and after each, and there must be a space between the operand and the operator in the conditional expression!!!
<br/>

Common parameters for file testing
operator function
-D Test whether the file is a directory type
-E Test whether the file exists
-F Determine if it is a generic file
-R Tests whether the current user has permission to read
-W Tests whether the current user has permission to write
-X Tests whether the current user has permission to execute

eg. #[-d/etc/fstab]
<br/>

Logical Test Operators
operator function
&& With operator, true on the left to calculate right
| | Or operator, false on the left to calculate the right
! Non-operator, true and false inverse

eg. #[-e/dev/cdrom] && echo "Exist"
<br/>

Integer comparison Operators
operator function
-eq is equal to
-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

eg. #[10-ge 10]
<br/>

string comparison operators
operator function
= Compare strings for the same content
! = Compare strings for different content
-Z Determines whether the string contents are empty or undefined

eg. #[-Z $String]
<br/>

Process Control Statements
    1. If statement
    2. For statement
    3. While statement
    4. Case statement
      <BR/> If statement syntax

      Single Branch

      if 条件测试操作; then命令序列fi

      Dual Branch

      if 条件测试操作; then命令序列else命令序列fi

      Multi-Branch

      if 条件测试操作; then命令序列elif 条件测试操作; then命令序列else命令序列fi

      <br/>

      For statement syntax
      for 变量名 in 取值列表do命令序列done

      <br/>

      While statement syntax
      while 条件测试操作do命令序列done

      <br/>

      Case statement syntax
      case 变量值 in模式1)命令序列;;模式2)命令序列;;......*)默认命令序列esac

Shell Programming Basics

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.