Linux Shell Programming Learning Note one: Shell introduction

Source: Internet
Author: User

Q: What is a shell and what is a shell script?
A:shell is a program written in C, which is a bridge between the user and the Linux operating system. The user can either enter command execution or use shell scripting to do more complicated operations. It is the general designation of command Language, command interpreter and programming language. Its role is to follow a certain syntax to interpret the input commands and pass them on to the system.
command interpreter : The shell is a command language interpreter with its own built-in shell command set, and the shell can be called by other applications in the system. Commands entered by the user at the prompt are interpreted by the shell before being passed to the Linux kernel. It reads the user-entered command from the input device, turns it into a mechanical code that the computer can understand, and then executes it.
Command Language : It interactively interprets and executes commands entered by the user. When a normal user logs on successfully, the system executes a program called the shell. It is the shell process that provides the command line prompt. For ordinary users with "$" as a prompt, the superuser (root) with "#" as the prompt. Once the shell prompt appears, you can type the command name and the parameters required by the command. The shell executes the commands. If a command takes a long time to run, or produces a lot of output on the screen, you can interrupt it by pressing CTRL + C from the keyboard (abort it before it ends). When the user is ready to end the logon dialog process, you can type the logout command, Exit command, or file Terminator (EOF) (implemented by Ctrl+d) to end the login.
programming language (Shell script): Another important feature of the shell is that it is itself an interpreted programming language, and the Shell programming language supports most of the program elements that can be seen in high-level languages, such as functions, variables, arrays, and program control structures. Any command that can be typed at the prompt can be placed in an executable shell program. As a programming language, it defines variables and parameters, and provides many control structures in higher-order languages, including loops and branches. Although not part of the Linux kernel, it invokes most of the functionality of the system kernel to execute programs, create documents, and coordinate the operation of individual programs in parallel. Simply put, a shell script is a file that contains several lines of shell or Linux commands. For a large number of commands that are written and used more than once, you can save them using a separate file. So that you can reuse it later

The first shell program, which is saved as a mayuan.sh file (usually a shell script with an. sh suffix):

#! : This is a contract tag that tells the system what interpreter the script needs to execute, even if it uses a shell. such as: #!/bin/sh,#!/bin/bash,#!/bin/csh,#!/bin/tcsh and, #!/bin/ksh and so on.

The second line starts with # and represents a comment line, which is ignored by the interpreter. In addition to the first line, the lines begin with a comment line, and there is no multiline comment in sh, only one # for each line. What if, in the course of development, you encounter a large segment of code that needs to be annotated temporarily and then uncomment later? Each line with a # symbol is too laborious, you can put this piece of code to be annotated with a pair of curly braces, defined as a function, there is no place to call this function, the code will not be executed, to achieve the same effect as the annotation.

The third line defines a variable name with a value of: Mayaun. Note that there cannot be spaces between the variable name and =, otherwise the command not found is prompted when the script is executed.

The four-line echo: command is used to output text to a window. The $ (dollar sign) represents a variable substitution, which replaces the variable with the value of the variable specified later.

There are two methods of executing shell scripts:
1. As an executable program

Save the code as mayuan.sh and CD to the appropriate directory:
chmod +x./mayuan.sh #使脚本具有执行权限
./mayuan.sh #执行脚本
Note, be sure to write./mayuan.sh, not mayuan.sh, run other binary programs as well, direct write Mayuan.sh,linux system will go to the path to find there is no mayuan.sh, and only/bin,/sbin,/usr/ Bin,/usr/sbin wait in path, your current directory is usually not in path, so write mayuan.sh will not find the command, to use. /mayuan.sh told the system that it was looking in the current directory. As shown in the following:

2. As an interpreter parameter

This works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:
/bin/bash mayuan.sh
The script that runs this way does not need to specify the interpreter information in the first line, and it is useless to write.

Linux Shell Programming Learning Note one: Shell introduction

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.