Shell script programming 01: Basic knowledge

Source: Internet
Author: User

Shell scripts are similar to batch processing in Windows/DOS, that is, various commands are pre-placed into a file, and then the file can be executed to achieve similar features as batch processing, it is mainly used by administrators for setting or managing.

In other words, the shell script is written using the shell function.ProgramThis program uses a plain text file to write some shell syntax and instructions in it, and then uses regular notation, pipeline commands, data stream redirection, and other functions, in order to achieve the purpose of processing.

It is necessary to explain the differences between Shell and shell scripts.

Shell is a command line interpreter. Its function is to follow certain syntaxes to explain the input commands and send them to the system. It provides you with an interface system-level program that sends requests to Linux to run the program. You can use shell to start, suspend, stop, or even write some programs. Shell itself is a program written in C language, which serves as a bridge between users using Linux.

Shell is both a command language and a programming language (shell script ). As a command language, it interactively interprets and executes user-input commands. As a programming language, it defines various variables and parameters, it also provides many control structures that are available only in higher-level languages, including loops and branches. Although it is not part of the Linux kernel, it calls most of the functions of the system kernel to execute programs, create documents, and coordinate the running of various programs in parallel.

1. Syntax Basics

Starting with 1.1

The shell script must start with the following statement line (the first line of the file must be used ):

#! /Bin/sh

"#! /Bin/sh "is the shell declaration, indicating that you are using the shell type and its path. If the statement is not declared, the script is executed in the default shell.

1.2 annotations

During shell programming, a sentence starting with # represents a comment until the end of this line.

Note: "#! /Bin/sh "is not a comment.

1.3 Variables

In shell programming, all variables are composed of strings and do not need to be declared. Assign values to variables using the following statement:

Var = "Hello World"

Note: there must be no spaces before and after "=" and no ";" at the end of the sentence.

2. Common commands

Echo "some text": print the text on the screen

Ls: file list

WC-l filename: calculates the number of file lines.

WC-W filename: calculates the number of words in the file.

WC-C filename: calculates the number of characters in the file.

CP sourcefile destfile: file copy

MV oldname newname: rename a file or move a file

Rm filename: delete an object

Grep 'string' filename: searches for strings in a file.

Cat filename: output file content to the screen

File filename: Obtain the file type.

Read var: prompt the user to input and assign the input value to the variable.

Find: Search for files

Tee: outputs data to the standard output device (screen) and files such as: somecommand | tee OUTFILE

Basename filename: returns the file name that does not contain the path.

Dirname filename: Path of the returned File

Sed: Find and replace

 

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.