Basic knowledge of Centos Shell programming

Source: Internet
Author: User
From the programmer's perspective, Shell itself is a program written in C language. From the user's perspective, Shell is a bridge between the user and the Linux operating system. You can enter commands for execution and use Shell script programming to perform more complex operations. Today, with the increasingly sophisticated LinuxGUI, Shell programming still plays an important role in system management and other fields. A deep understanding of Shell programming is one of the required lessons for every Linux user. Linux

From the programmer's perspective, Shell itself is a program written in C language. From the user's perspective, Shell is a bridge between the user and the Linux operating system. You can enter commands for execution and use Shell script programming to perform more complex operations. With the increasingly sophisticated Linux GUI, Shell programming still plays an important role in system management and other fields. A deep understanding of Shell programming is one of the required lessons for every Linux user.

Linux has many Shell types, including: Bourne Shell (/usr/bin/sh or/bin/sh) And Bourne Again Shell (/bin/bash) C Shell (/usr/bin/csh), K Shell (/usr/bin/ksh), Shell for Root (/sbin/sh), and so on. Different Shell languages have different syntaxes, so they cannot be exchanged. Each Shell has its own characteristics. Basically, it is enough to master any of them. In this article, we focus on Bash, also known as Bourne Again Shell, which is widely used in daily work due to ease of use and free of charge, bash is also the default Shell for most Linux systems. In general, people do not distinguish between the Bourne Shell and the Bourne Again Shell. Therefore, in the following text, we can see #! /Bin/sh, which can also be changed #! /Bin/bash.

The format of Shell scripts written in a text editor such as vi is fixed as follows:

 

       
        #!/bin/sh #comments Your commands go here
       

Symbol #! In the first line #! Tell the system that the program specified in the subsequent path is the Shell program that interprets the script file. If there is no such sentence in the first line, an error will occur when executing the script file. The subsequent part is the main program. Shell scripts, like advanced languages, also have variable assignment and control statements. Except the first line, the line starting with # is the comment line until the end of this line. If a row is not complete, add \ at the end of the row. This symbol indicates that the next row is merged into the same row with this row.

After editing, save the script disk as filename. sh. The file name suffix sh indicates this is a Bash script file. When executing a script, you must first change the attributes of the script file to executable:

 

       
        chmod +x filename.sh
       

The script execution method is as follows:

 

       
        ./filename.sh
       

Let's start with the classic "hello world" and look at the simplest Shell script.

 

       
        #!/bin/sh #print hello world in the console window a = "hello world" echo $a
       

Shell Script is a weak type language. When using a variable, you do not need to declare its type first. The new variable is stored in the memory allocated in the local data zone. This variable is owned by the current Shell and cannot be accessed by any sub-process. These variables are different from the environment variables. They are stored in another memory zone called the user environment zone. The variables in this memory can be accessed by the quilt process. The variable assignment method is as follows:

 

       
        variable_name = variable_value
       

If a value is assigned to a variable that already has a value, the new value replaces the old value. $ Must be added before the variable name. $ variable_name can be used in quotation marks. This is obviously different from other advanced languages. In case of confusion, you can use curly brackets to distinguish them. For example:

 

       
        echo "Hi, $as"
       

"Hi, hello worlds" is not output, but "Hi," is output ,". This is because Shell treats $ as a variable, while $ as is not assigned a value, and its value is null. The correct method is:

 

       
        echo "Hi, ${a}s"
       
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.