Linux script writing BASICS (1)

Source: Internet
Author: User

1.1 Basic syntax Introduction

Start with 1.1.1

The program must start with the following line (must begin with the first line of the file ):

#! /Bin/sh

Symbol #! The parameter used to tell the system that the program is used to execute the file. In this example, we use/bin/sh to execute the program.

When editing a script, you must make it executable if you want to execute it.

To make the script executable:

Compile chmod + x filename to run it with./filename.

1.1.2 notes

During shell programming, a sentence starting with # represents a comment until the end of this line. We sincerely recommend that you use annotations in your program. If you have used annotations, you can understand the role and working principle of the script in a short time even if the script is not used for a long time.

1.1.3 Variables

You must use variables in other programming languages. In shell programming, all variables are composed of strings, and you do not need to declare variables. To assign a value to a variable, you can write as follows:

#! /Bin/sh

# Assign values to variables:

A = "Hello World"

# Print the content of variable:

Echo "A is :"

Echo $

Sometimes the variable name is easily confused with other words, such:

Num = 2

Echo "this is the $ numnd"

This does not print "this is the 2nd", but only prints "this is the", because shell will search for the value of the variable numnd, but there is no value for this variable. We can use curly braces to tell shell that we want to print the num variable:

Num = 2

Echo "this is the $ {num} Nd"

This will print: This is the 2nd

1.1.4 Environment Variables

Variables processed by the Export keyword are called environment variables. We will not discuss environment variables, because generally, only environment variables are used in the login 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.