Shell Scripting Considerations

Source: Internet
Author: User

Set foot name as Test.sh

The first line should be #! /bin/bash

1, the operation and debugging results are not the same

Debug Sh-x test.sh when calculating two-digit and sum=$[$a + $b] to get sum=3+4 instead of 7

Run to give script execution permission first, executive section results 7

2, after the same line, there are other commands to be separated by semicolons, if the condition is determined after the then keyword, and then the command

3, the judgment condition uses [] to enclose, inside the condition and between the brackets to have the space division, and the arithmetic comparison cannot use the operator only-lt,-gt,-le.-ge,-eq,-ne;

To use an operator, use (()) to include a judgment statement, note that two parentheses

Format of If statement

If Judgment statement one; Then

Command

Elif Judgment Statement two; Then

Command

Else

Command

Fi

4, the shell script if also often judge about the file attributes, such as the judge is a normal file or directory, determine whether the file has read and write execution permissions. There are several options that are commonly used:

-E: Determine if a file or directory exists

-D: Determine if the directory is not present and whether it exists

-F: Determine if the file is normal and exists

-R: Determine if the document has Read permissions

-W: Determine if Write permission is available

-X: Determine if executable

If you use the If judgment, the format is: if [-e filename]; Then

5, echo xxx >> filename is to append the content to the file to the last

echo XXX > FileName Clears the contents of the file and then writes the new content

6. No spaces between equals and variables when assigning values to variables

7, in the shell script, in addition to using if to determine the logic, there is a common way, that is case. The specific format is:

Case variable in

value1)

Command

;;

value2)

Command

;;

VALUE3)

Command

;;

*)

Command

;;

Esac

In the above structure, there is no limit to the number of values, and * represents a value other than the above.

Shell Scripting Considerations

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.